Demystifying jargon's related to functions in JavaScript

Demystifying jargon's related to functions in JavaScript

Many times while reading articles or books related to functions in JavaScript, we encounter a lot of confusing terminologies. In this blog, I'll try to make some of those terms clear. Firstly functions behave much differently and have much more features in JavaScript compared to other languages.

Function Statement or Function Declaration

Yes, both Function Statement and Function Declaration mean the same.

What do they mean?

Writing or declaring a function using the function keyword is known as a function statement or function declaration.

function a(){
  console.log("Inside the function a");
}

Anonymous function

A function without any name is known as an Anonymous function in JavaScript. In order to use these functions, they must be assigned to a variable. Yes in JavaScript we can assign a function to a variable.

const b = function(){
  console.log("Inside a anonymous function assigned to b");
}

Function expression

Declaring a function and assigning the function to a variable is known as a function expression. We can assign either an Anonymous function or a named function to a variable.

#Assigning an Anonymous function to a variable 
const c = function (){
  console.log("Inside a anonymous function assigned to c");
}

#Assigning a named function to a variable
const d= function z(){
  console.log("Inside a named function assigned to d");
}

Firstclass functions in JavaScript

Firstclass functions mean nothing but we can use a function as a variable in JavaScript i.e.

  • assign a function to a variable
  • pass a function as an argument to another function
  • return a function from another function.
#Assign a function to a variable 
const e = function (){
  console.log("A function is assigned to e variable");
}
#Passing a function as an argument to another function 
function f (){
  console.log("Inside a function f");
}
function g(param1)(){
  param1();
}
g(f);

function h (){
  return function(){
   console.log("Inside returned function");
  }
}

Callback functions

A function passed as an argument to a function can be called inside the function. Such functions which are passed as arguments are known as callback functions.

function z(){
  console.log("Inside the callback function");
}
function y(param1){
  param1();
}
#z is the callback function
y(z);

Arrow Functions

Arrow functions were introduced into JavaScript with the release of ECMAScript 2015, also known as ES6.

#syntax of arrow functions
const arrowFunction = (params)=>{
  console.log("Inside the arrowFunction");
}
arrowFunction();

Differences between normal functions and Arrow functions are :

  • Syntax
  • Arguments binding.
  • arrow function doesn't accept duplicate parameters.
  • arrow functions cannot be used as a constructor function.

Here is a blog by Ashutosh Verma. It explained the above differences in a clear way.

And thanks to Akshay Saini for explaining all the above topics in his video. I would suggest you to watch the video for a better understanding.

I hope this blog has helped you understand the jargon of functions in JavaScript. This is my first blog. Write your comments It will help me in writing better blogs.

Thank you

Vinay Bajjuri

twitter