JavaScript变量名需要遵守下面的规则
1. 变量命名
- 必须使用英文字母或者下划线”_”或者”$”为开头;
- 变量名长度不能超过255个字符。
- 必须使用英文字母、数字、下划线组成,不能出现空格或制表符;
- 不能使用javascript关键字与javascript保留字;
- 不能使用JavaScript语言内部的单词,比如Infinity,NaN,undefined;
- 大小写敏感,也就是说x和X是不一样的两个标识符。
2. 关键字
关键字对于JavaScript程序有着特别的含义,它们可标识程序的结构和功能,所以,在编写代码时,不能用它们作为自定义的变量名或者函数名。表1列出了JavaScript的关键字。
break |
case |
catch |
continue |
default |
delete |
do |
else |
finally |
for |
function |
if |
in |
instance |
try |
return |
swith |
this |
throw |
new |
typeof |
var |
void |
while |
with |
3. 保留字
除了关键字,JavaScript还有一些可能未来扩展时使用的保留字,同样不能用于标识符的定义,表2列出了这些保留字。
abstract |
boolean |
byte |
char |
class |
const |
debugger |
double |
enum |
export |
extends |
final |
float |
goto |
implements |
import |
int |
interface |
long |
native |
package |
private |
synchronized |
public |
short |
static |
super |
protected |
throws |
transient |
volatile |
|
|
|
|