Difference between Function Declaration(or Function Statement) and Function Expression in Javascript?

Nihal Mahesh
2 min readFeb 26, 2021

--

You might be working for a very long time in javascript but sometimes we don't understand the jargons associated with it. So here I will be demystifying what these jargons mean and what is the difference associated with it.

Photo by Maxwell Nelson on Unsplash

So basically function declaration tells about the function name, return type and parameters. Example with a code snippet:-

function declaration

A function expression can be defined as a function stored in a variable. Example:-

Function Expression

So now the question will be what is the difference between these 2 when you call it.

Photo by Simone Secci on Unsplash

And the answer lies in Hoisting.

So if you call these 2 functions before declaring them function expression always throws an error because function expression has been attached with a variable and when the global execution context is created at that time value of the variable will be undefined for the very first time so that the reason why it will give you an error if you are calling the function before.

diff

Let me know in the comments if you face any issues. Till then Happy Coding.

--

--

Nihal Mahesh
Nihal Mahesh

Written by Nihal Mahesh

All about tech and programming

No responses yet