codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

14 / 65 topics
14JavaScript Functions15JavaScript Function Expressions16JavaScript Arrow Functions17JavaScript Variable Scope18JavaScript Hoisting19JavaScript Closures20JavaScript Recursion
Tutorials/JavaScript/JavaScript Functions
🌐JavaScript

JavaScript Functions

Updated 2026-04-20
4 min read

Introduction

Functions are a fundamental building block in JavaScript, allowing you to encapsulate code into reusable blocks. They help improve code organization, readability, and maintainability. In this section, we'll explore various aspects of functions in JavaScript, including their syntax, types, scope, and best practices.

Function Declaration

A function declaration is one of the most common ways to define a function in JavaScript. It consists of the function keyword followed by the function name, parentheses, and curly braces containing the function body.

// Function Declaration
function greet(name) {
  return `Hello, ${name}!`;
}

console.log(greet('Alice')); // Output: Hello, Alice!

Characteristics

  • Hoisting: Function declarations are hoisted to the top of their scope. This means you can call a function before it is defined in your code.
console.log(sayHello()); // Output: Hello!

function sayHello() {
  return 'Hello!';
}

Function Expression

A function expression involves defining a function inside an expression, which can be assigned to a variable or passed as an argument to another function. This method is not hoisted.

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

console.log(add(5, 3)); // Output: 8

Characteristics

  • Not Hoisted: Unlike declarations, expressions are not hoisted. You cannot call the function before its definition.
console.log(sum(2, 4)); // Error: sum is not defined

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

Arrow Functions

Arrow functions provide a more concise syntax for writing functions and have some differences in behavior compared to traditional function expressions. They were introduced in ECMAScript 6 (ES6).

// Arrow Function
const multiply = (a, b) => {
  return a * b;
};

console.log(multiply(3, 4)); // Output: 12

Characteristics

  • Shorter Syntax: For single-line functions, you can omit the curly braces and return keyword.
const subtract = (a, b) => a - b;

console.log(subtract(10, 5)); // Output: 5
  • No this Binding: Arrow functions do not have their own this context. They inherit the this value from the parent scope.
function Person() {
  this.age = 0;

  setInterval(() => {
    this.age++; // `this` refers to the Person object
  }, 1000);
}

const p = new Person();

Function Parameters

Functions can accept parameters, which are values passed into the function. JavaScript supports default parameter values and rest parameters.

Default Parameters

Default parameters allow you to specify default values for function parameters if no argument is provided or if it is undefined.

function greet(name = 'Guest') {
  return `Hello, ${name}!`;
}

console.log(greet()); // Output: Hello, Guest!
console.log(greet('Bob')); // Output: Hello, Bob!

Rest Parameters

Rest parameters allow you to represent an indefinite number of arguments as an array.

function sum(...numbers) {
  return numbers.reduce((acc, num) => acc + num, 0);
}

console.log(sum(1, 2, 3)); // Output: 6
console.log(sum(5, 10, 15, 20)); // Output: 50

Function Scope

Understanding function scope is crucial for managing variable lifetimes and avoiding bugs.

Local Scope

Variables declared inside a function are local to that function and cannot be accessed outside of it.

function example() {
  const localVar = 'I am local';
  console.log(localVar); // Output: I am local
}

example();
// console.log(localVar); // Error: localVar is not defined

Global Scope

Variables declared outside any function are global and can be accessed from anywhere in the code.

const globalVar = 'I am global';

function showGlobal() {
  console.log(globalVar); // Output: I am global
}

showGlobal();
console.log(globalVar); // Output: I am global

Closures

A closure is a function that has access to its own scope, the outer function's scope, and the global scope. It allows functions to "remember" their lexical environment.

function createCounter() {
  let count = 0;
  
  return function() {
    count++;
    return count;
  };
}

const counter = createCounter();
console.log(counter()); // Output: 1
console.log(counter()); // Output: 2

Characteristics

  • Preserves State: Closures can be used to create private variables and encapsulate functionality.
function secretCode() {
  const code = '1234';
  
  return function(guess) {
    if (guess === code) {
      return 'Access granted!';
    } else {
      return 'Access denied.';
    }
  };
}

const checkCode = secretCode();
console.log(checkCode('5678')); // Output: Access denied.
console.log(checkCode('1234')); // Output: Access granted!

Higher-Order Functions

Higher-order functions are functions that take other functions as arguments or return them. They enable functional programming paradigms like map, filter, and reduce.

map Method

The map method creates a new array by applying a function to each element of the original array.

const numbers = [1, 2, 3, 4];
const doubled = numbers.map(num => num * 2);

console.log(doubled); // Output: [2, 4, 6, 8]

filter Method

The filter method creates a new array with all elements that pass the test implemented by the provided function.

const evenNumbers = numbers.filter(num => num % 2 === 0);

console.log(evenNumbers); // Output: [2, 4]

reduce Method

The reduce method executes a reducer function on each element of the array, resulting in a single output value.

const total = numbers.reduce((acc, num) => acc + num, 0);

console.log(total); // Output: 10

Best Practices

  • Use Descriptive Names: Choose meaningful names for functions to improve code readability.

  • Keep Functions Short and Focused: Each function should perform a single task. This makes your code easier to understand and test.

  • Avoid Side Effects: Try to make functions pure, meaning they do not modify external state or have side effects like modifying global variables or DOM elements.

  • Use Arrow Functions for Callbacks: They provide a more concise syntax and lexical this binding.

  • Document Your Functions: Use comments or JSDoc to describe what the function does, its parameters, and return value.

/**
 * Calculates the sum of two numbers.
 * @param {number} a - The first number.
 * @param {number} b - The second number.
 * @returns {number} The sum of the two numbers.
 */
function add(a, b) {
  return a + b;
}

Conclusion

Functions are a powerful feature in JavaScript that enable code reusability and modularity. By understanding different types of functions, their scope, closures, and higher-order functions, you can write more efficient and maintainable code. Always strive to follow best practices to ensure your functions are clean, readable, and effective.

Additional Resources

  • MDN Web Docs - Functions
  • JavaScript.info - Functions
  • Eloquent JavaScript - Functions

By mastering functions in JavaScript, you'll be well-equipped to tackle a wide range of programming challenges and build robust applications.


PreviousJavaScript break and continueNext JavaScript Function Expressions

Recommended Gear

JavaScript break and continueJavaScript Function Expressions