define

Undefined is a basic data type in JavaScript that has a unique value called Undefined.

Undefined Null Number String Boolean Symbol

The typeof operator indicates the typeof the operand
typeof undefined; // "undefined"
typeof 2; // "number"
Copy the code

use

In general, undefined refers to the undefined property of the window object. That is, when you use undefined on the browser side, you actually use window.undefined, and the initial value of window.undefined is undefined.

If undefined is used, use window.undefined instead of undefined.

Look at the following example:

null = 10; // An error was reported because a specific value could not be assigned
undefined = 11; // No error, because undefined refers to the undefined property of the window object
'undefined' in window; // true
Copy the code

If still in doubt, here’s another chestnut:

"use strict";
undefined = 11; 
// Script snippet #1:2 Uncaught TypeError: Cannot assign to read only property 'undefined' of object '#<Window>'
In strict mode, assigning a read-only attribute to an object raises an error
Copy the code

Window. undefined can be changed to any value in ES3, but in ES5 it is a non-writable, non-configurable property and its value is always undefined.

So in a global environment, if the window object is not overwritten, it is safe to use undefined because it is not modifiable.

Window. undefined is not modifiable, but the window object is modifiable

let a;
a === undefined;         // true
a === window.undefined;  // true

// window.undefined cannot be modified
window.undefined = 11;
window.undefined; // undefined
Copy the code

However, it may not be reliable to use undefined inside a function. Although undefined is a data type, it is not a keyword, so it can be used as an identifier, for example:

(function () {
    let a;
    let undefined = '123';
    console.log(a === undefined); // false
    console.log(a); // undefined
    console.log(undefined); / / 123
    console.log(window.undefined) // undefined}) ()Copy the code

As mentioned earlier, window.undefined is not always reliable either. Look at the following example:

function fun() {
   let undefined = 'hello world',
       f = {},
       window = {
           'undefined': 'abc'
       };
   console.log(undefined);// hello world
   console.log(window.undefined); //abc
   console.log(f.a === undefined); //false
   console.log(f.a === void 0); //true
}
fun();
Copy the code

As you can see, if a piece of code overwrites the Window object, there are some problems with using undefined, but there is always a solution.

For example, jQuery source code is used like this:

// The self-executing function defines two parameters, but only one parameter is passed when calling, so the second parameter undefined is undefined
(function( window, undefined ) { 
    // Construct the jQuery object
    var jQuery = (function() { 
        var jQuery = function( selector, context ) {
            return new jQuery.fn.init( selector, context, rootjQuery );
        };
        // omit code for other modules
     }); 
     window.jQuery = window.$ = jQuery; }) (window );

Copy the code

Underscore is used in the source code like this:

// Whatever follows the void operator returns undefined
_.isUndefined = function(obj) {
    return obj === void 0;
};

Copy the code

The assignment

You can assign a variable to undefined in one of the following ways:

  1. When a variable is declared, but not assigned, the variable is equal toundefined;
  2. When the function was called, the argument that should have been provided was not providedundefined;
  3. The object has no assigned attribute, which has a value ofundefined;
  4. The function returns no value by defaultundefined;
  5. performvoid 0, the return value isundefined;