Functions in JavaScript are blocks of reusable code that can be defined, called by name, and executed on-demand. They can accept input parameters, perform specific tasks, and optionally return a value. Functions are first-class objects in JavaScript, meaning they can be assigned to variables, passed as arguments, and returned as values. They enable modular, maintainable, and DRY (Don't Repeat Yourself) programming.
function keyword, followed by the function name, a list of parameters in parentheses, and a code block in curly braces.javascriptfunction add(a, b) {
return a + b;
}
javascriptlet add = function addNumbers(a, b) {
return a + b;
};
javascriptlet add = function(a, b) {
return a + b;
};
this keyword.javascriptlet greet = () => {
console.log("Hello, World!");
};
javascriptlet square = x => {
return x * x;
};
javascriptlet add = (a, b) => {
return a + b;
};
javascriptlet sum = add(10, 20); // 30
javascriptfunction multiply(a, b = 1) {
return a * b;
}
let result = multiply(5); // 5, since b has a default value of 1
return keyword, followed by the value or expression to be returned. If a function doesn't have a return statement, it returns undefined by default.javascriptfunction greet() {
return "Hello, World!";
}
let greeting = greet(); // "Hello, World!"
javascriptlet globalVar = "I am global!";
function checkScope() {
let localVar = "I am local!";
console.log(globalVar); // "I am global!"
console.log(localVar); // "I am local!"
}
checkScope();
console.log(globalVar); // "I am global!"
console.log(localVar); // ReferenceError: localVar is not defined
javascriptfunction outer() {
let count = 0;
return function inner() {
count++;
return count;
};
}
let counter = outer();
console.log(counter()); // 1
console.log(counter()); // 2
console.log(counter()); // 3
In this example, the inner function has access to the count variable from the outer function, even after the outer function has completed execution. Each time the counter function is called, it increments the count variable and returns the updated value.
javascriptfunction greet(name, formatFunction) {
let formattedName = formatFunction(name);
console.log(`Hello, ${formattedName}!`);
}
function uppercase(name) {
return name.toUpperCase();
}
function lowercase(name) {
return name.toLowerCase();
}
greet("Alice", uppercase); // "Hello, ALICE!"
greet("Bob", lowercase); // "Hello, bob!"
javascriptfunction createMultiplier(factor) {
return function (number) {
return number * factor;
};
}
let double = createMultiplier(2);
let triple = createMultiplier(3);
console.log(double(5)); // 10
console.log(triple(5)); // 15
javascriptfunction factorial(n) {
if (n === 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
console.log(factorial(5)); // 120
In this example, the factorial function calls itself with progressively smaller values of n until it reaches the base case (n === 0), at which point it starts returning values that are then multiplied together to produce the final result.
Understanding functions in JavaScript is essential for writing clean, maintainable, and modular code. By mastering function declaration, expressions, arrow functions, scope, closures, higher-order functions, and recursion, you can create efficient and reusable code for a wide range of tasks and applications.