Skip the IIFE brackets
Mitesh Kamat

Mitesh Kamat @miteshkamat27

About: Front End Engineer | Big fan of JavaScript

Location:
India
Joined:
Jan 22, 2020

Skip the IIFE brackets

Publish Date: Feb 27 '20
7 0

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 !!!

Comments 0 total

    Add comment