In JavaScript, functions are fundamental building blocks that allow you to organize code, make it reusable, and perform specific tasks. A function is a block of code designed to perform a particular task when called upon. JavaScript also offers function expressions, which allow you to assign functions to variables.
A JavaScript function is a block of reusable code that performs a specific task. Functions allow you to write code that can be invoked multiple times, making your programs more organized and efficient. A function is defined using the function
keyword followed by a name, parameters (optional), and a block of code.
function functionName(parameter1, parameter2) {
// Code to be executed
console.log(parameter1, parameter2);
}
functionName
: The name of the function.parameter1
, parameter2
: Parameters are optional and represent values passed to the function when called.{}
is executed when the function is invoked.Let’s take a look at a simple function that calculates the sum of two numbers.
function add(a, b) {
return a + b;
}
let result = add(5, 3);
console.log(result); // Output: 8
Explanation:
add
takes two parameters (a
and b
) and returns their sum.add(5, 3)
, it returns 8
, which is then logged to the console.A function expression is a function that is defined inside an expression, typically assigned to a variable. Unlike function declarations, function expressions are not hoisted, meaning the function cannot be called before it is defined.
const functionName = function(parameter1, parameter2) {
// Code to be executed
console.log(parameter1, parameter2);
};
functionName
), which can then be used to invoke the function.Here’s an example where we use a function expression to create a function that multiplies two numbers.
const multiply = function(x, y) {
return x * y;
};
let result = multiply(4, 5);
console.log(result); // Output: 20
Explanation:
multiply
. It takes two parameters (x
and y
) and returns their product.multiply(4, 5)
, it returns 20
, which is logged to the console.Both function declarations and function expressions are used to define functions, but there are important differences between them:
function
keyword followed by a name.
function greet() {
console.log("Hello!");
}
const greet = function() {
console.log("Hello!");
};
Feature | Function Declaration | Function Expression |
---|---|---|
Hoisting | Hoisted and can be called before defined | Not hoisted, must be defined before called |
Syntax | function functionName() |
const functionName = function() |
Name | Must have a name | Can be anonymous or named |
Use | Typically used for general-purpose functions | Often used for callback functions, event handlers |
A function expression can be anonymous, meaning the function does not have a name. Anonymous functions are commonly used for one-time tasks or passed as arguments to other functions.
setTimeout(function() {
console.log("This is an anonymous function!");
}, 1000);
Explanation:
setTimeout
method. After 1 second, the anonymous function is executed, and the message is logged to the console.JavaScript also provides a shorthand syntax for writing function expressions called arrow functions. Arrow functions are more concise and do not have their own this
context.
const functionName = (parameter1, parameter2) => {
// Code to be executed
return parameter1 + parameter2;
};
const add = (a, b) => a + b;
let result = add(3, 4);
console.log(result); // Output: 7
Explanation:
add
takes two parameters and returns their sum. The =>
syntax makes the function more concise.An IIFE is a function that is defined and executed immediately after its creation. This is commonly used to create a private scope for variables, preventing them from polluting the global scope.
(function() {
// Code to be executed immediately
console.log("I am an IIFE!");
})();
(function() {
let message = "Hello from the IIFE!";
console.log(message);
})();
Explanation:
message
variable is not accessible outside the IIFE, creating a local scope.