DEV Community

Shrushti Polekar
Shrushti Polekar

Posted on

More about 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/regular function definition.
Syntax:

function function_name(){
//statements
}
Enter fullscreen mode Exit fullscreen mode

function 1

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
}
Enter fullscreen mode Exit fullscreen mode

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

function

Also remember , function expressions are never hoisted!
So we cannot do something like this.

function

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.
function(){
 //body of function
};
Enter fullscreen mode Exit fullscreen mode

Store an anonymous function in a variable.

var variable_name = function(){
    //body of function;
}
Enter fullscreen mode Exit fullscreen mode

function
Passing anonymous functions as an argument in JavaScript.

anonymous functions

Now if we call myfunc inside show we get,

function

Returning anonymous function

function

Arrow Functions!
Arrow functions were introduced from ES6.

arrow functions

This was all for this blog!
Hope it helps!
Happy learning!

Top comments (0)