Function declaration and Function Expression

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.

Funct…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by adebomiolusegun

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.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by adebomiolusegun


Print Share Comment Cite Upload Translate Updates
APA

adebomiolusegun | Sciencx (2023-01-02T02:42:51+00:00) Function declaration and Function Expression. Retrieved from https://www.scien.cx/2023/01/02/function-declaration-and-function-expression/

MLA
" » Function declaration and Function Expression." adebomiolusegun | Sciencx - Monday January 2, 2023, https://www.scien.cx/2023/01/02/function-declaration-and-function-expression/
HARVARD
adebomiolusegun | Sciencx Monday January 2, 2023 » Function declaration and Function Expression., viewed ,<https://www.scien.cx/2023/01/02/function-declaration-and-function-expression/>
VANCOUVER
adebomiolusegun | Sciencx - » Function declaration and Function Expression. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/01/02/function-declaration-and-function-expression/
CHICAGO
" » Function declaration and Function Expression." adebomiolusegun | Sciencx - Accessed . https://www.scien.cx/2023/01/02/function-declaration-and-function-expression/
IEEE
" » Function declaration and Function Expression." adebomiolusegun | Sciencx [Online]. Available: https://www.scien.cx/2023/01/02/function-declaration-and-function-expression/. [Accessed: ]
rf:citation
» Function declaration and Function Expression | adebomiolusegun | Sciencx | https://www.scien.cx/2023/01/02/function-declaration-and-function-expression/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.