Learn JavaScript basics every day (1)-null and undefined

Learn JavaScript basics every day (1)-null and undefined

null and undefined

JavaScript has 5 simple data types (basic data types) and 1 complex book data type;

  • Basic data types: Undefined, nul, Boolean, Number, String;
  • Complex data types Object :;

The following compares two data types that represent null values, nulland undefined.


nullRepresents a special value, often used to describe "empty value" .

Perform a typeof operation on null, and the result returns the string "object". Null can be considered as a special object value, meaning non-object.

Logically, it nullrepresents a null object pointer.

let dog = null;
  • In fact, generally considered null is the only member of its own type, can represent numbers , strings , objects are no values .
  • If the defined variable is going to be used to save the object in the future , it is best to initialize the variable to null, rather than another value. In this way, as long as you directly check the nullvalue, you can know whether the corresponding variable has saved a reference to an object.
//Perform certain operations on the car object


undefinedAlso used to denote the value of vacancy, expressed undefined , undefinedvalue represents a deeper level of "null." All values do not exist, are represented as undefined.

It is a value of a variable, indicating that the variable is not initialized. If a variable is declared but not initialized, the type of the variable is undefinedas follows:

let a;

The result of not initializing the variable and initializing the variable to undefinedis the same, as follows:

let b;
let c = undefined;
console.log(typeof(b)==typeof(c)? true: false);//true

Generally speaking, there is no need to explicitly set undefinedthe value of a variable to . The introduction of this value is mainly to distinguish between null object pointers and uninitialized variables.

The types of undefined variables and defined but uninitialized variables are both undefined,

let c;//uninitialized
//dd is undefined

Even uninitialized variables are automatically given undefind value, but explicitly initialize variables still be a better choice and habits, if they can do that, then when typeof return undefined, we know that variable has not been detected A statement (that is, it does not exist), rather than it has not yet been initialized.

let e = null;
//f does not exist

3. The connection and difference between null and undfined

  • undefinedValue is derived from nullthe value, both in the ==case are equal, but in ===(exactly equal) are not equal under.
console.log(null == undefined? true:false);//true
console.log(null === undefined? true:false);//false
  • nullIt is an object, it exists, it undefinedis undefined, and it means something that does not exist.

4 references

  • "JavaScript Advanced Programming"
  • The Definitive Guide to JavaScript
Reference: https://cloud.tencent.com/developer/article/1677972 Learn JavaScript basics every day (1)-null and undefined-Cloud + Community-Tencent Cloud