JS data types
Before ES6, JavaScript has six data types: Undefined, Null, Boolean, Number, String, Object.
A Symbol has been added to ES6. Objects of this type are never equal, that is, when they are first created, they are passed the same value to solve the problem of property name conflicts, as a token.
Basic and reference data types
Basic types: Undefined, Null, Boolean, Number, String, Symbol in addition to Object
Features: 1. Basic types of access is access by value; 2. Properties and methods cannot be added. 3. There is a stack area for the basic type variables, including the variable identifier and the value of the variable (the stack area is not too large, mainly used to exist the basic type); 4. The basic type of replication is to open up a new store in the stack memory to store new variables;Copy the code
Reference types: There are three main categories
-
Basic reference types: Object Date, Function, Array, RegExp;
-
Basic wrapper types: String, Number, Boolean;
-
Single built-in objects: Global, Math.
Features: 1. Values of reference types are accessed by reference. 2. Reference types can have properties and methods and can change dynamically; 3. Store the stack area and heap area that need memory, wherein the stack area stores variable identifier and pointer to the object in memory; 4. Comparison of reference types is comparison of references; 5. Reference type replication replicates the reference address.Copy the code
-
Undefined type
A variable is undefined when it is declared using var/let and not initialized
var x;
console.log(x); // undefined
Copy the code
-
Null type
Null represents an empty object pointer, which is why the Typeof operator returns object when it checks for null values.
console.log(typeof(null)); // object
Copy the code
-
Null and undefined
Null has only one value, which is Null. The object does not exist.
Undefined has only one value, Undefined. No initialization. Undefined is derived from null.
Undefined is undefined, null is defined but null.
Null and undefined do not have toString() methods, and String functions do not return literals for these two values.
Conversions between basic types
Turn digital Number ()
The Number function converts the type to a numeric type, ignores all leading zeros, and returns NaN if the argument cannot be converted to a Number
NaN, or non-numeric, is a special Number value. NaN has two characteristics: any operation with NaN returns NaN, and NaN is not equal to any value, including NaN. Use the isNaN() function to determine if a value isNaN. When isNaN() receives an argument, it attempts to convert the value to a number, and returns true for any value that cannot be converted to a number
console.log(Number("x")) // NaN
console.log(Number(undefined)) // NaN
console.log(Number(null)) / / + 0
console.log(Number(false)) / / + 0
console.log(Number(true)) / / 1
console.log(Number("1")) / / 1
console.log(Number("1")) // -1
console.log(Number("1.2")) / / 1.2
console.log(Number("0123")) / / 123
console.log(Number("0123")) / / - 123
console.log(Number("")) / / 0
console.log(Number({a: 1})) // NaN
console.log(Number([1.2.3])) // NaN
Copy the code
Turn String()
The String function converts a type to a String
console.log(String()) // Empty string
console.log(String(undefined)) // undefined
console.log(String(null)) // null
console.log(String(false)) // false
console.log(String(true)) // true
console.log(String(0)) / / 0
console.log(String(-0)) / / 0
console.log(String(NaN)) // NaN
console.log(String(1)) / / 1
console.log(String({a: 1})) // "[object Object]"
console.log(String([1.2.3])) / / "1, 2, 3"
Copy the code
Boolean value
The Boolean function converts a type to a Boolean type, which is true except when it is false
Boolean(false) //false
Boolean(undefined) //false
Boolean(null) //false
Boolean(+0) //false
Boolean(-0) //false
Boolean(NaN) //false
Boolean("") //false
Copy the code
Object to string and number
Objects are converted to strings by the toString method, and numbers by the Valueof method. All objects with any value other than null and undefined have the toString method, which is generally the same as the result returned by using the String method.
Object. The prototype. The toString method according to the [[class]] internal properties of an Object, returns [Object class] string
console.log(({}).toString()) // "[object Object]"
console.log([].toString()) / / ""
console.log([0].toString()) / / 0
console.log([1.2.3].toString()) / / 1, 2, 3
console.log((function(){}).toString()) // function (){}
console.log((/\d+/g).toString()) // /\d+/g
console.log((new Date(2021.0.1)).toString()) // Fri Jan 01 2021 00:00:00 GMT+0800
Copy the code
ValueOf converts an object to a valueOf basic data
ValueOf implementations for different built-in objects:
- String => Returns a String value
- Number => Returns a numeric value
- Date => Returns a number, the time value
- Boolean => Returns the value of the Boolean itself
- Object => Return this (itself)
console.log('123'.valueOf()) / / '123'
console.log((1).valueOf()) The JS engine cannot determine what '. 'means, is it a dot operator (object method) or a floating point number?
console.log(new Date().valueOf()) / / 1628848676547
console.log(true.valueOf()) // true
console.log(({}).valueOf()) By default {} is parsed by the browser as a block of code, not an object, so it will be an error
console.log([].valueOf()) / / []
console.log((function(){}).valueOf()) / / ƒ () {}
Copy the code
Now one might think what about translating objects using the Number method?
console.log(Number(true)) / / 1
console.log(Number(false)) / / 0
console.log(Number(123)) / / 123
console.log(Number(null)) / / 0
console.log(Number(undefined)) // NaN
console.log(Number('0123')) / / 123
console.log(Number('1.1')) / / 1.1
console.log(Number(' ')) / / 0
console.log(Number('0xa')) / / 10
console.log(Number({})) // NaN
console.log(Number({a : 1})) // NaN
console.log(Number([])) / / 0
console.log(Number([0])) / / 0
console.log(Number([1.2.3])) // NaN
console.log(Number(function(){})) // NaN
console.log(Number(new Date(2021.0.1))) / / 1609430400000
Copy the code
From the result of the Number method, we draw the following conclusions
- If Boolean, true and false are converted to 1 and 0, respectively.
- If it is a numeric value, it is simply passed in and returned.
- If it is null, 0 is returned.
- If undefined, return NaN.
- If it is a string, follow the following rules:
- If the string contains only numbers (including those preceded by a plus or minus sign), convert it to a decimal number, that is, “1” becomes 1, “123” becomes 123, and “01” becomes 1 (leading zeros are ignored);
- If the string contains a valid floating point format, such as “1.1”, it is converted to the corresponding floating point value (again, leading zeros are ignored);
- If the string contains a valid hexadecimal format, such as “0xa”, convert it to a decimal integer of the same size.
- If the string is empty (does not contain any characters), convert it to 0;
- If the string contains characters other than those in the above format, convert it to NaN.
- If it is an object, the object’s valueOf() method is called and the returned value is converted according to the previous rule. If the result of the conversion is a NaN, the toString() method of the object is called and the returned string value is converted again according to the previous rule.
JSON.stringify
The json.stringify () method converts a JavaScript value to a JSON string by calling the toString method, which is also a type conversion method. When working with a basic type, it's basically the same as using toString, and the result is a string except for undefined, which behaves differently when parsing an array
console.log(JSON.stringify(null)) // "null"
console.log(JSON.stringify(undefined)) // Undefined, this undefined is not the string undefined
console.log(JSON.stringify(true)) // "true"
console.log(JSON.stringify(42)) 42 "/ /"
console.log(JSON.stringify("42")) 42 "/ /"
console.log([1.2.3].toString()) // "1,2,3" is removed from the array []
console.log(JSON.stringify([1.2.3])) / / [1, 2, 3] ""
Copy the code