根据要判断的数据对象,来选用合适的方法
1、原始数据类型
6种:number、string、boolean、undefined、null、symbol
一、typeof
let str = 'hello';
console.log(typeof str); // string
【特殊!!】null的类型判断,不能使用typeof、instanceof、constructor
let s = null; console.log(typeof s) //Object
下面是两种判断是否为null类型的方法:
console.log(s === null)
console.log(Object.prototype.toString.call(s) === '[object Null]');
2、对于部分引用数据类型:object、function,也可以使用typeof
let obj = {
}
console.log(typeof obj); // object
function func