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 |
|
|
|
|