var functionName = function() {} vs function functionName() {}
Function declaration vs Function expression
A function declaration is a declaration; it's not a statement or expression. A function declaration is processed when execution enters the context in which it appears, before any step-by-step code is executed. The function it creates is given a proper name , and that name is put in the scope in which the declaration appears.
functionOne - It is a function expression and so only defined when that line is reached.
functionTwo- It is a function declaration and is defined as soon as its surrounding function or script is executed.
Example-
Function Expression
// TypeError: functionOne is not a function
functionOne();
var functionOne = function() {
console.log("Hello!");
};
Function declaration:
// Outputs: "Hello!"
functionTwo();
function functionTwo() {
console.log("Hello!");
}
0 comments:
Post a Comment