Miscellaneous Questions - Quick JavaScript Interview Questions (2015)

Quick JavaScript Interview Questions (2015)

Chapter 17. Miscellaneous Questions

Q. What is strict mode?
ANSWER
Strict mode is a new directive in new ECMA 5 JavaScript specification. It is used for secure JavaScript coding. It eliminates some JavaScript silent errors by changing them to throw errors.

The syntax of writing strict mode is below expression.
“use strict”
Below code shows the use of strict mode inside a function.

function sayHello(){
"use strict";
myName = "Sandeep";

}
sayHello();
The following screenshot shows the error produces by using an undeclared variable inside the sayHello() method.


Q. List out some of the conditions that are not allowed in strict mode?
ANSWER
Find the list of the conditions that are not allowed in ECMA5 strict mode in below:

Using a variable without declaring is not allowed. Deleting a variable, a function, or an argument is not allowed.

Defining a property more than once is not allowed. Duplicating a parameter name is not allowed.

Octal numeric literals and escape characters are not allowed.
Writing to a read-only property is not allowed. Deleting an undeletable property is not allowed.

The string "eval" cannot be used as a variable. The string "arguments" cannot be used as a variable. The with statement is not allowed.
Future reserved keywords are not allowed.

Q. What is the output of 0.1+0.2 produces in the console and why? ANSWER
JavaScript math library follows IEEE 754 standards for math. IEEE 754 standards use 64 bit representation for a floating point number. This causes a problem while evaluating the 0.1 + 0.2 expression. Below screenshot shows the Firebug console for this expression.

JavaScript internally converts the 0.1 to 16 precision which becomes 0.1000000000000000 and then 0.2 gets added and becomes 0.30000000000000004. Below screenshot shows this
demonstration in JavaScript code.

Q. How to resolve the issue 0.1+0.2 = 0.30000000000000004 and produce 0.1+0.2 = 03?
ANSWER
This issue can be resolved by using to toFixed(1) method to this expression. toFixed() method converts a number to the specified decimal points. Below screenshot shows the use of toFixed() method to produce the correct output which is 0.3.

Q. What will be the output of the below code and why?

(function(){
var a = b = 3;
})();
console.log(typeof a);
console.log(typeof b);
ANSWER
The above code will print undefined and Number in the console. Below screenshot shows the output of the above code in the Firebug console.

JavaScript treats the above code as below screenshot. From the below code it is clear that variable a is in local scope of the function and be is treated as this.b . The current reference this represents the window object.

Q. What will be the output of the below code and why? console.log(1+2+4);
ANSWER
The output of the above code is 7 as the parameters are all
numbers. Below screenshot shows the output of the above code in
a chrome console.

Q. Explain Hoisting in JavaScript?
ANSWER
Hoisting is JavaScript's default behavior of moving declarations to the top. In other words, a variable can be used before it has been declared. Let’s understand hoisting using these following examples.

Example 1:
The following code has a display() method which prints the value of a in the console.

function display(){ console.log(a);
}
display();

The output of the preceding code will be a reference error as we have not defined the variable. The following screenshot shows the output of the preceding code.

Example 2:
The following code has a display() method which prints the value of a in the console.

function display(){
var a;
console.log("Output: "+a);

}
display();

The output of the preceding code will be undefined as we have defined the variable but not assigned any value. The following screenshot shows the output of the preceding code.

Example 3:
The following code has a display() method which prints the value of a in the console.

function display(){
console.log("Output: "+a);
var a;

}
display();

The output of the preceding code will be undefined as we have defined the variable but not assigned any value. Example 2 and Example 3 has same output. It means the variable declaration is moved to the top. The following screenshot shows the output of the preceding code.