JavaScript Basic Type Values Undefined Null Boolean

  • 2021-07-24 09:23:56
  • OfStack

A general introduction

There are five simple data types (also known as basic data types) in ECMAScript: Undefined, Null, Boolean, Number, and String.

Undefined

Undefined is an attribute of a global variable that has only one value: undefined. When a variable is declared with var but not initialized, the value of the variable is undefined.


var test;
console.log(test); //undefined

You can only do one operation on an undeclared variable, that is, typeof detects its data type


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

Common scenarios for undefined:

1. Variables that have been declared but not assigned

2. typeof detects the data type of an undeclared variable

3. Execution results of functions without return values

4. The function has no parameters passed in

5. void (expression)


//1 , 
 var test;
 console.log(test); //undefined
 //2 , 
 //var a 
 console.log(typeof a); //undefined
 //3 , 
 function f(){}
 console.log(f()); //undefined
 //4 , 
 function f(x){
 return x;
 }
 console.log(f()); //undefined
 //5 , 
 console.log(void(0)); //undefined

Strict equality and undefined

You can use strict equality and unequality to detect whether a variable has a value


var x ;
 if(x === undefined){
 console.log(1);
 }else{
 console.log(2);
 }

Note: Strict equality is used here because null = = undefined

void Operator and undefined

You can use the void operator instead of undefined


var x ;
 if(x === void(0)){
 console.log(1);
 }else{
 console.log(2);
 }

Null

The Null type is the second data type with only one value, and this special value is null. From a logical point of view, the null value represents a pointer to an empty object. null is often placed in a parameter position that expects 1 object but does not refer to any object.

console.log(document.getElementById('ol')); //null

Note:

console.log(typeof null); //object

As mentioned earlier, the value of null represents a pointer to an empty object, and the data type detected by typeof is definitely object

From the bottom level, different objects are represented as binary at the bottom level. If the first three bits of binary are all 0 in javascript, it will be judged as object type, and the binary representation of null is all 0, so 'object' will be returned when typeof is executed

null and undefined

null and undefined are different, but they both denote null values, null for "null" and undefined for "undefined".


typeof null // object 
 typeof undefined // undefined
 null === undefined // false
 null == undefined // true
 null === null // true
 null == null // true
 !null //true
 isNaN(1 + null) // false
 isNaN(1 + undefined) // true

Boolean

The Boolean type is the most used type in ECMAScript and has only two literals: true and false

Note: The literals true and false of type Boolean are case sensitive

Although there are only two literals of type Boolean, the values of all types in ECMAScript have values equivalent to these two Boolean values. To convert 1 value to the corresponding Boolean value, call the transition function Boolean ()


var message = 'hello world';
var messageBoolean = Boolean(message);
console.log(messageBoolean); //true

Conversion table:

字符串 数字 布尔值
undefined "undefined" NaN false
null  “null”  0  false
true  “true”  1  
false  “false”  0  
""空字符串    0  
"1.2" 非空,数字    1.2  
"o" 非空,非数字      
 0  "0"    false
 -0 "0"    false 
 NaN  "NaN"   false 
 infinity  "infinity"   true 
-infinity   "infinity"   true 


Related articles: