js数据类型分为:基本数据类型和引用数据类型,其中:
js的基本类型有: null、undefined、string、number、Boolean和Symbol
js的引用类型是:object
基本类型判断:
null
typeof null //输出:object
undefined
typeof x || typeof undefined //输出:undefined
number
typeof 1 //输出:number
string
typeof 'js' //输出:string
boolean
typeof true || typeof false //输出:boolean
Symbol
typeof Symbol(2) //输出:Symbol
注: null的类型判断为object,而不是null
引用类型使用typeof判断的情况
typeof [1,2] //数组,输出:object
typeof {a:1,b:2} //对象,输出:object
typeof new Number(2) //new Number(2),输出:object
typeof function(){} //函数,输出:function
注:
函数的类型判断是function
引用类型判断:
数组
[1,2] instanceof Object //输出:true
对象
var obj = {a:1,b:2}
obj instanceof Object //输出:true
函数
var fun = function(){} ;
fun instanceof Object //输出:true
null
null instanceof Object //输出:false
注:通过instanceof可知,null不是引用类型
原文:https://blog.csdn.net/qq_32862143/article/details/79029097