What is a first class function
A first-class function means that functions are treated like any other value in JavaScript. They can be:
- Assigned to a variable
- Passed as an argument to another function
- Returned from another function
- Stored in objects or arrays
Example
function greet() { return "Hello"; } // Assigned to a variable const sayHello = greet; // Passed as an argument function execute(fn) { console.log(fn()); } execute(greet); // Hello
One-line Interview Answer
"In JavaScript, functions are first-class citizens, which means they can be assigned to variables, passed as arguments, returned from other functions, and stored in data structures like any other value."
Comments
Post a Comment