This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

What is undefined?

Undefined is both a primitive data type and a primitive value data

Undefined is an attribute on a global object

console.log(window.undefined) // undefined
Copy the code

Is undefined writable? Is it configured?

Do not writewritable: false
window.undefined = 1; Log (window.undefined) // undefinedCopy the code
configurableconfigurable: false
delete window.undefined; Console. log(window.undefined) // Print undefined as normalCopy the code
An enumerationenumerable: false
for(var k in window){ if(k === undefined){ console.log(k); // No print}}Copy the code
Not redefinableObject.defineProperty()An error in the
Object.defineProperty(window, 'undefined', {
  enumerable: true,
  writable: true,
  configurable: true
})
Copy the code

About the undefined

Unassigned variables are automatically assigned toundefinedAnd the type as wellundefined
var a; console.log(a); // undefined function test(a){ console.log(typeof a); // type undefined return a; }Copy the code
The function does not pass values to printundefined
console.log(test()); // undefined
Copy the code
No return value is written inside the functionundefined
function test2(){
  console.log(123);
  // return undefined
}
console.log(test2()); // undefined
Copy the code
undefinedCannot be used as a variable
var undefined = 1; 
console.log(undefined); // undefined
Copy the code
undefinedIt is not a JS reserved word or keyword. It is not writable globally, but when the local is used as a variable, it is not found globally. The local scope can be printed out1Even in strict mode
function test3(){ 'use strict'; Var undefined = 1; console.log(undefined); // 1 } test3();Copy the code
void(0)Evaluate 0, always returnundefined
var a, b, c; a = void(b = 1, c = 2); console.log(a, b, c); // undefined 1 2 // <a href="javascript:void(0)"> // Return undefined to prevent <a> tags from going console.log(void(0) === window.undefined;  // true void(0) all equals window.undefinedCopy the code
In case the local scope does not fetch the realundefinedThe old program will usevoid(0)The returnedundefinedAs aundefinedAnd check with the other variables to see if it’s equal toundefined
function test4(){ var undefined = 1; console.log(undefined); // 1 console.log(void(0)); // undefined console.log(void(0) === undefined); // false, because undefined can be used as a variable in local scope}Copy the code