JavaScript Functions

javascript functions

Functions are the fundamental element of Javascript language and It is a just a code instruction wrap with given name in order to perform some task So that anytime it can be executed with passing necessary parameters into it.

Also functions are consider Higher order functions in javascript that because function can be passed as parameter or return from another function. These two behaviour rely on the functions being a first class citizen or treat as a value in the language.

Why functions are considered First class function/Citizen? Read more..

Function can be assigned to a variable and pass it around. So a Function is treated as a value.

Why functions are considered Higher Order Function? Read more..

This behaviour rely on the function being a fist class citizen in the language.

  • Function can be passed as a parameter.
  • A Function can return another function.

Even a variable can be assigned to a function. Weird ..!

Because of these behaviour, JavaScript function are called as first class citize and Higher order functions. But other object oriented programming languages or procedureal programming languages do not have these flexibility.

Other important things is, JavaScript is prototype base programming language. That’s mean objects connect with its parent object though prototype chain. At last object of that chain will be a object of Object.prototype. I will explain in another thread. Simply remember JavaScript is like one single type of object is holding all object though a hierarchy.

Way of make JavaScript functions.

Function Declaration

These types of function always Hoist with implementation current scope. So that function can be execute above the declaration.

You can read more on Javascript Hoisting in here

Function Expression

These type of function do not have a name, so we call it anonymous function, but variable which holding that function will give name for it.

Arrow Function – Introduce in ES6

This was introduced with ES6 and very easy to access outer scope in this syntax.

 Class Keyword — Introduce in ES6

This was introduced with ES6 and it is noting but syntactic sugar of regular function. How do you prove it’s a function? you can use typeof().

Function constructor– NOT RECOMMEND

You can use Function constructor to define a function. function body will be passed as second parameter in constructor. But this method is not recomend to use.


There are many ways to defined function in javascript and depend upon your requirement, so you have to decide which one is most suitable for that. Always remember Javascript a prototype base language. There are no any Object Oriented concepts but that can be achieved though prototype chain. In my opinion, Arrow function is the best and clear declaration of a function if you target on ES6 support bowsers.

One Reply to “JavaScript Functions”

Leave a Reply