Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

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

Quick answer

A function declaration defines a function directly with a name, as in function double(n) { ... }. A function expression creates a function as part of an expression, often assigning it to a variable, as in const double = function(n) { ... };. Both can be called to return a value. Expressions can be named or anonymous, and the forms differ in initialization behavior, so do not assume they are available at the same point in your code. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I see both function greet() {} and const greet = function() {} in JavaScript. What is the practical difference?

Geez

The difference is how the function is introduced in the code.

A function declaration uses the function keyword followed by a function name and body:

function double(n) {
  return n * 2;
}

const result = double(4);
console.log(result); // 8

A function expression creates a function as part of an expression. A common pattern assigns it to a variable:

const double = function(n) {
  return n * 2;
};

const result = double(4);
console.log(result); // 8

In both examples, calling double(4) runs the function, and return n * 2 supplies the value 8. MDN describes declarations as the function name(...) { ... } form and expressions as functions created within expressions, often assigned to variables. [S1] [S2]

Follow-up question

Does the function in an expression need its own name? And why does the variable seem to be the name?

Geez

An expression can contain a named or anonymous function. In this example, function(n) has no name of its own, and the variable double is the name you use to refer to the assigned function:

const double = function(n) {
  return n * 2;
};

You can also give the function inside the expression a name:

const double = function multiplyByTwo(n) {
  return n * 2;
};

Here, the expression contains the name multiplyByTwo, while the assigned variable is double. For a basic call from surrounding code, use double(4). MDN confirms that a function expression may be named or anonymous and can be assigned to a variable. [S1] [S2]

Follow-up question

If both examples work, how should I decide which one to write? Is one always available sooner?

Geez

Choose based on how you want to organize the function, not because the two forms are interchangeable in every situation. A declaration makes a named function directly in the surrounding code. An expression is useful when you want to assign a function as a value, as in the const example, or use a function as part of an expression.

Do not assume they have identical initialization behavior. MDN notes that declarations and expressions differ in initialization behavior and availability. The examples here deliberately call each function after its definition or assignment; they do not demonstrate calling either one earlier. If order matters in your code, check the exact form and scope you are using rather than relying on a blanket rule. [S1] [S2]

Follow-up question

Can you give me a quick way to compare the syntax and check that both return the same value?

Geez

Use this side-by-side example. It defines each function before calling it and shows each returned value:

function declarationDouble(n) {
  return n * 2;
}

const expressionDouble = function(n) {
  return n * 2;
};

console.log(declarationDouble(4)); // 8
console.log(expressionDouble(4));  // 8

Read it in three steps:

  1. The declaration starts with function declarationDouble.
  2. The expression starts with const expressionDouble = and assigns a function to that variable.
  3. Each call passes 4; each function returns 8.

This comparison shows the syntax and result for these specific examples. It does not establish that declarations and expressions are available at the same point in every program. [S1] [S2]

A practical checklist

  • Look for function name(...) to identify a declaration.

  • Look for a function assigned as a value, such as const name = function(...) { ... };, to identify an expression.

  • Check whether the function expression has its own name or is anonymous.

  • Check the exact form and context before relying on when a function is available.

  • Follow the return statement to determine the value a call produces.

A few more useful answers

Does a function expression always have to be assigned to a variable?

No. It can be used as part of an expression in other contexts too. Assignment to a variable is simply a common pattern. [S1] [S2]

Do the declaration and expression examples return different kinds of values?

Not in these examples. Both functions use the same calculation and receive the same input, so each returns the number 8.

Can I call either form before the line where it appears?

Do not assume so from this comparison: declarations and expressions differ in initialization behavior. Check the specific form and context, or place the call after the definition or assignment as shown here. [S1] [S2]

Sources

References used for this guide.

  1. [S1] function — JavaScript | MDN
    developer.mozilla.org · Checked
  2. [S2] function expression — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work