JavaScript 数据类型

JavaScript 中所有的值都属于特定的数据类型。理解数据类型是编程的基础——不同类型决定了数据能做什么、不能做什么。

两种大类

JavaScript 的数据类型分为原始类型(Primitive)和引用类型(Reference):

原始类型(7 种)

类型示例说明
string'hello'字符串:文本
number42, 3.14数字:整数和浮点数
booleantrue, false布尔值:是 / 否
nullnull空值:故意设置为”没有值”
undefinedundefined未定义:变量声明了但未赋值
symbolSymbol('id')符号:唯一标识符(较少用)
bigint9007199254740991n大整数:表示超大整数

引用类型(1 种 + 子类型)

类型示例说明
object{ name: '张三' }对象:键值对的集合
array[1, 2, 3]数组(object 的子类型)
functionfunction() {}函数(可调用的 object)

原始类型和引用类型的根本区别:原始类型按比较和存储(复制时产生独立副本),引用类型按引用比较和存储(复制时共享同一个数据)。

typeof — 判断数据类型

typeof 运算符返回一个表示数据类型的字符串:

javascript
document.write(typeof 'hello' + '<br>');      // "string"
document.write(typeof 42 + '<br>');           // "number"
document.write(typeof true + '<br>');         // "boolean"
document.write(typeof undefined + '<br>');    // "undefined"
document.write(typeof null + '<br>');         // "object" ← 这是一个历史遗留 bug!
document.write(typeof Symbol() + '<br>');     // "symbol"
document.write(typeof 9007199254740991n + '<br>'); // "bigint"
document.write(typeof {} + '<br>');           // "object"
document.write(typeof [] + '<br>');           // "object"
document.write(typeof function(){} + '<br>'); // "function"

typeof null 返回 "object" 是 JavaScript 最早版本中的一个错误,因为历史原因被永久保留。判断一个值是否为 null 应使用 value === null

字符串(String)

字符串是文本数据,用引号包裹:

javascript
const name = '张三';         // 单引号
const greeting = "你好";     // 双引号
const template = `你好,${name}`; // 模板字面量(反引号)
document.write(name + '<br>');
document.write(greeting + '<br>');
document.write(template + '<br>');

单引号和双引号功能完全相同,选择一个风格后保持一致即可。模板字面量(反引号)支持嵌入变量和表达式:

javascript
const a = 10;
const b = 20;
document.write(`${a} + ${b} = ${a + b}`); // "10 + 20 = 30"

数字(Number)

JavaScript 只有一种数字类型——number,整数和浮点数不做区分:

javascript
const integer = 42;
const float = 3.14;
const negative = -10;
const scientific = 1.5e6; // 1.5 × 10⁶ = 1,500,000
document.write(integer + '<br>');
document.write(float + '<br>');
document.write(negative + '<br>');
document.write(scientific + '<br>');

JavaScript 的数字基于 IEEE 754 双精度浮点标准,有两个重要的局限性:

javascript
// 精度限制
document.write('0.1 + 0.2 = ' + (0.1 + 0.2) + '<br>'); // 0.30000000000000004(不等于 0.3!)
document.write('0.1 + 0.2 === 0.3: ' + (0.1 + 0.2 === 0.3) + '<br>'); // false

// 安全整数范围
document.write('MAX_SAFE_INTEGER: ' + Number.MAX_SAFE_INTEGER); // 9007199254740991

超出安全范围的整数应使用 BigInt

布尔值(Boolean)

只有两个值:truefalse。用于条件判断:

javascript
const isLoggedIn = true;
const hasPermission = false;

if (isLoggedIn) {
  document.write('欢迎回来');
}

nullundefined

这两个值都表示”没有”,但含义不同:

javascript
// undefined:声明了但未赋值(系统自动给)
let x;
document.write('x: ' + x + '<br>'); // undefined

// null:主动表示"空"(开发者设置)
let user = null; // 明确表示"当前没有用户"
document.write('user: ' + user); // null
javascript
// 函数没有返回值时返回 undefined
function noReturn() {}
document.write('noReturn(): ' + noReturn() + '<br>'); // undefined

// 访问对象中不存在的属性返回 undefined
const obj = {};
document.write('obj.missing: ' + obj.missing); // undefined

简单区分:undefined 是”系统自动给的没东西状态”,null 是”开发者主动设为空”。检查时使用 == null 可以同时匹配两者;严格区分时使用 === null=== undefined