JS data type
1.1 Basic data types
Undefined
Null
Number
String
Boolean
Symbol
BigInt
1.2 Reference data types
Object->Function Array Date
Data type conversion
2.1 Convert to a Boolean value using Boolean()
const To_bool = [undefined.null.0.1.' '.'abc'.'123'.Symbol(), {}, {age:24.name:'zcl'},NaN.Infinity.0n.1n, [], [1.2.3].function(){},new Date()]
for (let i of To_bool){
console.log(Boolean(i)) // false false false true false true true true true true false true true true true true true
}
Copy the code
Null string 0 undefined null NaN 0n is false all other types are true
Note that empty arrays and empty objects are also true
2.2 Convert to a String using the.toString() and String() methods
const to_string = [undefined.null.0.1.true.false.Symbol(), {}, {age:24.name:'zcl'},NaN.Infinity.0n.1n, [], [1.2.3].function(){},new Date()]
for (let i of to_string){
console.log(String(i)) // 'undefined' 'null' '0' '1' 'true' 'false' 'Symbol()' '[object Object]' '[object Object]' 'NaN' 'Infinity' '0' '1' '' '1, 2, 3' function () {} '
}
Copy the code
The toString() method notices that an empty array is an empty string
2.3 Convert to numeric type using Number(), parseInt(), parseFloat() methods
const to_number = [undefined.null.true.false, {}, {age:24.name:'zcl'}, [], [1.2.3].function(){},new Date(),'1'.'abc'.' ']
for (let i of to_number){
console.log(Number(i)) // NaN 0 1 0 NaN NaN 0 NaN NaN 1624363844249 1 NaN 0
}
Copy the code
Undefined Empty and non-empty object non-empty array function non-numeric string is NaN
Null null array false is 0
True to 1
Data of type Symbol cannot be converted to numeric type
3. Data type judgment
3.1 typeof
console.log(typeof 2) // number
console.log(typeof Infinity) // number
console.log(typeof NaN) // number
console.log(typeof Number.MAX_SAFE_INTEGER) // number
console.log(typeof true) // boolean
console.log(typeof '123') // string
console.log(typeof undefined) // undefined
console.log(typeof null) // object
console.log(typeof Symbol()) // symbol
console.log(typeof []) // object
console.log(typeof function(){}) // function
console.log(typeof new Date()) // object
console.log(typeof {}) // object
Copy the code
Special nulls are of type object when typeof is used
Function (){} typeof should also be object
3.2 the Object. The prototype. ToString. Call ()
function typeMyself(value){
let res = Object.prototype.toString.call(value).split("") [1] // Undefined]
return res.substring(0,res.length-1).toLowerCase()
}
console.log(typeMyself(2)) // number
console.log(typeMyself(true)) // boolean
console.log(typeMyself('123')) // string
console.log(typeMyself(undefined)) // undefined
console.log(typeMyself(null)) // null
console.log(typeMyself(Symbol())) // symbol
console.log(typeMyself([])) // array
console.log(typeMyself(function(){})) // function
console.log(typeMyself(new Date())) // date
console.log(typeMyself({})) // object
Copy the code
Use the Object. The prototype. ToString. Call () can accurately distinguish null type
Function (){} new Date() [] function(){} new Date() [
3.3 Instanceof Is used to determine object types
console.log(2 instanceof Number) // false
console.log(true instanceof Boolean) // false
console.log('123' instanceof String) // false
console.log(Symbol(a)instanceof Symbol) // false
console.log(undefined instanceof Object) // false
console.log(null instanceof Object) // false
console.log([] instanceof Array); // true
console.log([] instanceof Object); // true
console.log(function(){} instanceof Function); // true
console.log(function(){} instanceof Object); // true
console.log({} instanceof Object); // true
Copy the code
Instanceof cannot determine the base data type and null undefined is even more difficult to determine
3.4 the constructor
console.log((2).constructor === Number); // true
console.log((true).constructor === Boolean); // true
console.log(('123').constructor === String); // true
console.log(([]).constructor === Array); // true
console.log(([]).constructor === Object) // false
console.log((function() {}).constructor === Function); // true
console.log((function() {}).constructor === Object); // false
console.log(({}).constructor === Object); // true
Copy the code
3.5 summarize
Typeof can accurately determine basic data types
Instanceof is used to determine the reference data type, but functions and arrays or Date types can be identified as Array,Function, Date, or Object if instanceof is used, whereas typeof only functions can be identified as Function types. All others are of Object type. But using Object. The prototype. ToString. Call () can determine the new Date () for the Date, the function () {} for the function, for array, the array Object for the Object.
4. JSON. Stringify ()
console.log(JSON.stringify(true)) // "true"
console.log(JSON.stringify(1)) / / "1"
console.log(JSON.stringify(NaN)) // "null"
console.log(JSON.stringify(Infinity)) // "null"
console.log(JSON.stringify(null)) // "null"
console.log(JSON.stringify('123')) / / "123"
console.log(JSON.stringify(Symbol())) // undefined
console.log(JSON.stringify(function(){})) // undefined
console.log(JSON.stringify(undefined)) // undefined
console.log(JSON.stringify([])) / / "[]"
console.log(JSON.stringify({})) / / "{}"
console.log(JSON.stringify([undefined.function(){},Symbol()))// "[null,null,null]"
console.log(JSON.stringify({[Symbol()] :123.x:undefined.y:function(){}})) / / "{}"
Copy the code