Immediately Invoked Function Expression with Arguments in JavaScript

In JavaScript, an Immediately Invoked Function Expression (IIFE) is a way to create a function that is executed as soon as it is defined. It is commonly used to enclose code and create a private scope.

By default, an IIFE doesn’t take any arguments. However, there are times when you may need to pass arguments to the IIFE. Here’s how you can do it:

(function(arg1, arg2, arg3) {
  // code to be executed
})(value1, value2, value3);

In the above example, we define an anonymous function and immediately invoke it. The arguments arg1, arg2, and arg3 inside the function can be accessed and used just like any regular function arguments. The values value1, value2, and value3 are passed as arguments when invoking the function.

Here’s a practical example:

(function(name) {
  console.log("Hello, " + name + "!");
})("John");

In the example above, we define an IIFE that accepts a name argument. The IIFE is immediately invoked with the argument "John". The output will be “Hello, John!”.

Using an IIFE with arguments can be useful when you want to create a modular piece of code that can accept custom values without polluting the global scope. It allows you to encapsulate your code and safely pass parameters.

Remember to use meaningful variable names for the arguments and provide descriptive comments to make your code more readable and maintainable.

#JavaScript #IIFE