Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reason for the existence of undefined variable in the global scope in JavaScript?

I've found many discussions about undefined as value e.g. how to check if is equal etc. But what is the "engineering" reason for the existence of the undefined as a global variable? There is no null variable in opposite...

console.log(undefined in this);  // logs true
console.log(null in this);  // logs false
like image 428
Adam Bogdan Boczek Avatar asked Nov 11 '13 22:11

Adam Bogdan Boczek


1 Answers

In JavaScript, null is a reserved word; undefined isn't, but is implemented by the environment as a global variable with a value of undefined.

You'll notice you can change the value of undefined, but not of null, except in strict mode (which will throw an error) or ES5 (which will ignore the assignment.)

Now, why undefined is not reserved, I do not know.

like image 161
Mathletics Avatar answered Oct 16 '22 13:10

Mathletics