 undefined和void
undefined和void
  # 前端中的 void 0 与 undefined 深度解析
 在 JavaScript 中,void 0 和 undefined 都表示"未定义"的概念,但它们在使用场景、行为特性和最佳实践方面存在重要差异。本文将全面解析这两者的区别和应用场景。
# 核心概念解析
# undefined 是什么?
 undefined 是 JavaScript 中的一个原始值,表示变量未被赋值或对象属性不存在。它是全局对象的一个属性(在浏览器中是 window.undefined)。
let unassigned;
console.log(unassigned); // undefined
const obj = {};
console.log(obj.nonExistentProperty); // undefined
1
2
3
4
5
2
3
4
5
# void 运算符是什么?
 void 是一个运算符,它计算给定的表达式,然后返回 undefined。语法为 void expression。
console.log(void 0); // undefined
console.log(void "hello"); // undefined
console.log(void (2 + 2)); // undefined
1
2
3
2
3
# 关键区别对比
| 特性 | undefined | void 0 | 
|---|---|---|
| 类型 | 原始值 | 运算符 | 
| 可重写性 | ES5前可被重写 | 不可重写 | 
| 作用域安全性 | 局部作用域可能被覆盖 | 始终安全 | 
| 代码压缩 | 9个字符 | 6个字符 | 
| 使用场景 | 常规开发 | 库开发、安全关键代码 | 
| 返回值 | 值本身 | 总是返回 undefined | 
# 为什么需要 void 0?
 # 1. 历史原因:undefined 可被重写
 在 ES5 之前(2009年前),undefined 可以被重新赋值:
// ES3 环境中(已过时但需了解)
undefined = "now I'm defined";
console.log(undefined); // "now I'm defined"
1
2
3
2
3
void 0 提供了一种始终获取真正 undefined 值的安全方式:
console.log(void 0); // undefined (始终安全)
1
# 2. 作用域安全性
即使在现代 JavaScript 中,局部作用域中仍可声明名为 undefined 的变量:
(function() {
  const undefined = "defined";
  console.log(undefined); // "defined"
  console.log(void 0); // undefined
})();
1
2
3
4
5
2
3
4
5
# 3. 代码压缩优势
在库开发中,使用 void 0 可以节省空间:
// 原始代码
if (value === undefined) { /* ... */ }
// 压缩后
if (a===void 0){/* ... */} // 更短
1
2
3
4
5
2
3
4
5
# 现代 JavaScript 中的使用
# ES5+ 环境下的变化
从 ES5 开始,全局 undefined 变为只读:
// 现代浏览器中
undefined = "new value"; 
console.log(undefined); // 仍然输出 undefined(严格模式下报错)
1
2
3
2
3
但在严格模式下,尝试修改 undefined 会报错:
"use strict";
undefined = "new value"; // TypeError: Cannot assign to read only property
1
2
2
# 最佳实践建议
- 常规开发:使用 - undefined更直观- function greet(name = "Guest") { if (name === undefined) { console.log("Hello, Guest!"); } else { console.log(`Hello, ${name}!`); } }1
 2
 3
 4
 5
 6
 7
- 库/框架开发:使用 - void 0确保绝对安全- // jQuery 源码示例 jQuery.extend = function() { var options, name, src, copy, copyIsArray, clone, target = arguments[0] || {}, i = 1, length = arguments.length, deep = false; // 使用 void 0 检查 undefined if (typeof target === "boolean") { deep = target; target = arguments[i] || {}; i++; } // ... };1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
- 立即返回 - undefined- // 在箭头函数中返回 undefined const noop = () => void 0; // 替代方案 const noopAlt = () => undefined;1
 2
 3
 4
 5
# 特殊应用场景
# 1. 阻止链接跳转
在 HTML 中,void 0 常用于阻止默认行为:
<a href="javascript:void(0)" onclick="doSomething()">
  点击不会跳转
</a>
1
2
3
2
3
# 2. IIFE 中的安全 undefined
 (function(undefined) {
  // 这里 undefined 是安全的
  const isUndefined = value => value === undefined;
  
  console.log(isUndefined()); // true
})(void 0);
1
2
3
4
5
6
2
3
4
5
6
# 3. 最小化副作用
// 只执行函数,忽略返回值
void fetchDataAndUpdateUI();
// 等同于
fetchDataAndUpdateUI();
undefined; // 但 void 更明确表示忽略返回值
1
2
3
4
5
6
2
3
4
5
6
# 性能比较
在现代 JavaScript 引擎中,undefined 和 void 0 的性能差异可以忽略不计:
// 性能测试
const iterations = 1e8;
console.time('undefined');
for (let i = 0; i < iterations; i++) {
  const test = undefined;
}
console.timeEnd('undefined');
console.time('void 0');
for (let i = 0; i < iterations; i++) {
  const test = void 0;
}
console.timeEnd('void 0');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
典型结果(Chrome 115):
- undefined: ~50ms
- void 0: ~55ms
差异微小,不应作为选择依据。
# 替代方案:安全访问 undefined
 # 1. 使用全局对象
// 浏览器环境
const safeUndefined = window.undefined;
// 通用方法
const getSafeUndefined = () => {
  const _undefined = void 0;
  return _undefined;
};
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 2. ES6 默认参数
function isUndefined(value = void 0) {
  return value === void 0;
}
1
2
3
2
3
# 总结与最佳实践
- 现代开发(ES5+): - 优先使用 undefined,更直观易读
- 在严格模式下,undefined是安全的
 
- 优先使用 
- 库/框架开发: - 使用 void 0确保绝对兼容性
- 特别支持旧浏览器时必要
 
- 使用 
- 特殊场景: - 需要绝对安全时用 void 0
- 需要最小化代码时用 void 0
- HTML 中阻止默认行为用 javascript:void(0)
 
- 需要绝对安全时用 
- 避免: - 不要使用 void 0代替undefined作为默认参数值
- 不要在现代项目中过度使用 void 0降低可读性
 
- 不要使用 
// 现代最佳实践示例
function modernExample(param) {
  // 检查 undefined
  if (param === undefined) {
    console.log("参数未提供");
  }
  
  // 安全访问可能被覆盖的环境
  const safeCheck = (function(undefined) {
    return value => value === undefined;
  })(void 0);
  
  console.log(safeCheck()); // true
}
// 库开发示例
function libraryExample(value) {
  // 使用 void 0 确保安全
  if (value === void 0) {
    return defaultValue;
  }
  return process(value);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
理解 void 0 和 undefined 的区别有助于编写更健壮的 JavaScript 代码,特别是在开发需要广泛兼容性的库和框架时。
编辑  (opens new window)
  上次更新: 2025/09/05, 09:36:06
