function f(){ }();
The JavaScript parser interprets the function f(){ }(); as function f(){ } and (); where the former is a function declaration while the latter (a pair of brackets) is an attempt to execute a function without specifying a name, resulting in Uncaught SyntaxError: Unexpected token ).
(function f(){ })() and (function f(){ }()) are two ways to fix it that involve adding more brackets. These functions are not available in the global scope, and you can even omit their names if you don't need to refer to them within the body.
You can also use the void operator: void function f(){ }(); .However, there is a drawback in this strategy. Because the evaluation of a given expression is always undefined, you can't use your IIFE function if it returns anything. Consider the following scenario:
// Don't add JS syntax to this code block to prevent Prettier from formatting it.
const f = void
function b() {
return 'f';
}();
console.log(f); // undefined
Posted Date:- 2021-10-08 09:20:55
How do you create an array in Javascript?
What do you understand by Callback and Callback hell in JavaScript?
Define call back? How do they work?
What do you understand by Weakmap?
What is the Prototype Design Pattern?
What are the default parameters?
What is the reason behind adding Symbol to ES6?
What is the difference between for..of and for..in?
What is Bubbling and Capturing?
What are the states of Promises in ES6?
What do you understand by Weakset?
How can you create a class in ES6?
What are the default parameters?
Why should one use ES6 classes?
Discuss the template literals in ES6.
What are the benefits of using Web pack?
Explain the Rest parameter in ES6.
Why are functions are called as first class objects?
What do you understand by the Generator function?
What are the template literals in ES6?
Discuss the spread operator in ES6 with an example.
Explain briefly about classes, modules, and proxies?
What is a Destructuring assignment, and explain in brief?
Explain about Internationalization and localization?
Explain about Default parameter values, Rest parameter, Spread operator?
Explain about Webpack and the benefits of using Webpack?
Explain briefly about Arrow functions?
What are Block Scoped variables and functions?
What is Functional Programming?
How to define a function in es6?
What is the difference between es5 and es6?
What is Event propagation, capturing, bubbling?
Which keywords can be used to implement inheritance in es6?
What is the arrow function in es6?