3. Declaring and Calling Functions
A function declaration defines a named function that can be invoked later in the code. However, in JavaScript, there are several ways to declare a function in JavaScript:
To call a function, use its name followed by parentheses enclosing any arguments.
3.1 - Named Function
A named function is defined using the function
keyword, followed by the function name, parameters in parentheses, and the function body within curly braces. This function can be called later in the code using its name.
//Declaring function
function greet() {
console.log("Hello");
}
//calling function
greet();
In this example, the function
keyword is used to declare a function. greet
is the name of the function. name
is a parameter, which acts as a placeholder for the value passed to the function when it is called. The function body contains the code to be executed when the function is called.
3.2 - Anonymous Function
An anonymous function does not have a name and is typically assigned to a variable. The function can be called using the variable name.
//Declaring function
const greet = function() {
console.log("Hello");
};
//Calling function
greet();
In this example, the function is anonymous (it doesn't have a name) and is assigned to the variable greet. The function can then be called using the variable name greet.
3.3 - Arrow Function
Arrow functions provide a shorter syntax for writing function expressions and are particularly useful for writing concise code in callbacks and array operations.
//Declaring function
const greet = () => {
console.log("Hello");
};
//Calling function
greet();
3.4 - Function Expression
A function expression is a way to define a function in JavaScript by assigning it to a variable. It can be named or anonymous.
Function Expression (Named)
A named function expression includes a function name, which can be useful for recursion or debugging purposes.
//Declaring function
const greet = function sayHello() {
console.log("Hello");
};
//Calling function
greet();
Note that: In the above function expression, the function name sayHello
is not used when calling the function. The function name is accessible within the function body, making it useful for identifying the function in stack traces during debugging.
Named function expressions can be particularly useful in two main scenarios: recursion and debugging. For recursion, named function expressions are advantageous because the function can refer to itself within its own body, allowing for repeated or self-referential tasks. In terms of debugging, named functions offer the benefit of more informative stack traces. The presence of a function name can help identify the source of errors more easily during the debugging process.
Function Expression (Anonymous)
An anonymous function expression does not include a name, making it simpler and less prone to confusion for straightforward use cases.
//Declaring function
const greet = function () {
console.log("Hello");
};
//Calling function
greet();
Anonymous function expressions are frequently utilized in scenarios where a name is not necessary, such as simple assignments. They are ideal for straightforward tasks where adding a name would not provide any additional benefit. Additionally, anonymous function expressions are commonly used as callbacks, making them a popular choice for event handlers or array methods. Their simplicity and directness make them well-suited for these purposes.
3.5 - Immediately Invoked Function Expression (IIFE)
An IIFE is a function that is defined and executed immediately. This pattern is often used to create a local scope and avoid polluting the global scope.
(function() {
console.log('This is an IIFE');
})();
An IEFE is designed to be called immediately and cannot be called again directly because it does not have a name. However, we can call a similar function by wrapping it in another function or by using a named function or variable. Here are a couple of examples:
const callIIFE = () => {
(function() {
console.log('This is an IIFE');
})();
};
// Call the function
callIIFE();
callIIFE();
3.6 - Constructor Function
Constructor functions are used to create objects and are called with the new
keyword. These functions can define properties and methods for the objects they create.
function Person(name, age) {
this.name = name;
this.age = age;
}
const person1 = new Person('Alice', 30);