Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Function Expressions | Modern and Advanced Function Patterns
Functions in JavaScript

bookFunction Expressions

A function expression in JavaScript lets you define a function as a value and assign it to a variable. Unlike a function declaration, which starts with the function keyword at the top level, a function expression creates a function inside an expression and usually stores it in a variable or passes it as an argument.

The main differences between function expressions and declarations are:

  • Function declarations are hoisted. They are available in their scope before any code runs;
  • Function expressions are not hoisted and can only be used after their definition.

This distinction makes function expressions more flexible in situations where you want to create functions conditionally or pass them around as values.

1234567
// Assign a function expression to a variable const square = function (number) { return number * number; }; // Call the function using the variable console.log(square(5));
copy

When to Use Function Expressions

Use function expressions in the following situations:

  • When you need to assign a function to a variable for later use;
  • When you want to pass a function as an argument to another function, such as with array methods like map or filter;
  • When you want to create an anonymous function that does not require a name;
  • When you need more control over the timing of function creation, since function expressions are not hoisted;
  • When you want to keep your code modular and flexible by treating functions as values.

Function expressions help you write concise, maintainable code, especially when working with callbacks, event handlers, or higher-order functions.

question mark

Which code snippet demonstrates a function expression?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 1

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Awesome!

Completion rate improved to 7.69

bookFunction Expressions

Veeg om het menu te tonen

A function expression in JavaScript lets you define a function as a value and assign it to a variable. Unlike a function declaration, which starts with the function keyword at the top level, a function expression creates a function inside an expression and usually stores it in a variable or passes it as an argument.

The main differences between function expressions and declarations are:

  • Function declarations are hoisted. They are available in their scope before any code runs;
  • Function expressions are not hoisted and can only be used after their definition.

This distinction makes function expressions more flexible in situations where you want to create functions conditionally or pass them around as values.

1234567
// Assign a function expression to a variable const square = function (number) { return number * number; }; // Call the function using the variable console.log(square(5));
copy

When to Use Function Expressions

Use function expressions in the following situations:

  • When you need to assign a function to a variable for later use;
  • When you want to pass a function as an argument to another function, such as with array methods like map or filter;
  • When you want to create an anonymous function that does not require a name;
  • When you need more control over the timing of function creation, since function expressions are not hoisted;
  • When you want to keep your code modular and flexible by treating functions as values.

Function expressions help you write concise, maintainable code, especially when working with callbacks, event handlers, or higher-order functions.

question mark

Which code snippet demonstrates a function expression?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 1
some-alt