ganqqwerty / 123-Essential-JavaScript-Interview-Questions

JavaScript interview Questions
BSD 3-Clause "New" or "Revised" License
5.01k stars 1.18k forks source link

Question 2, 12, 15 #29

Closed fengshuo closed 6 years ago

fengshuo commented 6 years ago

Question 2

var y = 1;
if (function f(){}) {
    y += typeof f;
}
console.log(y);

I got 1object instead of 1undefined, might be related to this issue

Question 12

var trees = ["redwood", "bay", "cedar", "oak", "maple"];
delete trees[3];
console.log(trees); // ["redwood", "bay", "cedar", empty, "maple"]; //trees[3] is empty instead of undefined on chrome

Question 15

Here associativity of the assignment operator is Right to Left so first typeof y will evaluate first which is undefined and assigned to z and then y would be assigned the value of z and then z would be assign value 1.

z should be assign the value of y which is undefined

ganqqwerty commented 6 years ago

Question 2 in which environment did you get 1object instead of 1undefined? I tried to run this snippet on Chrome, Firefox, Safari and node.js (all from the console), and in jsfiddle

ganqqwerty commented 6 years ago

Question 12 Thanks, it's interesting, I didn't know about that! It seems to me that it's just a way of pretty printing the undefined, check it out:

> trees[3]
undefined
> typeof trees[3]
"undefined"
ganqqwerty commented 6 years ago

Question 15 I didn't understand your comment

ganqqwerty commented 6 years ago

closing for now. @fengshuo if you can explain what you meant in your comment to Question 15, I will reopen the task and fix whatever the issue is.