Sitemap
Radheshyam Kumar

Radheshyam Kumar

Function statement, function declaration, function expression in JavaScript

3 min readAug 18, 2021

--

1 . Function statement/ Function declaration

A function statement or function declaration is made of function keyword, followed by function name, parameters in parenthesis (para1,..paraN) and a pair of curly braces {…..} to delimits the body code.

 //function declaration
function printName(name) {
console.log(name);}
//function invocation
printName(“Radheshyam”);

This way of creating a function is known as function statement or declaration.

2 . Function expression

You might get sometime confused between the function declaration and function expression as they look very similar but produce functions with different properties.

In JavaScript we can assign function to a variable .

var b =function(){console.log(“inside function b”)}
b();

This way of creating a function is known as function expression.

NOTE : Function declaration and function expression differs in terms of hoisting

see the example given below:

Press enter or click to view image in full size

Here calling the function fun2() before defining it caused an error whereas it perfectly worked for fun1(). The reason is that during the memory creation phase fun1 is created in memory and function is assigned to it. But in case of fun2()(created using function expression) , fun2 is treated as the other variables. So memory is created for fun2 but it was assigned undefined initially. So when the JavaScript engine started executing the code line by line and reaches to line no 9(see above img) then only the function is assigned to variable fun2 until then it was undefined. So at the time of calling fun2() (line no 2) value of fun2 was undefined ,that’s why it caused an error.

3. Anonymous function

An anonymous function is a function without a name.

function (){console.log(“Anonymous function”)}

But we can’t create anonymous function like this . This will cause an error :Function statement requires a function name.

Get Radheshyam Kumar’s stories in your inbox

Join Medium for free to get updates from this writer.

An anonymous function is often not accessible after its initial creation. So in order to call it later we assign the anonymous function to a variable .

var show = function () {
console.log(‘Anonymous function’);
};
show();

Using anonymous functions as arguments of other functions

setTimeout(function () { 
console.log(‘Execute later after 5second’)
}, 5000);

4. Named function expression

It is similar to the function expression when instead of using anonymous function ,we use function with a name.

var a = function xyz(){console.log(“inside function a”)}

Here instead of writing anonymous function we write the function with name “xyz”.

But xyz() can’t be accessed in the outer scope because it is not created in the outer scope .So it is not a function of outer scope .

Press enter or click to view image in full size

It is created locally so it can be accesed locally inside the function.

Press enter or click to view image in full size

Thanks Keep learning!!!!

--

--