My JavaScript quiz
Recently, there have been a couple of JavaScript quizzes floating around. There was one by Dmitry Baranovskiy (for which I explained the answers) and one by Kangax. But there are so many strange pieces of JavaScript that I thought I’d put together a quiz of my own. I’ve decided that each part will be a single code example followed by one or more questions. Once again, I don’t think such quizzes are useful for job interviews, but they are fun to test the depths of your knowledge. Without further adieu, here it is (answers to follow by end of week).
Example #1
var num1 = 5,
num2 = 10,
result = num1+++num2;
Questions:
- What is the value of
result? - What is the value of
num1? - What is the value of
num2?
Example #2
var x = 5,
o = {
x: 10,
doIt: function doIt(){
var x = 20;
setTimeout(function(){
alert(this.x);
}, 10);
}
};
o.doIt();
Questions:
- What value is displayed in the alert?
Example #3
var num1 = "10",
num2 = "9";
Questions:
- What is the value of
num1 < num2? - What is the value of
+num1 < num2? - What is the value of
num1 + num2? - What is the value of
+num1 + num2?
Example #4
var message = "Hello world!";
Questions:
- What is the value of
message.substring(1, 4)? - What is the value of
message.substr(1,4)?
Example #5
var o = {
x: 8,
valueOf: function(){
return this.x + 2;
},
toString: function(){
return this.x.toString();
}
},
result = o < "9";
alert(o);
Questions:
- What is the value
result? - What is the value displayed in the alert?
That’s it!
Whereas the other quizzes might have made your eyes cross trying to trace scope changes and the like, I wanted this one to be as dead simple as possible. The point? JavaScript is complex enough when written simply. Try to answer the questions without running the code. Answers with complete explanations will follow soon.
Disclaimer: Any viewpoints and opinions expressed in this article are those of Nicholas C. Zakas and do not, in any way, reflect those of my employer, my colleagues, Wrox Publishing, O'Reilly Publishing, or anyone else. I speak only for myself, not for them.
Both comments and pings are currently closed.




20 Comments
Great quiz! I’ve missed only the #2. Had a great 5 minutes!
Adam Brunner on February 16th, 2010 at 10:25 am
Nice
Are we supposed to put/discuss the answers here or shut up about them for now…?
qFox on February 16th, 2010 at 12:02 pm
@qFox – Don’t post answers, keep it interesting for everyone else. I’ll post a longer description of the answers later this week.
Nicholas C. Zakas on February 16th, 2010 at 3:28 pm
Good quiz, thanks. I’ve made a mistake in result’s value in question #5. I’m looking forward to an explanation
Michael Shkutkov on February 16th, 2010 at 3:48 pm
Can you explain #3, I just don’t get it.
jerone on February 16th, 2010 at 3:52 pm
Well, the difficulty of this quiz is less than kangax’s (although, all such quizzes are not for deep theoretical analysis as they are quite simple). But anyway, thanks
The difficulty if this quiz – is 3; kangax’s – is 4.
Dmitry A. Soshnikov on February 16th, 2010 at 4:19 pm
[...] the internet is setting quizzes on some of the more unusual aspects of JavaScript. This time round Nicholas Zakas is providing the entertainment, so I thought I’d provide some answers. Let’s get [...]
Answering Nicholas Zakas’ JavaScript quiz : Ed Spencer on February 16th, 2010 at 4:41 pm
Obligatory answers post – beware spoilers (obviously):
http://edspencer.net/2010/02/answering-nicholas-zakas-javascript-quiz.html
Ed Spencer on February 16th, 2010 at 4:42 pm
Made one mistake, #example 3, question 4
.
Michal Kozak on February 16th, 2010 at 4:58 pm
Good questions.
`substring` vs. `substr` is a killer. I can never get those right
A small nitpick: Two of your questions (#1 and #5) include host objects — `alert` in #1, and `setTimeout` in #5. Those are not part of any standard (they are in HTML5, but it’s still only a draft), so technically, their behavior is unspecified. I know that `alert` coerces object to string, but it’s not clear if it follows something similar to ToString algorithm (9.8 in ES3), or merely invokes `toString` method of an object. And in fact, we can observe that browsers differ when it comes to handling edge cases:
alert({ toString: {}, valueOf: function(){return 1} });
alerts `1` in WebKit, but fails to show dialog in Firefox or Opera.
Similarly:
alert({ toString:function(){ return { } } });
alerts ” in WebKit, but fails to show dialog in Firefox or Opera.
Ditto for `setTimeout`. MDC says that callback function is invoked in context of global object, but we can’t be sure that all implementations follow this behavior.
This is why I tried to focus only on language features in my test. As soon as we touch upon host objects, there’s a whole new level of ambiguity
kangax on February 17th, 2010 at 3:49 am
Good questions.
`substring` vs. `substr` is a killer. I can never get those right
A small nitpick: Two of your questions (#1 and #5) include host objects — `alert` in #1, and `setTimeout` in #5. Those are not part of any standard (they are in HTML5, but it’s still only a draft), so technically, their behavior is unspecified. I know that `alert` coerces object to string, but it’s not clear if it follows something similar to ToString algorithm (9.8 in ES3), or merely invokes `toString` method of an object. And in fact, we can observe that browsers differ when it comes to handling edge cases:
alert({ toString: {}, valueOf: function(){return 1} });alerts `1` in WebKit, but fails to show dialog in Firefox or Opera.
Similarly:
alert({ toString:function(){ return { } } });alerts ” in WebKit, but fails to show dialog in Firefox or Opera.
Ditto for `setTimeout`. MDC says that callback function is invoked in context of global object, but we can’t be sure that all implementations follow this behavior.
This is why I tried to focus only on language features in my test. As soon as we touch upon host objects, there’s a whole new level of ambiguity
kangax on February 17th, 2010 at 3:50 am
[...] this week, I posted my JavaScript quiz, containing some basic code along with questions. Here are the [...]
My JavaScript quiz - answers | NCZOnline on February 18th, 2010 at 9:01 am
Maybe you’ll be interested: http://javascript.ru/blog/Dmitry-A.-Soshnikov/The-quiz
Dmitry A. Soshnikov on February 18th, 2010 at 11:27 am
@kangax – if we lived in a world where everything was governed by a standard, we all wouldn’t be that important.
I understand your point about host objects though I’d say they’re just as necessary to understand as the well-defined ones.
Nicholas C. Zakas on February 18th, 2010 at 2:32 pm
Nice Quiz
I have missed the first questions of examples #4 and #5.
I’ll check the explanation of when valueOf and when toString are used to represent the value of an object.
Amer Aidi on February 18th, 2010 at 2:35 pm
good quiz, made me think and I got all answers right.
keep ‘em coming, ncz
Livingston Samuel on February 18th, 2010 at 4:24 pm
Example #4 subpoint 2. Why do you use nonstandard methods in quiz? If i should answer on this question, i will give answer: TypeError. And when you include nonstanrd methods and host objects, will be good to define specified environment. Environment in which can observe proper behavior of your questions.
Thanks for quiz.
Asen Bozhilov on February 19th, 2010 at 9:29 am
[...] “you know JavaScript”? The recent quizzes were by kangax, Dmitry Baranovskiy and by Nicholas C. Zakas. All that quizzes are interesting, many questions though are mostly theoretical rather than [...]
ECMA-262 by Dmitry A. Soshnikov » The quiz on February 27th, 2010 at 7:42 am
[...] attempting multiple JavaScript quizzes I thought it would be fun to have a go creating one for my readers. It doesn’t involve any [...]
Another JavaScript quiz – James Padolsey on April 27th, 2010 at 5:11 am
[...] }James Read Post After attempting multiple JavaScript quizzes I thought it would be fun to have a go creating one for my readers. It doesn’t involve any [...]
Another JavaScript quiz | DesignerLinks | Home to Web design news, jQuery Tutorials, CSS tutorials, Web Designing tutorials, JavaScript tutorials and more! on April 27th, 2010 at 3:29 pm
Comments are automatically closed after 14 days.