Why undefined can be covered by with while null can't?

Viewed 49

Why can undefined be covered by with while null can't? And also, let can be covered while var can't (it will throw a error).

So what is the rule that a variable can be redefined?

enter image description here

var obj = {null:1,undefined:2};
with(obj) console.log(null,undefined);

1 Answers

Because undefined is a predefined global identifier, but null is a literal. Because var is a keyword, but let isn't (in loose mode; it is in strict mode). (In loose mode, it acts like a keyword or identifier depending on where it appears.)

I don't recall why undefined is a predefined global rather than being a keyword. The reason let is only a keyword in strict mode is that it was only added in 2015, 20 years after the language was first defined.

Related