DEV Community

kim-jos
kim-jos

Posted on • Edited on

What are First Class Functions in JS?

Let's go over common jargons used in JS.

What is a function statement and a function expression?

//function statement
function statement() {
 console.log('statement')
}
//function expression
var expression = function () {
 console.log('expression');
}
Enter fullscreen mode Exit fullscreen mode

What is the difference between declaring a function as an expression vs. a statement?

The main difference between declaring functions this way is hoisting.

statement(); // prints 'statement'
expression(); // TypeError: expression is not a function

function statement() {
 console.log('statement')
}

var expression = function () {
 console.log('expression');
}
Enter fullscreen mode Exit fullscreen mode

When JS allocates memory it copies the whole function when it is declared as a statement. But, JS assigns a value of undefined for variables which is why JS does not recognize function expressions as functions.

What is an anonymous function in JS?

Anonymous functions are functions without names. If you declare a function without a name it returns a syntax error. Anonymous functions are used when functions are used as values. In the example above, a function expression uses an anonymous function where the function is a value and has no name.

function () {} // this in itself returns SyntaxError;
Enter fullscreen mode Exit fullscreen mode

What are first-class functions in JS?

First-class is the ability to use functions as values, arguments, and returned values.

Top comments (0)