This is the third day of my participation in the August Challenge. For details, see:August is more challenging
The data type
ECMAScript has six simple data types: Number, String, Boolean, Null, and Undefined Symbol. Symbol is added to ES6.
There is also a complex data type called Object, there is a sentence in JS called everything is an Object, will be discussed in detail later. An Object is an unordered combination of name and value pairs.
The typeof operator
The typeof operator has to be mentioned before we go into specific data types, because ECMAScript’s data types are loose, so an operator is needed to determine the data typeof a variable. Using the Typeof operator on a variable returns one of the following strings: – “undefined” means the value is undefined;
- “Boolean” indicates that the value is a Boolean value;
- String indicates that the value is a string.
- “Number” indicates that the value is a number.
- “Function” indicates that the value is a function;
- “Symbol” indicates that the value is a symbol;
- “Object” indicates that the value is an Object or Null
Note that using Typeof for NULL returns object here because NULL is considered a reference to an empty object
Undefined type
The Undefined type has only one value, the special value Undefined. When a var or let declares a variable but is not initialized, it assigns a value Undefined to the variable.
letA;console.log(a);// This will print undefined
console.log(b);// An error is reported
console.log(typeof a);// This will print undefined
console.log(typeof b);// This will also print undefined
Copy the code
An interesting thing to see from the above code is that undeclared variables are also undefined after calling Typeof.
Null type
Null type The same only one value is the special value Null. As mentioned earlier with the Typeof operator, using Typeof for null returns object because a null value represents a pointer to an empty object. For Null, remember this: any time a variable holds an object and there is no object to hold, fill the variable with Null. This preserves the semantics that NULL is a pointer to an empty object.
Boolean type
The Boolean type has two literals: true and false. Although the Boolean type has only two values, values of all other types have their corresponding Boolean equivalent, and the Boolean() function can be called to convert values of other types to Boolean values. The conversion rule is as follows
The data type | Convert to true | The value converted to false |
---|---|---|
Boolean | true | false |
String | Non-empty string | “” Empty string |
Number | Any non-zero value | 0 and NaN (not a number) |
Object | Any object | null |
undefined | There is no | undefined |