Why does the following selector always report an error
var add-button=$('button[name=add]')
The error will not be reported if the variable name is changed
var add=$('button[name=add]')
Beginners encounter small problems when writing js, I hope someone can help me solve them
Variable names are case-sensitive and are allowed to contain letters, numbers, dollar signs ($), and underscores, but the first character is not allowed to be a number. Spaces and other punctuation marks are not allowed. JavaScript keywords and reserved words are not allowed in variable names. full name. add(-)button belongs to other symbols.
The two people above are right. It is best for beginners to understand the naming rules and habits
Identifier is a name used to identify a specific object. The most common identifiers are variable names, and function names to be mentioned later. JavaScript language identifiers are case-sensitive, so a and A are two different identifiers.
Identifiers have a set of naming rules. Those that do not comply with the rules are illegal identifiers. The JavaScript engine will report an error when it encounters an illegal identifier.
Simply put, the identifier naming rules are as follows:
The first character can be any Unicode letter (including English letters and letters from other languages), as well as the dollar sign ($) and the underscore (_).
The second character and subsequent characters, in addition to Unicode letters, dollar signs and underscores, can also use numbers 0-9.
The following are legal identifiers.
The following are illegal identifiers.
Chinese is a legal identifier and can be used as a variable name.
JavaScript has some reserved words that cannot be used as identifiers: arguments, break, case, catch, class, const, continue, debugger, default, delete, do, else, enum, eval, export, extends, false, finally, for ,function,if,implements,import,in,instanceof,interface,let,new,null,package,private,protected,public,return,static,super,switch,this,throw,true,try,typeof,var,void , while, with, yield.
In addition, although there are three words that are not reserved words, they should not be used as identifiers because they have special meanings:
Infinity, NaN, and undefined
.The identifier does not conform to the specification
in the middleadd-button
The-
illegal charactersIt’s best to use _, don’t use -