JS: Function Terminology

Vivek Doshi
2 min readAug 10, 2020
Javascript Function

In Javascript or any other language,

We might have work with all kinds of syntax and diff stuff, but sometimes we are just not aware of that like this is called this, and it happens a lot in the interview because if you are not aware of the right terminology, you can’t explain the right thing.

So, Here is a small article about just Function Terminology in Javascript.

1 : Function Declaration:
It’s pretty simple, If the function is declared as a separate statement in the main code flow, that’s called a “Function Declaration”.

// This is called function declarationfunction village() {
console.log("Hidden Leaf Village");
}

2 : Function Expression :
But now, we are storing the anonymous function on some variable instead of just declaring it. If the function is created as a part of an expression, it’s called a “Function Expression”.

// This is called function expressionconst village = function () { 
console.log("Hidden Leaf Village");
}
function(){} // <---- anonymous function
function village(){} //<----- named function

3 : Factory Function :
A factory function is any function that is not a class or constructor that returns an object. In JavaScript, any function can return an object but when it does so without the new keyword, it’s a factory function.

function village(name) {
return {
name
}
}
const narutoVillage = village();

4 : Constructor function:
You can create a Constructor function with the help of new , ( note : you can use this only inside the constructor function.) The main use of this is, sometimes we need kind of blueprint for an object that we want to create at many places, so we can create a constructor function and then we can create an object as many as we want via new

function Village(name) {
this.name = name
}
const narutoVillage = new Village("Hidden Leaf Village");

5 : Method
So many of us might not be aware of that method and function are not the same thing, you have already seen the functions, then what is method then, any function that is being used within Object is called method

const ninja = {   name : "Naruto",
village : "Hidden Leaf Village",
info : function () { // <---- This is called method
console.log(`${this.name}, from ${this.village}`)
}
}
ninja.info(); // <--- This is a method

That’s it, folks, Thanks for giving your time and hope this might have helped you to get better.

--

--