InterviewSolution
| 1. |
b let i = ? console.log(i * i); //Gives 0 console.log(i + 1); //Gives 1 console.log(i - 1); //Gives -1 console.log(i / i); //Gives 1 |
|
Answer» IIFE MEANS Immediately Invoked Function Expression. Let’s first see a normal way to create function in JavaScript, also known as function as “Function DECLARATION”. This is most SIMILAR, to the syntax of declaring functions in most other traditional languages like C++ and Java. Consider the below example which have a simple function to add two numbers. Notice that these function definitions always start with the function keyword. You can’t omit it as it’s invalid syntax. In JavaScript functions are first CLASS citizens. So, there is another way to DECLARE them. It is known as “Function Expression”. We will refactor the above addNumbers() code. Here we are treating as if addNumbers is a variable declaration. But then assigning an anonymous function to it. IIFE(Immediately Invoked Function Expression) is nothing but a function expression, but it is defined and invoked at the same time. Here we had got rid of the addNumbers name itself and calling it also at the same time. Notice, that we are passing 2,3 immediately after the anonymous function is declared. As JavaScript for most part is function scoped, so IIFEs are great way to create private variables. |
|