Function Declaration
function keyword is the first word in statement then it is a function declaration.
Just as variable declaration must start with "var", function declaration must begin with "function".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function square(x) { // function declaration | |
return x*x; | |
} | |
console.log(square(5)); // 25 |
Function Expression
function keyword is not the first word in statement then it is a function expression.
function expression can be Anonymous function expression or named function expression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var square = function(x) { // Anonymous function expression | |
return x*x; | |
}; | |
console.log(square(5)); // 25 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = function square(x) { // Named function expression | |
return x*x; | |
}; | |
console.log(a(5)); |
Link 1 - Function Declaration
Link 2 - Anonymous Function Expression
Link 3 - Named Function Expression
No comments:
Post a Comment