Open In App

Different ways of writing functions in JavaScript

Last Updated : 20 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A JavaScript function is a block of code designed to perform a specific task. Functions are only executed when they are called (or "invoked"). JavaScript provides different ways to define functions, each with its own syntax and use case.

Below are the ways of writing functions in JavaScript:

1. Function Declaration

A Function Declaration is the traditional way to define a function. It starts with the function keyword, followed by the function name and any parameters the function needs.

Example: Below is an example that illustrates the use of Function Declaration.

JavaScript
// Function declaration 
function add(a, b) {         
    console.log(a + b);
}

// Calling a function
add(2, 3);

Output
5

After defining a function, we call it whenever the function is required.

2. Function Expression

Function Expression is another way to define a function. Here, the function is defined inside a variable, and the function's value (its returned value) is stored in that variable.

Example: Below is an example that illustrates the use of Function Expression.

JavaScript
// Function Expression
const add = function (a, b) {
    console.log(a + b);
}

// Calling function
add(2, 3);

Output
5

Here, the whole function is an expression and the returned value is stored in the variable. We use the variable name to call the function.

3. Arrow Functions

Arrow Functions were introduced in ES6 (a newer version of JavaScript). They provide a shorter syntax for writing functions. Instead of using the function keyword, you use an arrow (=>).

Example: Below is the example that illustrates the use of the Arrow Function.

JavaScript
// Single line of code
let add = (a, b) => a + b;
console.log(add(3, 2));

Output
5

This shortens the code to a single line compared to other approaches. In a single line of code, the function returns implicitly.

Note: When there is a need to include multiple lines of code we use brackets. Also, when there are multiple lines of code in the bracket we should write return explicitly to return the value from the function.

Example: This is an example with multiple lines of code in arrow function

JavaScript
// Multiple line of code
const great = (a, b) => {
    if (a > b)
        return "a is greater";
    else
        return "b is greater";
}
console.log(great(3, 5));

Output
b is greater

Key Differences: Function Declaration vs. Function Expression

  • Function Declaration: The function is defined and can be called anywhere in the code (even before it’s defined, due to something called "hoisting").
  • Function Expression: The function is defined as part of an expression (usually assigned to a variable), and it can only be called after the line where it’s defined.

For a detailed comparison, you can refer to our article on the Difference between Function Declaration and Function Expression.


Next Article

Similar Reads