JavaScript Questions & Answers - 1
These questions & answers are based on the material from the book Eloquent JavaScript by Marijn Haverbeke, which covers ECMAScript 5.
Q. Operators are generally symbols. Name an operator that is written as a word
A. typeof - a unary operator
Q. What will the following statement return?
Q. What will the following statement return?
Q. What is the difference between == and === ?
A. When an operator is applied to the “wrong” type of value, JavaScript will quietly convert that value to the type it wants, using a set of rules that often aren’t what you want or expect. This is called type coercion. Example -
The rules for converting strings and numbers to Boolean values state that 0, NaN, and the empty string ("") count as false, while all the other values count as true. Because of this, expressions like 0 == false and "" == false are also true.
If you do not want any automatic type conversions to happen, there are two extra operators: === and !==. === tests whether a value is precisely equal. So "" === false is false as expected.
The three-character comparison operators should be used defensively to prevent unexpected type conversions from tripping you up. But when you’re certain the types on both sides will be the same, there is no problem with using the shorter operators.
=== is not quicker if the types are the same. If types are not the same, === will be quicker because it won't try to do the conversion
Q. The expression to the right binary operators || and && is evaluated only when necessary. True or False
A. True.
In the case of true || X, no matter what X is—even if it’s an expression that does something terrible—the result will be true, and X is never evaluated. The same goes for false && X, which is false and will ignore X. This is called short-circuit evaluation.
Q. What will the following statement return?
Q. What will the following statement return?
console.log can take any number of arguments — it outputs all of the values
Q. What is the different between the indexOf method supported by both the Array and String objects?
A. One difference is that a string’s indexOf can take a string containing more than one character, whereas the corresponding array method looks only for a single element.
Q. In browsers, the global scope object is stored in the ___ variable.
A. window
var myVar = 10;
Q. What will the following statement return?
console.log(typeof null)
A. "object"
Q. Operators are generally symbols. Name an operator that is written as a word
A. typeof - a unary operator
Q. What will the following statement return?
console.log ( NaN == NaN );A. false
Q. What will the following statement return?
console.log ( null == undefined ) ;A. true
Q. What is the difference between == and === ?
A. When an operator is applied to the “wrong” type of value, JavaScript will quietly convert that value to the type it wants, using a set of rules that often aren’t what you want or expect. This is called type coercion. Example -
console.log( false == 0) // true
The rules for converting strings and numbers to Boolean values state that 0, NaN, and the empty string ("") count as false, while all the other values count as true. Because of this, expressions like 0 == false and "" == false are also true.
If you do not want any automatic type conversions to happen, there are two extra operators: === and !==. === tests whether a value is precisely equal. So "" === false is false as expected.
The three-character comparison operators should be used defensively to prevent unexpected type conversions from tripping you up. But when you’re certain the types on both sides will be the same, there is no problem with using the shorter operators.
=== is not quicker if the types are the same. If types are not the same, === will be quicker because it won't try to do the conversion
Q. The expression to the right binary operators || and && is evaluated only when necessary. True or False
A. True.
In the case of true || X, no matter what X is—even if it’s an expression that does something terrible—the result will be true, and X is never evaluated. The same goes for false && X, which is false and will ignore X. This is called short-circuit evaluation.
Q. What will the following statement return?
alert ("Hello" , "Good Evening" , "How do you do ?");A. The function alert officially accepts only one argument. Yet when you call it like this, it doesn’t complain. It simply ignores the other arguments and shows you “Hello”. JavaScript is extremely broad-minded about the number of arguments you pass to a function. If you pass too many, the extra ones are ignored. If you pass too few, the missing parameters simply get assigned the value undefined
Q. What will the following statement return?
console.log ("R" , 2 , "D" , 2);A. // R 2 D 2
console.log can take any number of arguments — it outputs all of the values
Q. What is the different between the indexOf method supported by both the Array and String objects?
A. One difference is that a string’s indexOf can take a string containing more than one character, whereas the corresponding array method looks only for a single element.
console.log("one two three".indexOf("ee") ); //11
Q. In browsers, the global scope object is stored in the ___ variable.
A. window
var myVar = 10;
console.log ("myVar" in window ) ; // true console . log ( window . myVar ) ; // 10
Q. What will the following statement return?
console.log(typeof null)
A. "object"
Comments
Post a Comment