Javascript Interview Questions and Answers – Part14

131. What will be the output of the following statements?
var myString = ‘Vikas’ // Create a primitive string object.
var myStringCopy = myString; // Copy its value into a new variable.
var myString = null; // Manipulate the value
console.log(myString, myStringCopy);
Ans: // Logs ‘null Vikas’

132. Consider the following statements and tell what would be the output of the logs statements?
var price1 = 10;
var price2 = 10;
var price3 = new Number(’10’); // A complex numeric object because new was used.
console.log(price1 === price2);
console.log(price1 === price3);
Ans:
console.log(price1 === price2); // Logs true.
console.log(price1 === price3); /* Logs false because price3
contains a complex number object and price 1
is a primitive value. */

133. What would be the output of the following statements?
var object1 = { same: ‘same’ };
var object2 = { same: ‘same’ };
console.log(object1 === object2);
Ans: // Logs false, JavaScript does not care that they are identical and of the same object type.
When comparing complex objects, they are equal only when they reference the same object (i.e., have the same address). Two variables containing identical objects are not equal to each other since they do not actually point at the same object.

134. What would be the output of the following statements?
var object1 = { same: ‘same’ };
var object2 = object1;
console.log(object1 === object2);
Ans: // Logs true

135. What is this?
var myArray = [[[]]];
Ans: Three dimensional array

136. Name any two JavaScript functions which are used to convert nonnumeric values into numbers?
Number()
parseInt()
parseFloat()

var n1 = Number(“Hello world!”); //NaN
var n2 = Number(“”); //0
var n3 = Number(“000010”); //10
var n4 = Number(true); //1
var n5 = Number(NaN); //NaN

137. Does JavaScript Support automatic type conversion, If yes give example.
Yes! Javascript support automatic type conversion. You should take advantage of it, It is most common way of type conversion used by Javascript developers.
Ex.
var s = ‘5’;
var a = s*1;
var b = +s;
typeof(s); //”string”
typeof(a); //”number”
typeof(b); //”number”
*Question(31) suggested by Florian Rappl

138. When should you use the “var” keyword in Javascript? How does the “var” keyword work?
The answer to the question is easy: you should always use the “var” keyword in Javascript. But, you should also understand why you should always use it, and what could happen if you do not use it. Let’s start with an example:
var testVar = “10”; //this has global scope

function scopeTest() {
var testVar = “20”; // this has local scope
alert(testVar); //WHAT DOES THIS LINE OUTPUT?
}
scopeTest();
alert(testVar); //WHAT DOES THIS LINE OUTPUT?

Note that in the code above we have 2 variables called testVar – the first one has global scope because it is defined outside of the function scopeTest. The second one has local scope because it is defined inside the scopeTest function. In the code above, we output the testVar variable in 2 different places – so the question is what is output in the code above? The code above will output “20″ first and then “10″ – no big surprise there. Because testVar is also defined locally in the scopeTest function, then the global testVar variable defined outside the function (the one with a value of “10″) is effectively ignored inside the function scopeTest(), so the alert inside the scopeTest function will output “20″. Then, the 2nd alert (outside the function) will output “10″, because the variable that’s recognized is the global one.
139.Declaring a variable without the “var” in a function ?
The next question is what would happen if we remove the “var” in front of the testVar variable that is defined inside the scopeTest function. Here is what the code will look like:
var testVar = “10”; //this has global scope

function scopeTest() {
testVar = “20”; //this variable now has no “var” in front of it
alert(testVar); //WHAT DOES THIS LINE OUTPUT?
}
scopeTest();
alert(testVar); //WHAT DOES THIS LINE OUTPUT?
Because we removed the “var” keyword in front of the testVar variable (the one inside the function), this actually means that the testVar variable inside the function will refer to the global variable that was defined before the function. So when we change the value of the “testVar” variable inside the scopeTest function, we are actually changing the global variable. This means that the value of “20″ will be attached to the “testVar” variable even outside of the scopeTest function. And, the code above will output “20″, and then “20″ again.

140.Wht is difference between Var and no Var?
The “var” keyword in front of a variable will basically declare that variable within the current scope. But, if that “var” keyword is missing, Javascript will search up the scope chain to see if there is a variable with that name in a different scope. And, since it finds a variable with the same name – “testVar” – in the global scope, Javascript will use that variable instead of declaring a new one local to the function.

Sign-up for our email newsletter and get free job alerts, current affairs and GK updates regularly.
Subscribe Here (or)

Leave a Reply

Your email address will not be published. Required fields are marked *


seven − 1 =

Popup Dialog Box Powered By : XYZScripts.com
  • RSS
  • Facebook
  • Google+
  • Twitter