Home >Web Front-end >JS Tutorial >Comprehensive Guide to Functions in JavaScript
Here’s a comprehensive guide to Functions in JavaScript with examples:
A function is a block of reusable code designed to perform a particular task. It is executed when it is invoked or called.
function functionName(parameters) { // Code to execute }
function greet(name) { console.log(`Hello, ${name}!`); } greet("Alice"); // Output: Hello, Alice!
A function declared using the function keyword.
function add(a, b) { return a + b; } console.log(add(2, 3)); // Output: 5
Functions can also be stored in variables.
const multiply = function (a, b) { return a * b; }; console.log(multiply(2, 3)); // Output: 6
A concise syntax for writing functions.
const functionName = (parameters) => { // Code to execute };
const subtract = (a, b) => a - b; console.log(subtract(5, 3)); // Output: 2
Functions without a name, often used as callbacks.
setTimeout(function () { console.log("This runs after 2 seconds"); }, 2000);
A function that runs immediately after it is defined.
(function () { console.log("IIFE is executed immediately!"); })();
function greet(name, age) { console.log(`Hi ${name}, you are ${age} years old.`); } greet("Bob", 25); // Output: Hi Bob, you are 25 years old.
Provide default values for parameters if no argument is passed.
function sayHello(name = "Guest") { console.log(`Hello, ${name}!`); } sayHello(); // Output: Hello, Guest!
Used to handle an indefinite number of arguments as an array.
function sum(...numbers) { return numbers.reduce((total, num) => total + num, 0); } console.log(sum(1, 2, 3, 4)); // Output: 10
Functions can return a value using the return statement.
function square(num) { return num * num; } console.log(square(4)); // Output: 16
A function passed as an argument to another function and executed later.
function processUserInput(callback) { const name = "Charlie"; callback(name); } processUserInput((name) => { console.log(`Hello, ${name}!`); }); // Output: Hello, Charlie!
Functions that accept other functions as arguments or return functions.
function functionName(parameters) { // Code to execute }
A closure is a function that remembers its outer variables even after the outer function has finished executing.
function greet(name) { console.log(`Hello, ${name}!`); } greet("Alice"); // Output: Hello, Alice!
Functions have their own local scope.
function add(a, b) { return a + b; } console.log(add(2, 3)); // Output: 5
A function that calls itself.
const multiply = function (a, b) { return a * b; }; console.log(multiply(2, 3)); // Output: 6
A pure function produces the same output for the same input and has no side effects.
const functionName = (parameters) => { // Code to execute };
Hi, I'm Abhay Singh Kathayat!
I am a full-stack developer with expertise in both front-end and back-end technologies. I work with a variety of programming languages and frameworks to build efficient, scalable, and user-friendly applications.
Feel free to reach out to me at my business email: kaashshorts28@gmail.com.
The above is the detailed content of Comprehensive Guide to Functions in JavaScript. For more information, please follow other related articles on the PHP Chinese website!