Function declaration and Function Expression
adebomiolusegun
Posted on January 2, 2023
The function expression is nearly similar to the function declaration, but there are some differences.
The function declaration is created by first declaring the function keyword, followed by a code block where we put our code to be executed.
Function name(parameters){
Our code to be executed
}
name(argument);
And meanwhile, the function expression can be stored in a variable, which is then used as a function.
Const name = function ( parameters a, parameter b){
Our code to be executed
};
name(argument);
As an expression, the semicolon is placed at the end of the code block because that is where it is expected to be.
Hoisting
hoisting allows you to use functions and variables before they are declared.
This is done with function declaration but not with function expression.
Examples
name(argument);
Function name(parameters){
Our code to be executed
}
This is a function declaration; the code will be executed because JavaScript hoisting accepts it.
name(argument);
Const name = function ( parameters a, parameter b){
Our code to be executed
};
Meanwhile, because this is a function expression, the code will not run because JavaScript hoisting does not accept it.
It’s will display undefined.
I hope this is of great assistance to someone out there.
Posted on January 2, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 24, 2024