Learn all about Functions in Javascript



Introduction

In Javascript, functions are one of the key building blocks on which our program relies. The block of the code that is required multiple times in the program can be written as a function, and that will see a lot of developer time as well as my the code more clean and readable. 

What is Function? 

A function is a block of code that can be executed whenever the function is called. Or another definition of the function is, a function is a named block of code that will execut when that function is called at runtime.

In Javascript, we can create functions in different ways and the syntax of the function depends on the type of the function we are creating.  

syntax:

function fxn_Name(){
  ...
  }

Type of function: 

There are different types of functions in programming languages as follows: 

  • Named Function
  • Anonymous Function
  • Immediately invoked function 

Named function:  

Function with the name is know as the Named function

Private function:  

A private function is a set of code that is available within the block of code.

Static function:  

A static function is a set of code that is available from the start of the application, there is only one copy of that code throughout the application at run time. 

Anonymous function:  

An anonymous function is same as a normal function but without a name, which means our function will not have any name.

syntax: function (){...........}

Arrow function:  

An arrow function will be a normal function but the syntax to initialize and definition of the anonymous function will be shorthanded.

Example: for a anonymous function the syntax to initialize the function is: 

                function (){

                .................

                 }

for an arrow function, we can write the above function as : 

            ()=>{ ...............  } 



1 Comments