JS数据类型转换与其判别方法详细比较

261 阅读2分钟

一.JS数据类型

1.1 基本数据类型

Undefined Null Number String Boolean Symbol BigInt

1.2 引用数据类型

Object->Function Array Date

二.数据类型转换

2.1 利用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
}

空字符串 0 undefined null NaN 0n均为false 其他类型均为true

注意空数组和空对象也为true

2.2 利用.toString()和String()方法转换为字符串

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(){}'  
}

这里注意null、undefined没有.toString()方法 注意空数组是空字符串

2.3 利用Number()、parseInt()、parseFloat()方法转换为数值类型

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
}

undefined 空和非空对象 非空数组 函数 非数字型字符串为NaN

null 空数组 false为0

true为1

Symbol类型的数据无法转换为数值型

三.数据类型判断

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

特殊的null使用typeof后是object类型

理论上function(){} typeof之后也应该是object类型

3.2 Object.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

利用Object.prototype.toString.call()可以准确的判别null类型

但是对于function(){} new Date() []无法准确判断出数据类型

3.3 instanceof主要用于判断对象的类型

console.log(2 instanceof Number)   // false
console.log(true instanceof Boolean)   // false
console.log('123' instanceof String)   // false
console.log(Symbol() 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

instanceof无法判断基本数据类型 且null undefined更加无法判断

3.4 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

3.5 总结

typeof可以精确判断基本数据类型

instanceof用于判断引用数据类型,但是函数和数组或者是日期类型使用instanceof的时候,判断为Array,Function,Date还是Object都是成立的,但是使用typeof只有函数可以判断为Function类型,其他都为Object类型。但是使用Object.prototype.toString.call()可以判断出new Date()就为date,function(){}就为function,数组就为array,对象就为object。

四.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(){}})) // "{}"