简体   繁体   中英

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 eg 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

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.

The simple answer is that ECMAScript defines a property of the global object called undefined whose initial value is undefined . This object is created before entering any execution context , so it always exists before any code is run.

It is likely a convenience for testing for the undefined value, otherwise it would be common do to things like:

var undefined;

// test against undefined.

function foo() {

    // And maybe here too
    var undefined;

   // test against undefined.
}

Since the global undefined property is not write protected, it is common to do things like:

(function(global, undefined) {

  // In here you can be sure undefined has the value undefined

}(this));

Edit

In summary, the goal seems to have been to create two separate ways of returning a value of "there is no value", where undefined means "my value is not defined at all", whereas null means "I do not have a value".

eg

function getElement(id) {
  if (document && document.getElementById) {
    return document.getElementById(id);
  }
  // return undefined implied
}

If the above function returns null , you know it ran successfully but didn't find an element with the provided id, whereas if it returns undefined , you know it didn't even try.

In regard to:

There is no null variable in opposite

I guess you are saying "why is undefined a global variable whereas null is global object.

The answer is the same: because that's how the spec was written. One explanation (not a reason) is that JavaScript was prematurely optimised and there are a few fundamental issues that should have been sorted earlier, but it's too late to change them now (and has been for many years).

Perhaps it would have been much better (IMHO) if undefined was implemented just like null (ie as a read–only property of the global object) and that:

typeof undefined === 'undefined'

and

typeof null === 'null'

since that is what they actually are. But changing that now might create more issues than it solves.

And then there is NaN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM