GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • JavaScript Function Examples
  • Introduction3 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Functions in JavaScript - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

Functions in JavaScript

Last Updated : 17 Jun, 2026

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

Understanding Functions

In functions, parameters are placeholders defined in the function, while arguments are the actual values you pass when calling the function.

Example:

JavaScript
function greet(name) {   // 'name' is a parameter
  console.log("Hello " + name);
}

greet("Alice");  // "Alice" is the argument
  • Parameter: name (placeholder inside the function).
  • Argument: "Alice" (real value given at call time).

Default Parameters

  • Default parameters are used when no argument is provided during the function call.
  • If no value is passed, the function automatically uses the default value.
JavaScript
function greet(name = "Guest") {
  console.log("Hello, " + name);
}

greet();
greet("Aman");  

Return Statement

  • The return statement is used to send a result back from a function.
  • When return executes, the function stops running at that point.
  • The returned value can be stored in a variable or used directly.
JavaScript
function add(a, b) {
  return a + b; // returns the sum
}

let result = add(5, 10);
console.log(result); 

Types of Functions

Here are all the main types of functions in JavaScript:

1. Named Function

A function that has its own name when declared. It’s easy to reuse and debug because the name shows up in error messages or stack traces.

JavaScript
function greet() {
  return "Hello!";
}
console.log(greet()); 

2. Anonymous Function

An anonymous function is a function defined without an explicit name. It is commonly used as a callback or assigned to a variable.

JavaScript
const greet = function() {
  return "Hi there!";
};
console.log(greet()); 

3. Function Expression

A function expression is a function created as part of an expression and assigned to a variable or passed to another function. It can be named or anonymous.

JavaScript
const add = function(a, b) {
  return a + b;
};
console.log(add(2, 3));

4. Arrow Function (ES6)

A new way to write functions using the => syntax. They are shorter and do not have their own this binding, which makes them useful in some cases.

JavaScript
const square = n => n * n;
console.log(square(4));

5. 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!");
})();

6. 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));

7. Constructor Function

A special type of function used to create multiple objects with the same structure. It’s called with the new keyword.

JavaScript
function Person(name, age) {
  this.name = name;
  this.age = age;
}

const user = new Person("Neha", 22);
console.log(user.name); 

8. Async Function

Functions that handle asynchronous tasks. Declared with async, they return a Promise, and you can use await inside them to pause until another Promise resolves.

JavaScript
async function fetchData() {
  return "Data fetched!";
}
fetchData().then(console.log);

9. Generator Function

Declared with an asterisk *, these functions can pause execution using yield and resume later. Useful for lazy loading values or handling iterators.

JavaScript
function* numbers() {
  yield 1;
  yield 2;
  yield 3;
}

const gen = numbers();
console.log(gen.next().value); 
console.log(gen.next().value); 

10. Recursive Function

A function that calls itself until a condition is met. Very useful for problems like factorial, Fibonacci, or tree traversals.

JavaScript
function factorial(n) {
  if (n === 0) return 1;
  return n * factorial(n - 1);
}
console.log(factorial(5));

11. Higher-Order Function

A function that either takes another function as a parameter or returns another function. These are common in JavaScript (e.g., map, filter, reduce).

JavaScript
function applyOperation(a, b, operation) {
    return operation(a, b);
}
function add(x, y) {
    return x + y;
}
console.log(applyOperation(2, 3, add));
  • applyOperation() is a higher-order function because it accepts another function (add) as an argument.
  • The passed function is executed inside applyOperation().

12. 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));

13. 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));

14. Rest Parameter Function

Uses the ... syntax to collect remaining arguments into an array. Useful when the number of arguments is unknown.

JavaScript
function sum(...nums) {
  return nums.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3, 4)); 

JavaScript Examples:

The following JavaScript section contains a wide collection of JavaScript examples, read here:

  • JavaScript Function Examples
Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.