Skip to content

Missing Semicolon

快速摘要

你 omitted semicolon and JavaScript's automatic insertion guessed 错误, causing unexpected behavior.

为什么发生

  • Omitting semicolons at 端的 语句

  • 开始行 使用 [ or (之后语句不使用 semicolon

  • Relying on ASI in ambiguous 上下文

最小示例

✗ 错误代码
const a = 5
const b = 10
[a, b].forEach(console.log) // ASI fails — a is 5, then [a, b] is misread
✓ 修复代码
const a = 5;
const b = 10;
[a, b].forEach(console.log); // Semicolons make boundaries clear

Without semicolons, JavaScript may interpret const b = 10[a, b] as a single expression (accessing index [a, b] on 10). Adding semicolons makes statement boundaries explicit.

如何诊断

  • 检查如果错误消息 mentions unexpected 令牌

  • Look at 行之前错误 — missing semicolon 那里可能是 cause

  • 运行 linter 那个 enforces semicolons

如何修复

  • 添加 missing semicolon at 端的 语句

  • Use formatter (Prettier) 那个添加 semicolons automatically

  • 从不开始行 使用 [ or (不使用 semicolon on 上一个行

如何预防

  • 总是 use semicolons explicitly

  • Use linter and formatter 那个 enforce semicolon 样式

相关资源

相关术语

相关课程

← 返回语言