DEV Community

Mitesh Kamat
Mitesh Kamat

Posted on

Skip the IIFE brackets

Introduction

This article is about how you can skip the brackets while creating an IIFE (Immediately Invoked Function Expression).

As we know , this is how we call IIFE

(function(){
   console.log('calling iife')
})()
//Output:  calling iife

It looks like those extra brackets tell the JavaScript parser, that the upcoming code is a Function Expression and not a Function. So, we can skip those extra brackets & still make a valid IIFE.

Let's give it a try:

void function(){
   console.log('calling iife')
}()
//Output: calling iife
//        undefined

+ function(){
   console.log('calling iife')
}()
//Output: calling iife
//        NaN

- function(){
   console.log('calling iife')
}()
//Output: calling iife
//        NaN

! function(){
   console.log('calling iife')
}()
//Output: calling iife
//        true

As you can see , I have skipped the brackets and added operators to tell the parser that it is a function expression.

Cheers !!!

Discussion (0)