JAVASCRIPT-类型判断
March 9, 2026
一、typeof
- 字符串 -
'string' - 数值 -
'number' - 布尔值 -
'boolean' - undefined -
'undefined' - null -
'object' - 基本对象 -
'object' - 数组 -
'object' - 函数 -
'function' - 大数字 -
'bigint' - Symbol -
'symbol' - 包装类型 -
'object'
已複製!typeof("ABC") // 'string' typeof(123) // 'number' typeof(true) // 'boolean' typeof(undefined) // 'undefined' typeof(null) // 'object' typeof({}) // 'object' typeof([]) // 'object' typeof(function(){}) // 'function' typeof(12n) // 'bigint' typeof(Symbol()) // 'symbol' typeof(new String('')) // 'object'
二、instanceof
只可以判断对象类型(有原型的类型)
已複製!'' instanceof String // false let a = {} a instanceof Object // true {} instanceof Object // Uncaught SyntaxError: Unexpected token 'instanceof' let b = Object.create(null) b instanceof Object // false let c = Object.create({}) c instanceof Object // true
三、toString
已複製!// 用法 Object.prototype.toString.call([type]) // [object Type]
已複製!Object.prototype.toString.call(123) // [object Number] Object.prototype.toString.call("") // [object String] Object.prototype.toString.call(true) // [object Boolean] Object.prototype.toString.call(undefined) // [object Undefined] Object.prototype.toString.call(null) // [object Null] Object.prototype.toString.call({}) // [object Object] Object.prototype.toString.call([]) // [object Array] Object.prototype.toString.call(function(){}) // [object Function] Object.prototype.toString.call(12n) // [object BigInt] Object.prototype.toString.call(Symbol()) // [object Symbol]
已複製!// 取出对应类型 Object.prototype.toString.call([type]).slice(8, -1).toLocaleLowerCase() // type
四、constructor
不可以判断null, undefined,constructor可以被修改
已複製![].constructor === Array // true ''.constructor === String // true 12n.constructor === BigInt // true
五、isPrototypeof
用于判断一个对象是否是另一个对象的原型。
已複製!const obj = { name: "云牧", age: 18 } const arr = [1, 2, 3] const proto1 = Object.getPrototypeOf(obj) console.log(proto1.isPrototypeOf(obj)) // true console.log(Object.isPrototypeOf({})) // false console.log(Object.prototype.isPrototypeOf({})) // true 期望左操作数是一个原型,{} 原型链能找到 Object.prototype console.log(Object.getPrototypeOf(obj) === Object.prototype) // true console.log(Object.getPrototypeOf(arr) === Array.prototype) // true
六、Array.isArray
用于判断变量是否是数组
已複製!Array.isArray([]) // true Array.isArray(123) // false