Function declarations versus function expressions
We saw two ways by which functions are defined. Though they both serve identical purposes, there is a difference between these two types of declarations. Check the following example:
//Function expression functionOne(); //Error //"TypeError: functionOne is not a function var functionOne = function() { console.log("functionOne"); }; //Function declaration functionTwo(); //No error //Prints - functionTwo function functionTwo() { console.log("functionTwo"); }
A function declaration is processed when execution enters the context in which it appears before any step-by-step code is executed. The function that it creates is given a proper name (functionTwo()
in the preceding example) and this name is put in the scope in which the declaration appears. As it's processed before any step-by-step code in the same context, calling functionTwo()
before defining it works without an error.
However, functionOne()
is an anonymous...