Javascript Function Declaration vs Expression
In this article, we will see what is the difference between javascript function declaration vs expression.
Function Declaration vs Expression
Function Declaration:
1. We can call a function before the function definition
wish(); // no error
function wish()
{
document.write("Hi");
}
2. We cannot create a variable for creating a function declaration
Syntax:
function functionname()
{
printing statement;
}
Function Expression:
1. We cannot call a function before the function definition
wish(); //error: wish is not a function
var wish = function()
{
document.write("Hi");
}
Note: Function declarations are hoisted not function expressions.
2. We should create a variable for creating a function Expression
Syntax:
var variablename = function()
{
printing statement;
}