🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Function Expressions

AI & DATA SCIENCE // function-expressions

Function expressions assign an anonymous or named function to a variable. Unlike declarations, they are NOT hoisted.

Syntax

const myFunc = function(a, b) {
  return a + b;
};

// Named function expression
const factorial = function fact(n) {
  return n <= 1 ? 1 : n * fact(n - 1);
};

Deep Dive Course

A **function expression** creates a function and assigns it to a variable. They are **not hoisted** — you cannot call them before the line where they are defined. Named function expressions give the function a name only accessible inside its own body (useful for recursion and stack traces).

1Understanding Function Expressions

A function expression creates a function and assigns it to a variable. They are not hoisted — you cannot call them before the line where they are defined. Named function expressions give the function a name only accessible inside its own body (useful for recursion and stack traces).

💡

Use function expressions when you need to conditionally create a function or pass it as an argument.

editor.html
// Function expression - NOT hoisted
// console.log(multiply(2, 3)); // ReferenceError!

const multiply = function(a, b) {
  return a * b;
};

console.log(multiply(2, 3)); // 6
localhost:3000

2Practical Example

Here is a real-world application of Function Expressions showing how it is used in production JavaScript code.

editor.html
// Named function expression (name only inside)
const factorial = function fact(n) {
  return n <= 1 ? 1 : n * fact(n - 1); // fact available here
};
console.log(factorial(5)); // 120
// console.log(fact(5)); // ReferenceError - not outside
localhost:3000

3Best Practices

Follow these guidelines when working with Function Expressions:

1. Use const to declare function expressions

2. Name your function expressions for better stack traces

3. Function expressions passed as callbacks are common in JavaScript

⚠️

Tip: Use function expressions when you need to conditionally create a function or pass it as an argument.

editor.html
// Function expression - NOT hoisted
// console.log(multiply(2, 3)); // ReferenceError!

const multiply = function(a, b) {
  return a * b;
};

console.log(multiply(2, 3)); // 6
localhost:3000

Examples

Example 01Basic Usage
// Function expression - NOT hoisted
// console.log(multiply(2, 3)); // ReferenceError!

const multiply = function(a, b) {
  return a * b;
};

console.log(multiply(2, 3)); // 6
Example 02Advanced Example
// Named function expression (name only inside)
const factorial = function fact(n) {
  return n <= 1 ? 1 : n * fact(n - 1); // fact available here
};
console.log(factorial(5)); // 120
// console.log(fact(5)); // ReferenceError - not outside

Best Practices

  • Use const to declare function expressions
  • Name your function expressions for better stack traces
  • Function expressions passed as callbacks are common in JavaScript

Interview Question

What is the difference between a function declaration and a function expression?

Hint: Hoisting and timing of availability.

Function declarations are hoisted (available throughout their scope). Function expressions are not hoisted — they're only available after the assignment executes. Both create callable functions, but expressions can be used as values (passed to callbacks, stored in arrays, returned from other functions) more naturally.

Exercises

MediumPractice using Function Expressions in a real scenario.
View Solution
// Function expression - NOT hoisted
// console.log(multiply(2, 3)); // ReferenceError!

const multiply = function(a, b) {
  return a * b;
};

console.log(multiply(2, 3)); // 6

Frequently Asked Questions

What is the difference between a function declaration and a function expression?

Function declarations are hoisted (available throughout their scope). Function expressions are not hoisted — they're only available after the assignment executes. Both create callable functions, but expressions can be used as values (passed to callbacks, stored in arrays, returned from other functions) more naturally.

Related Functions

Function-DeclarationArrow-FunctionsHoistingAnonymous-Functions