Why are parenthesis used to wrap a JavaScript function call?



In JavaScript, the functions wrapped with parenthesis are called β€œImmediately Invoked Function Expressions" or "Self Executing Functions.

The purpose of wrapping is to namespace and control the visibility of member functions. It wraps code inside a function scope and decrease clashing with other libraries. This is what we call Immediately Invoked Function Expression (IIFE) or Self Executing Anonymous Function.

Here’s the syntax βˆ’

(function() {
   // code
})();

As you can see above, the following pair of parentheses converts the code inside the parentheses into an expression βˆ’

function(){...}

In addition, the next pair, i.e. the second pair of parentheses continues the operation. It calls the function, which resulted from the expression above.

Updated on: 2020-06-16T11:12:24+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements