js中判断各种数据类型
1.数据类型的分类
基本类型:
字符串(String)
数字(Number)
布尔(Boolean)
空(Null)
未定义(Undefined)
Symbol (开发还未涉及到这个类型,详细介绍可看->博文)
引用类型:
对象(Object)
数组(Array)
函数(Function)
正则(RegExp)
日期(Date)
2.数据类型的判断
a、typeof xxx 进行判断数据类型
typeof "123" // 返回 "string"
typeof 123 // 返回 "number"
typeof false // 返回 "boolean"
typeof null // 返回 "object"
typeof undefined // 返回 "undefined"
typeof Symbol("1") // 返回 "symbol"
typeof [1,2,3,4] // 返回 "object"
typeof {
name:'John', age:34} // 返回 "object"
typeof function a(){
} // 返回 "function"
typeof /^[0-9]$/ // 返回 "object"
typeof new Date() // 返回 "object"
注:关于typeof null 的值为“object”,可查看->博文
typeof可以判断的数据类型有:string、number、boolean、undefined、symbol、function
b、A instanceof B 进行判断数据类型
"123" instanceof String // false
"123a" instanceof String // false
aa = new String('1123a')
aa instanceof String // true
123 instanceof Number // false
aa = new Number(123)
aa instanceof String // true
false instanceof Boolean // false
aa = new Boolean(false)
aa instanceof Boolean // true
null instanceof Null // Uncaught ReferenceError: Null is not defined
undefined instanceof Undefined // Uncaught ReferenceError: Undefined is not defined
Symbol("1") instanceof Symbol // false
aa = new Symbol("1") // Uncaught TypeError: Symbol is not a constructor
[1,2,3,4] instanceof Array // true
{
name:'John', age:34} instanceof Object // Uncaught SyntaxError: Unexpected token - ':'
aa = {
name:'John', age:34}
aa instanceof Object // true
function a(){
} instanceof Function // Uncaught SyntaxError: Unexpected token - 'instanceof'
function a(){
}
a instanceof Function // true
/^[0-9]$/ instanceof RegExp // true
new Date() instanceof Date // true
A instanceof B 总结:
1)不能判断基本数据类型,
2)判断引用数据类型时,若B为Object,返回结果均为true
3)若A为Array 、RegExp、Date可以直接进行判断,若A为Object 、Function,需要先声明,再进行判断
c、Object.prototype.toString.call(xxx) 进行判断数据类型
Object.prototype.toString.call("123") // 返回 "[object Number]"
Object.prototype.toString.call(123) // 返回 "[object String]"
Object.prototype.toString.call(false) // 返回 "[object Boolean]"
Object.prototype.toString.call(null) // 返回 "[object Null]"
Object.prototype.toString.call(undefined) // 返回 "[object Undefined]"
Object.prototype.toString.call(Symbol("1")) // 返回 "[object Symbol]"
Object.prototype.toString.call([1,2,3,4]) // 返回 "[object Array]"
Object.prototype.toString.call({
name:'John', age:34}) // 返回 "[object Object]"
Object.prototype.toString.call(function a(){
}) // 返回 "[object Function]"
Object.prototype.toString.call(/^[0-9]$/) // 返回 "[object RegExp]"
Object.prototype.toString.call(new Date()) // 返回 "[object Date]"
Object.prototype.toString.call(xxx) 可以判断所有的数据类型
d、isNaN(xxx)进行判断数据类型
isNaN('123') // false
isNaN('123a') // true
isNaN(123) // false
isNaN(false) // false
isNaN(true) // false
isNaN(null) // false
isNaN(undefined) // true
isNaN(Symbol('0')) // Uncaught TypeError:Cannot convert a Symbol value to a number(未捕获TypeError:无法将符号值转换为数字)
isNaN([1,2,3,4]) // true
isNaN({
name:'John', age:34}) // true
isNaN(function a(){
}) // true
isNaN(/^[0-9]$/) // true
isNaN(new Date()) // false
isNaN(xxx)可以判断的数据类型有:string、number、null、array、object、function、regExp
e、可以通过正则表达式去判断是否为数字
let reg= /^([0-9])*$/g
reg.test(1) // true
reg.test('1a') // false
reg.test('1') // true
reg.test(null) // false
reg.test(undefined) // false
reg.test([1]) // true
reg.test([1,2]) // false
reg.test({
name: 1}) // false
reg.test(function a(){
}) // false
reg.test(/^([0-9])*$/g) // false
reg.test(new Date()) // false