JavaScript 数据类型
primitive and 引用类型 in JavaScript, and 如何 typeof helps 你 inspect 它们.
你将学到
- seven primitive 类型: 字符串, 数字, 布尔值, 空, undefined, symbol, bigint
- 引用类型: 对象 (包含数组 and 函数)
- 如何 typeof behaves (and 它的 famous quirk 使用空)
- 类型 coercion: implicit 对比 explicit conversion
- difference 之间空 and undefined
概念讲解
Primitives vs References
JavaScript values are divided into primitives and objects.
Primitives (7 types)
字符串— text, like"hello"or'世界'数字— integers and floats alike, like42or3.14布尔值—真or假空— an intentional "否值"undefined— a variable that has not been assigned a valuesymbol— a unique, immutable identifier (ES6)bigint— integers of arbitrary precision, like9007199254740993n
Primitives are immutable — when you "更改" a string, you are actually creating a new one.
Objects (the reference type)
Everything that is not a primitive is an object: plain objects, arrays, functions, dates, regexps, maps, sets, and more. Objects are mutable and stored by reference, so two variables can point at the same object.
const a = [1, 2, 3];
const b = a; // b points to the SAME array
b.push(4);
console.log(a); // [1, 2, 3, 4] — a changed too!
The typeof Operator
typeof returns a string describing the type. It works for all primitives except one famous quirk:
typeof "hi" // "字符串"
typeof 42 // "数字"
typeof true // "布尔值"
typeof undefined // "undefined"
typeof Symbol() // "symbol"
typeof 10n // "bigint"
typeof {} // "对象"
typeof [] // "对象" ← arrays are objects
typeof null // "对象" ← a historic bug, never fixed
typeof function(){} // "函数"
null vs undefined
undefinedmeans a variable has been declared but not assigned, or a function returned nothing.空is an explicit "否值" that you assign yourself to signal emptiness.
Use === 空 or === undefined (strict equality) to check, not == 空 loosely.
Type Coercion
JavaScript automatically converts types in some operations, which can be surprising:
"5" + 3 // "53" ← string concatenation
"5" - 3 // 2 ← numeric subtraction
0 == "" // true ← loose equality coerces
0 === "" // false ← strict equality does not
Rule of thumb: always use === and !== (strict equality) to avoid coercion surprises. Convert types explicitly with Number(), String(), Boolean() when needed.
示例代码
// Inspect types with typeof
const samples = [
"hello",
42,
true,
null,
undefined,
{ name: "Ada" },
[1, 2, 3],
function () {},
10n,
Symbol("id"),
];
for (const value of samples) {
console.log(typeof value, "→", value);
}
// null vs undefined
let notAssigned;
console.log(notAssigned); // undefined
console.log(null); // null
console.log(typeof null); // "object" (historic bug)
// Strict vs loose equality
console.log(0 === ""); // false
console.log(0 == ""); // true ← avoid ==
typeof 是 quickest 方式到 inspect 值's 类型. Remember 空 quirk and 总是 prefer === over == 到 avoid implicit 类型 coercion.
动手试一试
- JSON Formatter
JavaScript 对象 and 数组是 JSON-如 — 格式 and inspect 它们.
- JSON Validator
验证结构的 JSON 数据你 构建 in JavaScript.
常见错误
错误写法
Using == 相反的 === and 获取 surprised 通过类型 coercion
正确做法
总是 use === and !== (strict equality). 它们做 不 coerce 类型, 所以 0 === '' 是假. Convert 类型 explicitly 当你 需要到.
错误写法
Expecting typeof 空到 是 '空'
正确做法
typeof 空返回 '对象' due 到 historic bug in 原始 JavaScript engine. 检查 for 空 explicitly 使用值 === 空.
相关资源
相关工具
- JSON 树 Viewer
Browse nested JavaScript 对象 visually.
相关速查表
- JavaScript Cheatsheet
数据类型引用 and conversion helpers.
练习 JavaScript 数据类型
28 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。