What does "first-class citizen" mean in JavaScript?

1 min read
First Class Citizen means being able to do others can do. In JavaScript functions are first class citizen. Here arise the question why? Because you can it can be assigned to variables, passed as a argument and has property and methods. Let’s try to understand the by examples.
You can assign function to variable like below":
const myFunction = function() {
console.log("Hello");
};
You can return as a value:
function createGreeter(greeting) {
return function(name) {
console.log(`${greeting}, ${name}!`);
};
}
const sayHi = createGreeter("Hi");
sayHi("Sameer"); // Outputs: Hi, Sameer
Functions can be stored within arrays and objects:
const functionArray = [myFunction, sayHi];
const functionObject = { greet: sayHi };
With these examples we can understand that functions meet all the criteria to be first class citizen.
0
Subscribe to my newsletter
Read articles from MD SAMEER ALI directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
