DEV Community

kim-jos
kim-jos

Posted on • Edited on

2 2

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.

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay