Q:

Problems with David Shariff's JavaScript Quiz

Some help needed with the Quiz:
Question 5:
function bar() {
    return foo;
    foo = 10;
    function foo() {}
    var foo = '11';
}
alert(typeof bar());

Q: What is alerted?
A: function.
Based on this tutorial, even it does not say that clearly and this is probably my misinterpretation, I was expecting the following behavior, when bar() gets called:

Function foo() added to the lexical environment of bar().
var foo = '11'; overrides this definition, leaving foo undefined.
When return foo; is executed, foo is undefined.

What happens in the initialization? Any links for good documentation?
Question 12:
String('Hello') === 'Hello';

Q: what is the result?
A: true.
I thought String() would return an object and 'Hello' is a primitive string, thus the answer would be "false". Why is it "true"?
Question 20:
NaN === NaN;

Q: what is the result?
A: false.
What is the logic? What happens here?

A:

Question 5:
This is because of hoisting, I answer this here in more detail.
function bar() {
    return foo;
    foo = 10;
    function foo() {}
    var foo = '11';
}

Is semantically the same as:
function bar() {
    var foo = function(){}; // function declarations and 
                           // variable declarations are hoisted
    return foo;
    foo = 10;
    foo = '11';
}

Question 12:
Calling String on something as a function does not create a new object. Note it is not called as a constructor:
String("a"); // a primitive value type string "a"
new String("a"); // this creates a new string object

Quoting the specification:

When String is called as part of a new expression, it is a constructor: it initialises the newly created object.

Question 20:
Pretty much because the specification says so. NaN is not equal to anything, including itself. The rationale is not to get two miscalculations equal to each other by mistake.

