Diving deeper into JavaScript functions!

We all know functions are group of statements that perform a specific task. Functions in JavaScript exhibit various forms .We have a variety of types in functions in JavaScript , like function expression, Anonymous functions, Arrow functions and IIFE.

Lets get started with some basic function pattern which we normally see in other programming languages as well i.e. normal function definition.

Syntax:

function function_name(){

//statements

}

Output:

hii

This was pretty easy! Now lets have a look at some other types.

Function Expressions

In JavaScript we can create functions and can assign them to variables as well!

Yes , you read it right! We can assign functions to variables.

Syntax:

var variable_name =function function_name(){

//statements

}

Output:

hii

We cannot call sayhi() directly, it results into reference error.

Also remember , function expressions are never hoisted!

So we cannot do something like this.

This results into TypeError.

Anonymous Functions

Anonymous functions are functions with no specific name.

They can be stored in a variable.

Can be returned in a function .

Can be passed in a function.

Syntax:

function(){

body of function

};

Store an anonymous function in a variable.

var variable_name = function(){

body of function;

}

Output:

hiii

Passing anonymous functions as an argument in JavaScript

Output:

Now if we call myfunc inside show we get,

Output:

Returning anonymous function

Output:

30

Arrow Functions!

Arrow functions were introduced from ES6.

Output:

30

This was all for this blog!

Hope it helps!

Happy learning!

--

--