<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d8211560\x26blogName\x3dTech+Tips,+Tricks+%26+Trivia\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLUE\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttp://mvark.blogspot.com/search\x26blogLocale\x3den\x26v\x3d2\x26homepageUrl\x3dhttp://mvark.blogspot.com/\x26vt\x3d-5147029996388199615', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>

Tech Tips, Tricks & Trivia

by 'Anil' Radhakrishna
An architect's notes, experiments, discoveries and annotated bookmarks.

Search from over a hundred HOW TO articles, Tips and Tricks


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?  
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"

Labels:

Tweet this | Google+ it | Share on FB

« Home | Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »

»

Post a Comment