Subscribe to our mailing list

* indicates required

Saturday, April 25, 2009

Can you pass this JavaScript test?

Think you know JavaScript? Try the following quick quiz. Guess what each expression evaluates to. (Answers given at the end.)

1. ++Math.PI
2. (0.1 + 0.2) + 0.3 == 0.1 + (0.2 + 0.3)
3. typeof NaN
4. typeof typeof undefined
5. a = {null:null}; typeof a.null;
6. a = "5"; b = "2"; c = a * b;
7. a = "5"; b = 2; c = a+++b;
8. isNaN(1/null)
9. (16).toString(16)
10. 016 * 2
11. ~null
12. "ab c".match(/\b\w\b/)

This isn't a tutorial, so I'm not going to explain each answer individually. If you missed any, I suggest while (!enlightenment()) meditate();

The answers:

1. 4.141592653589793
2. false
3. "number"
4. "string"
5. "object"
6. 10
7. 7
8. false
9. 10
10. 28
11. -1
12. [ "c" ]

For people who work with JavaScript more than occasionally, I would score as follows:

(correct answers: score)
8 - 10: EXPERT

A few quick comments.

The answer to No. 2 is the same for JavaScript as for Java (or any other language that uses IEEE 754 floating point numbers), and it's one reason why you shouldn't use floating point arithmetic in any serious application involving monetary values. Floating-point addition is not associative. Neither is float multiplication. There's an interesting overview here.

No. 6: In an arithmetic expression involving multiplication, division, and/or subtraction, if the expression contains one or more strings, the interpreter will try to cast the strings to numbers first. If the arithmetic expression involves addition, however, all terms will be cast to strings.

No. 7: The evaluation order in JavaScript (as in Java and C) is left-to-right, so what you've got here is "a, post-incremented, plus b," not "a plus pre-incremented b."

No. 9: toString( ) takes a numeric argument (optionally, of course). An argument of "16" means base-16, hence the returned string is a hex representation of 16, which is "10." If you write .toString(2), you get a binary representation of the number, etc.

No. 10: 016 is octal notation for 14 decimal. Interestingly, though, the interpreter will treat "016" (in string form) as base-ten if you multiply it by one.

Don't feel bad if you didn't do well on this quiz, because almost every question was a trick question (obviously), and let's face it, trick questions suck. By the same token, if you did well on a test that sucks, don't pat yourself on the back too hard. It just means you're a little bit geekier than any human being probably should be.


  1. It's not the order of evaluation that makes #7 work the way it does. JavaScript could evaluate the expression right to left and the result would be identical:

    "b plus (a, post-incremented)"

    So why isn't the code interpreted as "a plus (pre-incremented b)"? It happens in the lexical analysis. Before any expressions are evaluated, the lexer has already decided that the tokens in "a+++b" are:

    a ++ + b

    and not:

    a + ++ b

  2. I'd say the answer to 9 should be "10" rather than 10.

    An explanation of 5 would be welcome.

  3. Anonymous10:09 PM

    Marius: 5 looks more complicated than it is. The variable a is just a normal object with a property. That property just happens to be named null. It's value is also null.

    So in other words, a.null === null in the example. This can thus be simplified to typeof(null). And this can be compared with the same reasoning for NaN being a number... While it doesn't seem reasonable when thinking of it straight up, it is perfectly reasonable when you consider how it's used by the language. null is a value that can be used in place of any object, NaN can be used in place of any number, thus technically, they're objects and numbers respectively.

  4. Anonymous10:37 PM

    Marius: for #5, typeof null is object. null is assigned to the key "null". The key name is a string. it'd be the same as saying a: {b:null}; typeof a.b. since a.b = null, typeof a.b = object.

  5. Anonymous4:04 AM

    This test may show how well a programmer knows some fine details of the language that can be checked easily when unsure but these amount to trivia. More important skills are knowing the browsers, structuring large programs well and handle errors properly.

  6. "This test may show how well a programmer knows some fine details of the language that can be checked easily when unsure but these amount to trivia. More important skills are knowing the browsers, structuring large programs well and handle errors properly."
    Yeah, but knowing some of these quirks can save you a lot of headaches

  7. Anonymous1:20 PM

    What about no 1? Why does it increment a constant?

  8. Anonymous3:11 PM

    there are no constants in js nub

  9. Just a heads up:

    On spidermonkey, rhino, v8, and squirrelfish Javascript interpreters #5 is a parse error:

    js-shell> a = {null:null}; typeof a.null;
    typein:26: SyntaxError: invalid property id:
    typein:26: a = {null:null}; typeof a.null;
    typein:26: .....^

    On spidermonkey, rhino, v8, and squirrelfish Javascript interpreters #7 produces the string "52". They also turn a into a number type with value 6.

    js-shell> a="5";b="2";a+++b
    js-shell> a
    js-shell> typeof a

    This makes sense, because of the post-increment is handled separately from the "+" operation working on two strings, which results in concatenation. So it does "5"+"2", which results in the "52" for the expression. It then does the post-increment on a which is "5", treating it like a number, because strings don't have a post-increment, and in the process converts it to a number of value 6.


Add a comment. Registration required because trolls.