Are You a JavaScript Guru? Try This Test
asserttrue.blogspot.com
asserttrue.blogspot.com
The only slightly relevant question, I thought, was what typeof NaN was. Not knowing that might result is actual bugs.
Javascript has flaws and pitfalls, to be a good Javascript developer you must know what they are, but is it really necessary to be able to pull the results of (1/null) out of your ass?
After a while, I decided that was stupid and removed the bookmark -- if I ever needed to look at operator precedence, then I should use parentheses.
That said, to understand the edge cases are valuable, because you should recognize when you get close to them (not only in your own code).
FTFY
(0.1 + 0.2) + 0.3 == 0.1 + (0.2 + 0.3)
is not an edge case, unfortunately.7. a = "5"; b = 2; c = a+++b;
Why even ask that? It's boring. And operator precedence is not really important in practive, and it's bad style and no one writes like that. It's a question for junior-level interview, when you can't ask anything meaningful and (because you have to ask anything) you ask _this_.
9. (16).toString(16)
Oh, so Guru should memorize every obscure parameter, shouldn't he?
Other questions are fine, though (except 10. Numerical systems, c'mon, it's 2013, not 1980s)
JSHint complains if you don't provide the radix. The reason is to not get your ass in fire in production by the fact (016).toString() === "14" (leading 0 means JS guesses a number is in octal representation, unless it contains 8 or 9). And you use JSHint, don't you? If no, install it right now as a pre-build hook.
--
EDIT: what I wrote above is bullshit, see the comments below; toString is a different story than parseInt.
JSHint does not complain if you omit the radix in a number.toString() call, nor should it. The default radix value for number.toString() is 10, and there are no magical surprises as there are with parseInt(). If you write number.toString() it means exactly the same thing as number.toString(10). There is no reason to provide an explicit radix of 10 unless you just like it better than way.
In the (016).toString() example, the problem isn't the missing radix; (016).toString(10) returns the same value, "14". The problem is the 016 constant itself. That is an octal constant and has already been converted to decimal 14 before the .toString() call. There is nothing that .toString() can do to fix this problem.
Now you're right that it is useful for a developer to know about the radix parameter, so you can easily convert a number to its string representation in hex or binary or whatever base you need.
parseInt() is a different story, of course. You are quite correct that you should never write parseInt(number) without a radix argument, unless you actually want the behavior of a leading zero making it octal and a leading 0x making it hexadecimal.
(edited for clarity)
Regaring parseInt(), the story is even more complex though :) You should never use it without the radix because that behavior is not reliable when it comes to implying octal representation. Calling parseInt("016") yields 16 in Chrome (ES5 compliant [1]), while 14 in Firefox, Opera and IE8.
[1] https://developer.mozilla.org/en-US/docs/JavaScript/Referenc...
Maybe its just because I just woke up, but I didn't even realise why Q1 was interesting until after I read the HN comments. Those that are saying these are all useless for practical work are wrong in my opinion. Some of them are, but not all.
If you saw Q2 and didn't realise the possibility of it being false, the reasons for that are something you need to learn when dealing with any language that uses floating points. Q3 is something every JS programmer should know, as is Q4.
Q5 is more interesting, because the correct answer is "what version of javascript?" Protected terms cannot be used as object properties without being put in quote marks in earlier versions of JS. Doing something like this in an older browser would throw an error.
Q6 could have been better, it would be pretty hard to get it wrong. Something like "5" + 2 would have been more interesting because + is overloaded so you would have to know how javascript treated it in different situations, which is close to essential knowledge.
Q7 is definitely just trivia. No sane person writes code like that. Q8 is trivia too; easy if youre familiar with JS but not hugely useful. Q9 is easily guessable; I didn't know what the param for toString did but I guessed it correctly.
Q10 is important, if you dont know how JS treats numbers with a leading 0 you can get yourself into trouble and have no idea why.
Q11 I learned something new, I didn't know about the ~ operator, I wouldn't use it in regular code though.
Q12 is regex. Either you knew it or you didn't. If you didn't, you learned about \b, which can be rather useful.
Better questions involve things like variable scoping, modules, arguments, event bubbling, prototypes, etc.
Now, where I get stuck: Regular Expressions. I just don't think I will ever get comfortable with that.
Read Mastering Regular Expressions. I thought I understood regexps before I read that book. Now I can use them -- and am, at a minimum, confused on a higher plane... :-)
(function(){console.log(++Math.PI); console.log(Math.PI)})()
logs 4.14... and then 3.14.
Of course no one sane should write a code like this.