How to Init Functions in JavaScript
Functions are one of the most fundamental elements of JavaScript. The JavaScript function is very similar to that of a procedure.
A set of instructions/statements performs a task or calculates a value. For a procedure to qualify as a function, it must require input and return an output when tested.
To use a function, you must define it within the scope you want to call it.
In today’s article, we will learn about init functions in JavaScript.
the Init Functions in JavaScript
When functions are used only once, an Immediately Invoked Function Expression (IIFE) is common.
(function() {
/* Set of instructions */
})();
(() => {
/* Set of instructions */
})();
IIFEs are function expressions called as soon as the function is declared. Function declarations that do not begin with function are function expressions.
A normal function definition is also known as a function statement or function declaration. This function declaration consists of the keyword function
followed by:
- The unique name of the function.
- All the parameters required for the function are enclosed in parentheses and separated by commas.
- The JavaScript declarations define the function. It is enclosed in square brackets,
{...}
.
One of the advantages of creating a named function expression is that if we encounter an error, the stack trace will include the function name, making it easier to find the source of the error.
IIFEs is a design pattern, also known as a self-executing anonymous function, with two main parts.
- The first is the lexically delimited anonymous function, which is contained in the grouping operator
()
. This prevents access to variables within the IIFE language and pollutes the world scope. - The second part creates the function that is immediately invoked function expression
()
, which allows the JavaScript engine to interpret the function directly.
const additionFunction = function() {
console.log(2 + 3)
};
additionFunction();
(function() {
console.log(2 + 3)
})();
Because our application might contain many functions and global variables from different source files, it is important limiting the number of global variables.
Let’s take an example of the above code; we have declared two functions. Both the functions perform similar task and prints the result 5.
If we have startup code that we don’t need to reuse, we can use the IIFE pattern.
The code will not be reused. In this case, using IIFE is better than using a function declaration or expression.
When you run the above code in any browser, it will print something like this.
Output:
"5"
"5"
Shraddha is a JavaScript nerd that utilises it for everything from experimenting to assisting individuals and businesses with day-to-day operations and business growth. She is a writer, chef, and computer programmer. As a senior MEAN/MERN stack developer and project manager with more than 4 years of experience in this sector, she now handles multiple projects. She has been producing technical writing for at least a year and a half. She enjoys coming up with fresh, innovative ideas.
LinkedIn