简体   繁体   中英

How do I use a Javascript keyword as a variable name?

If I want to use the following in JS, Is some option available to do it?

var break = "";

In C# we have the option to use it using @, is similar option available in JS

public int @public;

Well, if you insist, you can

var $break = 1;

which is the same as C# in terms of characters ;)

Seriously, you cannot use reserved keywords as variables, but they are allowed as property names:

myObj = {}
myObj.break = 'whatever';

Also, do note that the repertoire of the reserved words varies depending on the strictness. For example,

var interface = 1;

is valid in the non-strict mode, but breaks once you add use strict to your script.

您不能将Javascript关键字用作变量名。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM