Loading...
Loading...
Compare original and translation side by side
const PI = 3.14159; // Immutable binding (preferred)
let count = 0; // Reassignable
var legacy = "avoid"; // Function-scoped (avoid)const PI = 3.14159; // 不可变绑定(推荐使用)
let count = 0; // 可重新赋值
var legacy = "avoid"; // 函数作用域(避免使用)| Type | Example | typeof |
|---|---|---|
| String | | |
| Number | | |
| Boolean | | |
| Null | | |
| Undefined | | |
| Symbol | | |
| BigInt | | |
| Object | | |
| 类型 | 示例 | typeof |
|---|---|---|
| 字符串 | | |
| 数字 | | |
| 布尔值 | | |
| 空值 | | |
| 未定义 | | |
| Symbol | | |
| BigInt | | |
| 对象 | | |
// Arithmetic
+ - * / % **
// Comparison (always use strict)
=== !== > < >= <=
// Logical
&& || !
// Nullish
?? ?.
// Assignment
= += -= *= /= ??= ||= &&=// 算术运算符
+ - * / % **
// 比较运算符(始终使用严格比较)
=== !== > < >= <=
// 逻辑运算符
&& || !
// 空值运算符
?? ?.
// 赋值运算符
= += -= *= /= ??= ||= &&=// Early return (preferred)
function validate(input) {
if (!input) return { error: 'Required' };
if (input.length < 3) return { error: 'Too short' };
return { valid: true };
}
// Switch with exhaustive handling
function getColor(status) {
switch (status) {
case 'success': return 'green';
case 'warning': return 'yellow';
case 'error': return 'red';
default: return 'gray';
}
}// 提前返回(推荐使用)
function validate(input) {
if (!input) return { error: '必填项' };
if (input.length < 3) return { error: '长度过短' };
return { valid: true };
}
// 带穷尽处理的Switch语句
function getColor(status) {
switch (status) {
case 'success': return 'green';
case 'warning': return 'yellow';
case 'error': return 'red';
default: return 'gray';
}
}// Explicit conversion (preferred)
Number('42') // 42
String(42) // "42"
Boolean(1) // true
// Truthy values: non-zero numbers, non-empty strings, objects
// Falsy values: 0, "", null, undefined, NaN, false// 显式转换(推荐使用)
Number('42') // 42
String(42) // "42"
Boolean(1) // true
// 真值:非零数字、非空字符串、对象
// 假值:0、""、null、undefined、NaN、false// Nullish coalescing
const name = input ?? 'Guest'; // Only null/undefined
// Optional chaining
const city = user?.address?.city; // Safe navigation
// Logical assignment
config.debug ??= false; // Assign if nullish// 空值合并运算符
const name = input ?? 'Guest'; // 仅当值为null/undefined时生效
// 可选链运算符
const city = user?.address?.city; // 安全导航
// 逻辑赋值运算符
config.debug ??= false; // 仅当值为nullish时赋值| Error | Cause | Fix |
|---|---|---|
| Variable not declared | Check spelling, scope |
| Accessing null/undefined | Use optional chaining |
| Invalid number operation | Validate input types |
Unexpected | Loose equality | Use strict |
| 错误 | 原因 | 解决方法 |
|---|---|---|
| 变量未声明 | 检查拼写和作用域 |
| 访问null/undefined值 | 使用可选链运算符 |
结果为 | 无效的数字运算 | 验证输入类型 |
意外的 | 使用了松散相等 | 使用严格相等 |
// 1. Check type
console.log(typeof variable);
// 2. Check value
console.log(JSON.stringify(variable));
// 3. Check for null/undefined
console.log(variable === null, variable === undefined);
// 4. Use debugger
debugger;// 1. 检查类型
console.log(typeof variable);
// 2. 检查值
console.log(JSON.stringify(variable));
// 3. 检查是否为null/undefined
console.log(variable === null, variable === undefined);
// 4. 使用调试器
debugger;function processNumber(value) {
if (typeof value !== 'number' || Number.isNaN(value)) {
throw new TypeError('Expected a valid number');
}
return value * 2;
}function processNumber(value) {
if (typeof value !== 'number' || Number.isNaN(value)) {
throw new TypeError('需要有效的数字');
}
return value * 2;
}const city = user?.address?.city ?? 'Unknown';
const callback = options.onComplete?.();const city = user?.address?.city ?? '未知';
const callback = options.onComplete?.();