此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
SyntaxError: identifier starts immediately after numeric literal
当标识符以数字开头时,JavaScript 会出现“identifier starts immediately after numeric literal”的异常。标识符只能以字母、下划线(_)或美元符号($)开头。
In this article
错误信息
SyntaxError: Unexpected identifier after numeric literal (Edge)SyntaxError: identifier starts immediately after numeric literal (Firefox)SyntaxError: Unexpected number (Chrome)
错误类型
SyntaxError什么地方出错了?
变量的名称(也称为标识符)需遵循特定的规则,而这些规则是你的代码必须遵循的!
JavaScript 标识符必须以字母、下划线(_)或美元符号($)开头。不能以数字开头!只有后续的字符可以是数字(0-9)。
示例
>以数字文字开头的变量名
在 JavaScript 中,变量名不能以数字开头。以下表达式会失败:
js
const 1life = "foo";// SyntaxError: identifier starts immediately after numeric literalconst foo = 1life;// SyntaxError: identifier starts immediately after numeric literalalert(1.foo);// SyntaxError: identifier starts immediately after numeric literal你需要重新命名变量以避免前导数字。
js
const life1 = "foo";const foo = life1;