Functions in JavaScript (original) (raw)

Last Updated : 15 Apr, 2025

Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.

JavaScript `

function sum(x, y) { return x + y; } console.log(sum(6, 9));

`

**Function Syntax and Working

A function definition is sometimes also termed a function declaration or function statement. Below are the rules for creating a function in JavaScript:

**Return Statement

In some situations, we want to return some values from a function after performing some operations. In such cases, we make use of the return. This is an optional statement. In the above function, “sum()” returns the sum of two as a result.

**Function Parameters

Parameters are input passed to a function. In the above example, sum() takes two parameters, x and y.

**Calling Functions

After defining a function, the next step is to call them to make use of the function. We can call a function by using the function name separated by the value of parameters enclosed between the parenthesis.

JavaScript `

// Function Definition function welcomeMsg(name) { return ("Hello " + name + " welcome to GeeksforGeeks"); }

let nameVal = "User";

// calling the function console.log(welcomeMsg(nameVal));

`

Output

Hello User welcome to GeeksforGeeks

Why Functions?

Function Invocation

The function code you have written will be executed whenever it is called.

**Function Expression

It is similar to a function declaration without the function name. Function expressions can be stored in a variable assignment.

**Syntax:

let geeksforGeeks= function(paramA, paramB) {
// Set of statements
}

JavaScript `

const mul = function (x, y) { return x * y; }; console.log(mul(4, 5));

`

Arrow Functions

Arrow functions are a concise syntax for writing functions, introduced in ES6, and they do not bind their own this context.

**Syntax:

let function_name = (argument1, argument2 ,..) => expression

JavaScript `

const a = ["Hydrogen", "Helium", "Lithium", "Beryllium"];

const a2 = a.map(function (s) { return s.length; });

console.log("Normal way ", a2);

const a3 = a.map((s) => s.length);

console.log("Using Arrow Function ", a3);

`

Output

Normal way [ 8, 6, 7, 9 ] Using Arrow Function [ 8, 6, 7, 9 ]

Immediately Invoked Function Expression (IIFE)

IIFE functions are executed immediately after their definition. They are often used to create isolated scopes.

JavaScript `

(function () { console.log("This runs immediately!"); })();

`

Output

This runs immediately!

Callback Functions

A callback function is passed as an argument to another function and is executed after the completion of that function.

JavaScript `

function num(n, callback) { return callback(n); }

const double = (n) => n * 2;

console.log(num(5, double));

`

Anonymous Functions

Anonymous functions are functions without a name. They are often used as arguments to other functions.

JavaScript `

setTimeout(function () { console.log("Anonymous function executed!"); }, 1000);

`

Nested Functions

Functions defined within other functions are called nested functions. They have access to the variables of their parent function.

JavaScript `

function outerFun(a) { function innerFun(b) { return a + b; } return innerFun; }

const addTen = outerFun(10); console.log(addTen(5));

`

Pure Functions

Pure functions return the same output for the same inputs and do not produce side effects. They do not modify state outside their scope, such as modifying global variables, changing the state of objects passed as arguments, or performing I/O operations.

JavaScript `

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

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

`

Key Characteristics of Functions

Advantages of Functions in JavaScript

Choosing the Right Function Type