Function Declarations versus Function Expressions
Function declarations are available in an execution context before any code is executed.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
document.writeln(sum(10,10));
function sum(num1, num2){
return num1 + num2;
}
</script>
</head>
<body>
</body>
</html>
The function declaration is equivalent with variable declaration:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
document.writeln(sum(10,10)); // ERROR
var sum = function(num1, num2){
return num1 + num2;
};
</script>
</head>
<body>
</body>
</html>
Home
JavaScript Book
Essential Types
JavaScript Book
Essential Types
Function:
- The Function Type
- Function Declarations versus Function Expressions
- Functions as Values
- Returning a function from a function
- Function arguments
- this for function context
- Function caller
- Function length property
- Function apply()
- Function.call() method
- Function's bind() method
- Function toLocaleString() and toString()