Problems with David Shariff JavaScript Quiz

Need some help with Quiz :

Question 5:

function bar() {
    return foo;
    foo = 10;
    function foo() {}
    var foo = '11';
}
alert(typeof bar());

      

Q: What is warned? A: function.

Based on this tutorial , even he doesn't say it is clear and this is probably my misinterpretation, I was expecting the following behavior when bar()

called:

  • The function has been foo()

    added to the lexical environment bar()

    .
  • var foo = '11';

    overrides this definition, leaving foo

    undefined.
  • When executed return foo;

    , foo

    - undefined The.

What Happens During Initialization? Any links for good documentation?

Question 12:

String('Hello') === 'Hello';

      

Q: what is the result? A: true.

I thought it String()

would return an object, and 'Hello'

is a primitive string, so the answer will be "false". Why is this "true"?

Question 20:

NaN === NaN;

      

Q: what is the result? A: false.

What is the logic? What's going on here?

+3


source to share


2 answers


Question 5:

This is because of the rise, I answer in more detail here .

function bar() {
    return foo;
    foo = 10;
    function foo() {}
    var foo = '11';
}

      

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

something as a function does not create a new object. Note that it is not called as a constructor:

String("a"); // a primitive value type string "a"
new String("a"); // this creates a new string object

      

Quote specification :

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

Question 20:

Largely because the spec says so. NaN

is not equal to anything, including itself. The rationale is not to make two mistakes by mistake.

+3


source


I made a javascript video series in which I explain all javascript questions to David Sharrif. Take a look at this playlist:



Solutions: David Sharrif Quiz

+1


source







All Articles