Reputation: 91
I was using this statement in my code to cache the jquery selector and this was causing an error in the console. The error was "Missing ; before statement"
var $medium-image-holder = $('#image_'+itemID_value);
where itemID_value
is a numeric value. This statement is inside a for loop
Out of curiosity and after trying various tricks to overcome this thing, I replaced the hyphen with underscore in the variable name.
var $medium_image_holder = $('#image_'+itemID_value);
Surprisingly this worked.
I want to know whether using hyphens in JavaScript variable names is not permitted. At least, I didn't know about this. Would be very helpful if someone clarifies.
Upvotes: 1
Views: 438
Reputation: 647
java script does not allow hyphen in variable name declaration variable name can only include alphabets numbers or underscore moreover variable name should only start with alphabet variable names cannot start with numbers
cheers
Upvotes: 0
Reputation: 12064
Your statement $medium-image-holder
is interpreted as $medium - image - holder
($medium minus image minus holder).
After interpreting this statement as an algebraic expression, you try to set the outcome of it to a value with another statement (=$('#image_'+itemID_value)
), which is not allowed. That is where your error message comes from.
Upvotes: 1
Reputation: 75317
MDC Guidelines for variable names state;
A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).
Upvotes: 3