DEV Community

Cover image for JavaScript function declarations and function expressions
Omor Faruk
Omor Faruk

Posted on

JavaScript function declarations and function expressions

In JavaScript, understanding the distinction between function declarations and function expressions is crucial for writing effective and predictable code. Here's a comprehensive breakdown:


๐Ÿง  Function Declarations

A function declaration defines a named function using the function keyword

function greet(name) {
  return `Hello, ${name}!`;
}
Enter fullscreen mode Exit fullscreen mode

Key Characteristics:

  • Hoisting:Function declarations are hoisted entirely, meaning both their name and body are moved to the top of their scope during the compilation phase. This allows you to call the function before its declaration in the code๎ˆ„๎ˆ†
  console.log(greet('Alice')); // Outputs: Hello, Alice!

  function greet(name) {
    return `Hello, ${name}!`;
  }
Enter fullscreen mode Exit fullscreen mode
  • Named Functions:Always have a name, which is useful for recursion and debugging

  • Global Scope (if declared globally):When declared in the global context, they become global functions


๐Ÿงฉ Function Expressions

A function expression involves creating a function and assigning it to a variable.

const greet = function(name) {
  return `Hello, ${name}!`;
};
Enter fullscreen mode Exit fullscreen mode

Key Characteristics:

  • Not Hoisted Only the variable declaration (greet) is hoisted, not the function assignment. Attempting to call the function before its definition results in an erro.
  console.log(greet('Alice')); // TypeError: greet is not a function

  const greet = function(name) {
    return `Hello, ${name}!`;
  };
Enter fullscreen mode Exit fullscreen mode
  • Anonymous or Named Can be anonymous (as above) or named. Named function expressions can aid in debuggin.
  const greet = function greetFunction(name) {
    return `Hello, ${name}!`;
  };
Enter fullscreen mode Exit fullscreen mode
  • Useful for Callbacks and IIFEs Commonly used in scenarios like callbacks or Immediately Invoked Function Expressions (IIFEs).
  (function() {
    console.log('IIFE executed!');
  })();
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Comparative Summery

Feature Function Declaration Function Expression
Hoisting Yes No
Function Name Required Optional
Callable Before Definition Yes No
Use Cases General-purpose functions Callbacks, IIFEs, closure

๐Ÿ“Œ When to Use Which?

  • Function Declarations: Ideal when you need to define reusable functions that can be invoked before their definition in the code.

  • Function Expressions: Preferable in situations requiring functions as valuesโ€”such as callbacks, event handlers, or when creating closures. They offer more control over the function's scope and timing of execution.


Understanding these differences ensures better control over function behavior and scope in your JavaScript programs.

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ

A function expression involves creating a function and assigning it to a variable.

Assigning a function to a variable and function expression are two different, unrelated things. Function expression is the creation of a function by using the function keyword at a point in the code where a statement would be invalid (i.e. as part of an expression). Whether or not you choose to store the created function in a variable is up to you, and has no bearing on the fact of it being function expression.

Collapse
 
theonlineaid profile image
Omor Faruk

You're absolutely right in distinguishing function expressions from just assigning a function to a variableโ€”they often get conflated, but technically theyโ€™re not the same.