There are five basic data types in JS: number, String, Boolean, undefined, null

Undefined and null both have their own values, and both are null

  1. Undefined: undefined. When a variable is declared but not assigned. The default value is undefined
  2. Null: defined. The value defined is null.

Here are the similarities and differences between the two

1. The similarities

(1) values equal (2) Booleans are both false

1.1 the same value

console.log( undefined= =null )//true
Copy the code

1.2 Convert Boolean to false

console.log( Boolean(undefined))//false
console.log( Boolean(null))//false
Copy the code

2. The difference between

(1) Different data types (2) Different values of the number type

2.1 Different Data types

console.log( undefined= = =null )//false     
Copy the code

2.2 Different values are converted to the Number type

console.log( Number(undefined))//NaN
console.log( Number(null))/ / 0
Copy the code

In summary, undefined and NULL are explained in more colloquial terms

  • Undefined is equivalent to housing. Bought a house, but it hasn’t been built yet. (Undefined)
  • Null is equivalent to a blank room. I bought a house, but it was empty and unoccupied. (null)