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

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)