DEV Community

Mitesh Kamat
Mitesh Kamat

Posted on

2

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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay