语法错误:标识符立即在数字文字之后开始

当标识符以数字开头时,会发生 JavaScript 异常 "标识符立即在数字文字之后开始"。标识符只能以字母、下划线 (_) 或美元符号 ($) 开头。

¥The JavaScript exception "identifier starts immediately after numeric literal" occurs when an identifier started with a digit. Identifiers can only start with a letter, underscore (_), or dollar sign ($).

信息

¥Message

SyntaxError: Invalid or unexpected token (V8-based)
SyntaxError: identifier starts immediately after numeric literal (Firefox)
SyntaxError: No identifiers allowed directly after numeric literal (Safari)

错误类型

¥Error type

SyntaxError

什么地方出了错?

¥What went wrong?

变量的名称(称为 identifiers)符合某些规则,你的代码必须遵守这些规则!

¥The names of variables, called identifiers, conform to certain rules, which your code must adhere to!

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

¥A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($). They can't start with a digit! Only subsequent characters can be digits (0-9).

示例

¥Examples

以数字文字开头的变量名称

¥Variable names starting with numeric literals

JavaScript 中变量名不能以数字开头。以下失败:

¥Variable names can't start with numbers in JavaScript. The following fails:

js
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

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

¥You will need to rename your variable to avoid the leading number.

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

也可以看看

¥See also