The cause of

Void 0 = undefined; void 0 = undefined; void 0 = undefined; void 0 = undefined; By the way, undefined and null are taken out to review.

introduce

Undefined and null are the seven types of data in JS

  • Undefined is declared undefined
  • Null is a null pointer and will not occur unless it is defined as null

null

There are some odd things about null, such as common interview questions

Why is typeof NULL object?

I have been exposed to this topic since the beginning of js learning. It is said that it is a bug in JS design, but because there are too many old codes, I am afraid that correction will cause influence, so I keep it. Some people say that this is a null pointer, an empty object, so it is called object. But THAT didn’t seem convincing enough to me, so I went online and did some research

In the original version of javascript, the 32-bit system used, and the underlying representation was binary, stored the type information of the variable at the lower level for performance reasons:

  • 000: object

  • 1: the integer

  • 010: floating point number

  • 100: string

  • 110: Boolean

  • All 0: null

So typeof uses this mechanism, so null all zeros compound the object 000, so it is judged as object

And there’s another caveat

null == undefined //trueNull and undefined are compared by ==true, and there is no implicit conversion, istrue
Copy the code

undefined

Next is undefined, this design feels also has a very big bug, so important thing, unexpectedly is not a keyword, unexpectedly is a variable. What does a variable mean, you can change it at will!!

So some programmers use void 0 instead of undefined, and who knows if undefined has been replaced by some sinister person.

thenvoid 0How did it come about?

Based on ECMAScript, void behind what is undefined, some said back in the habit of void 0 from other languages, is a kind of old driver writing, so the next time we want to use undefined will void 0 instead, this is the old driver

When is undefined replaced?

I tested it myself here

  • In Chrome 69.0.3497.100
    var undefined = 2;
    console.log(undefined); //undefined 
Copy the code

The result is undefined, which means you can’t change undefined under the global scope in the new Version of Google Chrome

// const undefined = 2; //Identifier'undefined' has already been declared
    console.log(undefined); 
Copy the code

If const is declared,let is declared


  • Try Internet Explorer (IE11)
    var undefined = 2;
    console.log(undefined); //undefined 
Copy the code

The result is undefined, so it cannot be replaced under ie11’s global scope

  • Until Internet Explorer 8 changed
var undefined = 2; console.log(undefined); / / 2Copy the code

Ie8’s global scope can be changed by undefined

That doesn’t seem to help, does it? Ie8 is not compatible with many companies. Wait

  • In Google Chrome version 69.0.3497.100, let’s try it in function scope
(function() { var undefined = 2 console.log(undefined); / / 2}) ()Copy the code

Found that this changed undefined

(function() { const undefined = 2 console.log(undefined); / / 2}) ()Copy the code

Const also works, and undefined is overwritten in versions 11,10,9, and 8 of IE without exception

Why is that?

In the global scope, the global variable will become a property of the window, so let’s look at what’s special about this property

console.log(Object.getOwnPropertyDescriptor(window, undefined)); //{value: undefined, writable: false, enumerable: false, configurable: false}
Copy the code

You can’t overwrite, enumerate, change eigenvalues or delete them, so you can’t overwrite them under the global scope.

That ie8? Why is it ok?

console.log(Object.getOwnPropertyDescriptor(window, undefined)); //{configurable: false, enumerable: false, value: undefined, writable: true}
Copy the code

Again, ie8 is not enumerable, cannot delete or modify eigenvalues, but!! Can be rewritten!! So ie8’s global scope can be overridden!!

Conclusion: although undefined cannot be changed in the global scope of most browsers, it can be overridden in the function scope or block-level scope. Most people would never do this, but use viod instead 0, just in case, but also more like an old driver’s code