Welcome to part-16 of the series and the second part in Tricky questions.

Question 109- What is the output of the below code and why?

(function(x) {
    return (function(y) {
        console.log(x);
    })(2)
})(1);

Answer-The output logged will be 1 and that will be because of closure. Here we have an outer IIFE and an inner IIFE. First the outer IIFE will run and will set x=1. Now when the inner IIFE will run, it will remember the value of “x” because of closure and console log will dispaly 1.

Question 110- What is the output of the below code and why?

for (let i = 0; i < 5; i++) {
  setTimeout(function() { console.log(i); }, i * 1000 );
}

Answer-The output logged will be:

0
1
2
3
4

It will output the above because we use let instead of var here. The variable i is only seen in the for loop’s block scope.

Question 111- What is the output of the below code and why?

var a = [1, 2, 3];
a[10] = 99;
console.log(a[6]);

Answer-The output logged will be: undefined

When we give the statement a[10] = 99; the JavaScript engine will make array slots 3 through 9 be “empty slots.”

So, a[6] will give undefined

Question 112- What is the output of the below code and why?

console.log(typeof undefined == typeof NULL);

Answer-The output logged will be: true

The reason is that NULL is different from primitive data type null. The variable NULL is just any other undefined variable. If we console log these three, we can see that typeof null and typeof NULL are different.

console.log(typeof undefined); **//undefined**
console.log(typeof null);  **//object**
console.log(typeof NULL);  **//undefined**

Question 113- What is the output of the below code and why?

console.log(typeof typeof 1);

Answer-The output logged will be: string

The reason is that typeof 1 will return "number" and typeof "number" will return string.

Question 114- What is the output of the below code and why?

var b = 1;
function outer(){
    var b = 2
    function inner(){
        b++;
        var b = 3;
        console.log(b)
    }
    inner();
}
outer();

Answer-The output logged will be: 3

There are three closures in the example, each with it’s own var b declaration. When a variable is invoked closures will be checked in order from local to global until an instance is found. Since the inner closure has a b variable of its own, that is what will be output.

Question 115- What is the output of the below code and why?

console.log([] + []);

Answer-The output logged will be: “ “ ie two empty spaces.

The reason is the plus(+) operator works on numbers and strings. So, when we give two empty arrays, it will typecast them to empty string and then print them.

Question 116- What is the output of the below code and why?

function a(greet) {
    return greet;
}

const sentence = a 

hi ;

console.log(sentence);

Answer-The output logged will be: Array ["hi"]

The reason is the the line const sentence = a hi ; also passes an argument to the function and it is know as tagged template. It is quite similar to the line const sentence = a( hi );

Question 117- How can you prevent user from adding new properties or modifying a property in an Object?Answer-We can prevent any user from adding a new property or modifying existing property in an Object by using the Object’s method freeze(). Below is the usage.

let profile = {
  name: "Nabendu"
};

Object.freeze(profile);

profile.age = 30;
profile.name = "Shikha";
console.log(profile); //**Object { name: "Nabendu" }**

Question 118- How can you prevent user from adding new properties but can modify an existing property in an Object?Answer-We can prevent any user from adding a new property but allow to modify existing property in an Object by using the Object’s method seal(). Below is the usage.

let profile = {
  name: "Nabendu"
};

Object.seal(profile);

profile.age = 30;
profile.name = "Shikha";

console.log(profile); //**Object { name: "Shikha" }**

Question 119- What is the output of the below code and why?

let x = [1, 2, 3] + [4, 5, 6];

console.log(x);

Answer-The output will be 1,2,34,5,6

It will be so, because the plus(+) operator works only on Numbers and Strings. So, in above question it will try to convert the arrays first. Since, they cannot be converted into Number, so they will be converted into a Strings like "1,2,3" + "4,5,6". And this string concatenation will produce "1,2,34,5,6"

Question 120- What is the output of the below code and why?

console.log(888888888888888888);

Answer-The output will be 888888888888888800

The reason is that in JavaScript we cannot have a number more then 16 digit. The question have 8 for 18 times. The largest number available in JavaScript is Number.MAXSAFEINTEGER. If we console log it, we will find that it have 16 digit.

console.log(Number.MAX_SAFE_INTEGER); **//9007199254740991**

Question 121- What is the differene between async and defer attributes in script tag?Answer-In JavaScript when the parser reaches the <script> tag, it stops to fetch it(if external file) and run it.

This can be a problem, because it generally stops our html code been rendered to the DOM and visible to the user. One way to solve this issue is to put the <script> tag just before the body tage ends ie </body> . But in some cases we might require some features from our javascript to be available to the page.

We will look at the three type of execution here — Normal, async and defer.

Normal Execution Take the example of