JS 7 种类型

类型 typeof 结果
undefined let x 没赋值 undefined
null 空值 object(历史 bug)
boolean true / false boolean
number 423.14NaNInfinity number
bigint 123n(任意大整数) bigint
string 'hi'"hi"`hi` string
symbol Symbol('id') symbol
object {}[]、函数、Date 等 objectfunction

typeof 速查

typeof 42                    // 'number'
typeof 'hi'                  // 'string'
typeof true                  // 'boolean'
typeof undefined             // 'undefined'
typeof null                  // 'object'   ← 经典 bug,记住就行
typeof {}                    // 'object'
typeof []                    // 'object'   ← 数组也是 object
typeof function(){}          // 'function'
typeof Symbol()              // 'symbol'
typeof 10n                   // 'bigint'

数字的怪事

0.1 + 0.2                    // 0.30000000000000004 (浮点精度)
Number.MAX_SAFE_INTEGER      // 2^53 - 1 = 9007199254740991
NaN === NaN                  // false(NaN 不等于任何东西,包括自己)
isNaN(NaN)                   // true
Number.isNaN(NaN)            // true(更严格,不会因为字符串误判)

1 / 0                        // Infinity
-1 / 0                       // -Infinity
0 / 0                        // NaN

大整数用 bigint

const big = 9007199254740993n;
big + 1n                     // 9007199254740994n
big + 1                      // TypeError: 不能混 number 和 bigint

null vs undefined

undefined null
含义 "还没赋值" "显式空"
来源 引擎自动 程序员手动
typeof 'undefined' 'object'
== 比较 null == undefined → true 同上
=== 比较 严格区分 严格区分

实战经验:

  • 函数没返回 / 变量没赋值 → undefined
  • "我故意没值" → null
  • 现代代码常常统一只用 undefined

类型转换(隐式)

'5' + 3                      // '53'      (+ 遇字符串就拼接)
'5' - 3                      // 2          (- 强转数字)
'5' * '2'                    // 10
!!''                          // false      (!! 转布尔)
!!'hi'                        // true
!!0                           // false
!!1                           // true
!!null                        // false
!!{}                          // true       (任何对象 = truthy)

显式转换(推荐)

Number('42')                 // 42
Number('42px')               // NaN
parseInt('42px', 10)         // 42         (**总是带第二参数**)
parseFloat('3.14')           // 3.14

String(42)                   // '42'
(42).toString()              // '42'
(42).toString(2)             // '101010'   (二进制)

Boolean(0)                   // false
Boolean('')                  // false
Boolean('false')             // true       ★ 注意:'false' 字符串是 truthy

"falsy" 值(转 boolean 是 false)

false   0   -0   0n   ''   null   undefined   NaN

只有这 8 个是 falsy,其他全是 truthy(包括 {} [] '0' 'false' 等)。

检测类型的正确姿势

// 数组
Array.isArray([])             // true
Array.isArray('hi')           // false

// null
value === null

// undefined
value === undefined
typeof value === 'undefined'

// 对象(不含 null 不含数组)
value !== null && typeof value === 'object' && !Array.isArray(value)

// 数字(不是 NaN)
typeof value === 'number' && !Number.isNaN(value)
Number.isFinite(value)        // 非 NaN / Infinity

// 整数
Number.isInteger(42)          // true
Number.isInteger(42.0)        // true(其实就是整数)
Number.isInteger(42.5)        // false

现代 JS 实用辅助

// 可选链 ?.
user?.profile?.name           // 任一为 null/undefined 整体 undefined

// 空值合并 ??
const v = input ?? 'default'  // input 是 null/undefined 才用 default
                                // (比 || 严格:0 和 '' 不会被替换)

// 解构 + 默认
const { name = 'anonymous' } = user
const [first, ...rest] = arr

  • == 有诡异规则——永远用 ===
  • typeof null === 'object'——这是 JS 历史 bug,记住
  • 数字精度:金额计算用 string 或 bigint,别用 number
  • Number('') 返回 0(不是 NaN)—— 验证数字要小心

下一篇:函数与闭包。