Skip to main content

Command Palette

Search for a command to run...

JavaScript Functions: Function Declaration vs Function Expression

Updated
•4 min read•View as Markdown

What Are Functions?

A function is a reusable block of code that performs a specific task.

Instead of writing the same code multiple times, you can place it inside a function and call it whenever you need it.

Think of a function like a machine:

Input → Function → Output

Example: adding two numbers.

function add(a, b) {
  return a + b;
}

console.log(add(3, 5));

Output:

8

Here:

  • add is the function name

  • a and b are parameters

  • The function returns the result


Function Declaration

A function declaration defines a function using the function keyword followed by a name.

Syntax

function functionName(parameters) {
  // code to execute
}

Example

function greet(name) {
  console.log("Hello " + name);
}

greet("Rahul");

Output:

Hello Rahul

In this example:

  • greet is the function

  • "Rahul" is passed as an argument


Function Expression

A function expression stores a function inside a variable.

Syntax

const functionName = function(parameters) {
  // code
};

Example

const greet = function(name) {
  console.log("Hello " + name);
};

greet("Riya");

Output:

Hello Riya

Here:

  • The function does not need a name

  • It is stored inside the variable greet


Declaration vs Expression (Side-by-Side)

Function Declaration

function add(a, b) {
  return a + b;
}

Function Expression

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

Both do the same job, but they behave differently in certain situations.


Understanding Hoisting (Simple Explanation)

Hoisting means JavaScript moves certain declarations to the top of the code before execution.

This affects function declarations and function expressions differently.


Function Declaration and Hoisting

You can call the function before it is defined.

Example:

sayHello();

function sayHello() {
  console.log("Hello!");
}

This works because function declarations are hoisted.


Function Expression and Hoisting

Function expressions cannot be used before they are defined.

Example:

sayHello();

const sayHello = function() {
  console.log("Hello!");
};

This will cause an error because the variable exists but the function is not assigned yet.


Comparison Table

Feature Function Declaration Function Expression
Syntax function name(){} const name = function(){}
Hoisting āœ… Fully hoisted āŒ Not hoisted
Use before definition āœ… Yes āŒ No
Stored in variable āŒ No āœ… Yes
Flexibility Basic More flexible

When Should You Use Each?

Use Function Declarations When:

  • You want a simple reusable function

  • You want to call the function before its definition

  • Writing basic program logic

Example:

function calculateTotal(price, tax) {
  return price + tax;
}

Use Function Expressions When:

  • You want to store functions in variables

  • You want to pass functions as arguments

  • Working with callbacks or advanced patterns

Example:

const calculateTotal = function(price, tax) {
  return price + tax;
};

Simple Execution Flow of a Function Call

Program Starts
      ↓
Function is Defined
      ↓
Function is Called
      ↓
Code Inside Function Runs
      ↓
Result is Returned

Example:

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

console.log(multiply(4, 5));

Output:

20

Practice Assignment 🧠

Try implementing the following yourself.

1ļøāƒ£ Write a Function Declaration

Create a function that multiplies two numbers.

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

console.log(multiply(4, 3));

2ļøāƒ£ Write the Same Logic Using Function Expression

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

console.log(multiply(4, 3));

3ļøāƒ£ Try Calling Functions Before Definition

Experiment with this:

console.log(add(2, 3));

function add(a, b) {
  return a + b;
}

Now try this with a function expression:

console.log(add(2, 3));

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

Observe the difference and understand how hoisting works.