


Khusboo Tayal
Functions are one of the core concepts of JavaScript. They allow you to group reusable blocks of code, making your programs more efficient, modular, and readable. In this tutorial, we will cover different ways of creating and using functions in JavaScript.
A function is a reusable block of code designed to perform a specific task. You can think of it as a machine that takes some input (parameters), processes it, and returns an output.
JavaScript supports multiple ways to create functions:
This is the most common and traditional way to create a function.
function functionName(parameters) {
// Code to be executed
return value;
}
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Alice")); // Output: Hello, Alice!In this method, a function is assigned to a variable. These functions can be anonymous (no name).
const functionName = function(parameters) {
// Code to be executed
return value;
};const multiply = function(a, b) {
return a * b;
};
console.log(multiply(3, 4)); // Output: 12Introduced in ES6 (ECMAScript 2015), arrow functions are a more concise way to write functions.
const functionName = (parameters) => {
// Code to be executed
return value;
};const add = (a, b) => a + b; console.log(add(5, 7)); // Output: 12
const square = num => num * num; console.log(square(4)); // Output: 16
| Feature | Function Declaration | Function Expression | Arrow Function |
|---|---|---|---|
| Syntax | Traditional | Assigned to a variable | Concise syntax (=>) |
| Hoisting | Yes (Can be used before declaration) | No (Not hoisted) | No (Not hoisted) |
| this Binding | Dynamic (depends on caller) | Dynamic (depends on caller) | Lexical (fixed to context) |
| Use Case | General-purpose functions | Dynamic, conditional use | Short, inline functions |
Hoisting is a JavaScript behavior where variable and function declarations are moved to the top of their scope (either global or local) during the compile phase. This means that you can use functions and variables before they are declared in the code.
Functions can take parameters (placeholders) that you provide when calling the function. These values are called arguments.
function calculateArea(length, width) {
return length * width;
}
console.log(calculateArea(5, 10)); // Output: 50You can set default values for parameters.
function greet(name = "Guest") {
return "Hello, " + name + "!";
}
console.log(greet()); // Output: Hello, Guest!
console.log(greet("Alice")); // Output: Hello, Alice!Rest parameters allow you to accept multiple arguments as an array.
function sum(...numbers) {
return numbers.reduce((acc, num) => acc + num, 0);
}
console.log(sum(1, 2, 3, 4, 5)); // Output: 15
Functions can return a value using the return keyword. If no return is specified, the function returns undefined.
function getFullName(firstName, lastName) {
return firstName + " " + lastName;
}
let fullName = getFullName("John", "Doe");
console.log(fullName); // Output: John Doeconst person = {
name: "Alice",
sayHello: function() {
console.log("Hello, " + this.name);
},
sayHelloArrow: () => {
console.log("Hello, " + this.name);
}
};
person.sayHello(); // Output: Hello, Alice
person.sayHelloArrow(); // Output: Hello, undefined (due to lexical this)An IIFE is a function that runs as soon as it is defined.
(function() {
console.log("This is an IIFE");
})();