JavaScript的语言类型
... 2025-7-10 小于 1 分钟
- 原始类型: Number 、 String 、 Boolean 、 Null 、 Undefined 、 Symbol(ES6) 、 bigInt(ES10)
- 引用类型: Object (如: Array 、 Funcion 、 Object 、 Date 等)
目前 JavaScript 有 8 种内置数据类型,除了 Object 之外的七种数据类型,都是值类型
我们用 typeof 检查它们的类型,都有它自身的数据类型, null 除外
object 是引用类型; 至于我们熟知的 Array , Function , Date , Math 等等,都是引用类型,归属 object 类型
当然引用类型中也有例外,用 typeof 检查 function 的时候,返回的是 function
typeof null 为 'object'的 bug
JavaScript 中的数据在底层是以二进制存储,比如 null 所有存储值都是 0
但是底层的判断机制,只要前三位为 0,就会判定为 object
所以才会有 typeof null === 'object'这个 bug。
String(1) === '1' // true
new String(1) === '1' // false
typeof String(1) // 'string'
typeof new String(1) // 'object'
1
2
3
4
2
3
4