• In Javascript, Undefined and Null are both basic data types. Null and Undefined are almost equal
  • Both null and undefined are converted to false in an if statement
console.log(null==undefined);    //trueBecause both are converted by default tofalse
console.log(typeof undefined);    //"undefined"  
console.log(typeof null);       //"object"  
console.log(null===undefined);    //false   "= = ="For absolute equality, null and undefined are different types, so the output"false"Copy the code

Null and undefined are basically the same, so what’s the difference between them?

  • Null means there is no object, that is, there should be no value there

    1. As an argument to a function, indicating that the argument to the function is not an object
    2. As the end point of the object prototype chain
  • Undefined indicates a missing value, that is, there should be a value here, but it is not defined

    1. Define parameter, pass no argument, display undefined
    2. If the object property name does not exist, undefined is displayed
    3. The function does not write the return value, so it does not write return, so it gets undefined
    4. I wrote return, but I didn’t assign, so I got undefined
  • Null and undefined are converted to the number data type

    1. Null is converted to 0 by default
    2. Undefined defaults to NaN

The difference between undefined and undeclared

var a; // undefined
b;     // b is not defined
Copy the code

Difference: A variable (such as a) that is declared but not assigned in the variable scope is undefined. In contrast, variables that are not declared in the variable scope are undeclared.

Error: ReferenceError: b is not defined when we attempt to access an “undeclared” variable. And typeof returns undefined for both undefined and declared variables. “Undefined” and “is not defined” are two different things.