Function Declaration Vs. Expression in Javascript

Dwipshikha Lodh
2 min readJul 25, 2021

We can do the same thing in both ways, but still there exist some differences which we should keep in mind.

Function declaration

In a function declaration, the syntax we follow is :

Syntax:

function func_name( ){

const k = 5;

}

func_name( );

  • Here we must use the “function” keyword and have a function name(in this case “func_name”), then we need to define our function & for the function call, we just need to use the function name along with parameters (if any).

Function Expression

In Function Expression, the syntax we follow is :

Syntax:

const func = function ( ) {

const k = 5;

}

func( );

  • Here we assign our function to a variable( in this case to “func”) and do use the “function” keyword. We don’t need to mention any function name and so while calling the function, we can just use the variable name( in this case “func”) and pass the parameters(if any).

One of the differences that exist is:

In Function Declaration:

We can invoke the function anywhere we want ie., either before the declaration of the function or even after the declaration of the function.

In Function Expression:

But in this case, we can invoke the function only after its declaration. If we try to invoke the function before its declaration, we will receive an error.

We can conclude that Function Expression doesn’t work like Function Declaration and the reason is Hoisting. It is a special feature of Javascript which at the time of execution shifts all the declarations at the top of the file, so it seems like the calling the function is happening after the declaration of the function.

--

--

Dwipshikha Lodh

I'm a Second Year Student at Barak Valley Engineering College. I'm into web dev, coding & few others things & still learning all these things side by side.