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
相关资源
相关课程
- JavaScript Overview
学习 JavaScript 基本
相关练习
- 查找 Arrow 函数错误
练习 spotting 语法错误 in 函数.
相关工具
- JSON Formatter
格式 and 验证 JSON 到 catch 语法错误.
- JSON Validator
验证 JSON 语法到 查找 unexpected 令牌.