JS Coding Questions Logo
JS Coding Questions
#424💼 Interview💻 Code

How to invoke an IIFE without any extra brackets?

Advertisement

728x90

Immediately Invoked Function Expressions(IIFE) requires a pair of parenthesis to wrap the function which contains set of statements.

js
1(function (dt) {
2
3console.log(dt.toLocaleTimeString());
4})(new Date());

Since both IIFE and void operator discard the result of an expression, you can avoid the extra brackets using void operator for IIFE as below,

js
1void (function (dt) {
2
3console.log(dt.toLocaleTimeString());
4})(new Date());

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 81

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
424of476