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

Time:2020-9-28

Null and undefined

JavaScript has five simple data types (basic data types) and one complex book data type;

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

Let’s compare two data types that represent null values,nullandundefined

1.null

nullRepresents a special value and is often describedNull value

The typeof operation is performed on null, and the result returns the string “object“. Null can be regarded as a special object value, which means non object.

Logically,nullRepresents a null object pointer.

let dog = null;
console.log(typeof(dog)); // object
  • In fact, null is generally considered to be the only member of its own type that can be representednumber , character string ,objectyesValueless

  • If the defined variable is ready for use in the futureSave object, it is best to initialize the variable tonullInstead of other values. In this way, just check directlynullValue to know whether the corresponding variable has saved a reference to an object.

if(car!=null)
{
//Perform some operations on the car object
}

2.undefined

undefinedIt is also used to represent the vacancy of a valueUndefined,undefinedValue represents a deeper “null value”. All nonexistent values are represented asundefined

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 isundefinedAs follows:

let a;
console.log(typeof(a));//undefined

Do not initialize variables and initialize variables toundefinedThe results are consistent as follows:

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

In general, there is no need to explicitly set the value of a variable toundefinedThis value is introduced to distinguish null object pointer from uninitialized variable

Undefined variables and defined but uninitialized variables are of typeundefined,

Let C; // uninitialized
//D d not defined
console.log(typeof(c));//undefined
console.log(typeof(d));//undefined
console.log(c);//undefined
console.log (d) ; // error

Even if uninitialized variables are automatically assigned the undefind value, it is still a better choice and habit to explicitly initialize variables. If this can be done, then when typeof returnsundefinedWe know that the detected variable has not been declared (that is, it does not exist), rather it has not been initialized.

let e = null;
//F does not exist
console.log(typeof(e));//object
console.log(typeof(f));//undefined

3. The relationship and difference between null and found

  • undefinedValue is derived fromnullValue, both in the==Below is equal, but in===(strictly equal) is not equal.
console.log(null == undefined ? true:false);//true
console.log(null === undefined ? true:false);//false
  • nullIt is an object and exists,undefinedIt’s undefined. It means something that doesn’t exist.

4 references

  • JavaScript advanced programming
  • JavaScript authority Guide