Skip to content

SyntaxError: Unexpected 令牌

错误信息

                Unexpected 令牌 'x'
              

快速摘要

JavaScript 找到 symbol 哪里它 shouldn't 是 — 通常 missing bracket, comma, or quote earlier in code.

为什么发生

  • Missing comma 之间项 in 数组 or 对象 literal

  • Unclosed 字符串, parenthesis, or bracket 从上一个行

  • Extra punctuation or stray character in code

最小示例

✗ 错误代码
const arr = [1, 2 3];
✓ 修复代码
const arr = [1, 2, 3];

comma 是 missing 之间 2 and 3 — parser sees 3 哪里它 expected comma or 关闭 bracket.

如何诊断

  • Look at 令牌 mentioned in 错误 and 行之前它

  • 检查 for missing commas, brackets, or quotes 附近 reported 位置

  • Use linter or formatter 到 highlight 语法 issue

如何修复

  • 添加 missing comma, bracket, or quote

  • 移除任何 extra or stray punctuation

  • 运行 code 通过 formatter (Prettier) 到 spot 结构 issues

如何预防

  • Use linter (ESLint) 到 catch 语法错误 as 你类型

  • 启用 editor 语法 highlighting and bracket matching

相关资源

相关术语

相关课程

相关练习

相关工具

← 返回语言