大网数据广告活动
Web 标准与前端开发

SyntaxError: identifier starts immediately after numeric literal

· 52CE 我爱测

当标识符以数字开头时,JavaScript 会出现“identifier starts immediately after numeric literal”的异常。标识符只能以字母、下划线(\_)或美元符号($)开头。

当标识符以数字开头时,JavaScript 会出现“identifier starts immediately after numeric literal”的异常。标识符只能以字母、下划线(_)或美元符号($)开头。

错误信息

SyntaxError: Unexpected identifier after numeric literal (Edge)
SyntaxError: identifier starts immediately after numeric literal (Firefox)
SyntaxError: Unexpected number (Chrome)

错误类型

SyntaxError

什么地方出错了?

变量的名称(也称为标识符)需遵循特定的规则,而这些规则是你的代码必须遵循的!

JavaScript 标识符必须以字母、下划线(_)或美元符号($)开头。不能以数字开头!只有后续的字符可以是数字(0-9)。

示例

以数字文字开头的变量名

在 JavaScript 中,变量名不能以数字开头。以下表达式会失败:

const 1life = "foo";
// SyntaxError: identifier starts immediately after numeric literal

const foo = 1life;
// SyntaxError: identifier starts immediately after numeric literal

alert(1.foo);
// SyntaxError: identifier starts immediately after numeric literal

你需要重新命名变量以避免前导数字。

const life1 = "foo";
const foo = life1;

参见

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。