-3

I can't figure out how to pass an ampersand in a variable (e.g Books & Cds). I tried Books%26Cds and Books \& Cds but with no success. I keep getting syntax error. What is the correct syntax? I googled for several hours but still no hope. Extract sample below.any help will do. thnks in advance.

function configureDropDownLists(category,allsalerentname) {
var Lessons_in_rhymes = ['Kids'];
var Books & Cds = ['Kids'];
.
.
.
}
kmaJJ
  • 11
  • 5
  • 1
    You can't have spaces or `&` in variable names. – Adam Konieska Mar 30 '16 at 13:45
  • You can't. Special characters are not allowed in variable names, because they are considered as characters used in functions (& is used for programming logic). Just use another name. – Jorrex Mar 30 '16 at 13:46
  • What are you trying to do? – Adjit Mar 30 '16 at 13:46
  • unfortunately, the `&` character is not a valid character to include in a variable name. http://stackoverflow.com/questions/1661197/what-characters-are-valid-for-javascript-variable-names – IanGabes Mar 30 '16 at 13:46

3 Answers3

2

Some characters are not allowed in variable names, ampersand and % are some of them. Have a look at the following post and answer: What characters are valid for JavaScript variable names?

Community
  • 1
  • 1
Kaspar Fenner
  • 161
  • 2
  • 7
0

As the comments indicate the & character is illegal as a variable identifier. Additionally, in your example, you also have spaces, which are also illegal in variable identifiers. But both the & and space characters are acceptable in a property identifier. While you can't have a variable containing these characters, you could have an object property, like this:

 var obj = {};
 var prop1 = "Books & Cds";
 obj[prop1] = "Kids";

You would then retrieve that property value with the same bracket notation used to create it. You can use the variable or you can use the literal value, either way, you must provide a string when using bracket notation:

 console.log(obj[prop1]);          // "Kids"
 console.log(obj["Books & Cds"]);  // "Kids"

Not sure if this will work for your use case, but it can be done.

Here's the code above, in a working example:

     var obj = {};
     var prop1 = "Books & Cds";
     obj[prop1] = "Kids";


     console.log(obj[prop1]);          // "Kids"
     console.log(obj["Books & Cds"]);  // "Kids"
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • What I've shown leverages bracket notation with objects (there is no dictionary type in JS) to create a solution whereby a property name can be dynamic AND contain these characters. As my answer states, it may not be appropriate for all use-cases, but it provides an insight into the flexibility of the JS language and how a good knowledge of its syntax can provide solutions that, on the surface, don't seem possible. Lastly, if you read my post, you would have read that I clearly stated that what I was showing was a property identifier, not a variable. – Scott Marcus Dec 08 '16 at 13:26
-1

its impossible, because some characters are illegal in variable names ( %, *, (, ), whitespace etc.)

JS variable name validator

János Farkas
  • 453
  • 5
  • 14
  • It's not impossible, as my answer explains. – Scott Marcus Dec 06 '16 at 14:23
  • its impossible, just you dont know what is variable name, and dictionary key ;) your variable name is obj, and its dict type... – János Farkas Dec 08 '16 at 08:53
  • First, there is no dictionary type in JavaScript - it's an object. Second, my variable is prop1. Third, bracket notation DOES allow for dynamic property names to be used, which is the entire point. Fourth, did I not clearly state that what I was showing was not a variable, but instead a property identifier? Lastly, does my answer show a workaround to solve the OPs problem? Yes, it does, hence what the OP wants to do is NOT impossible. – Scott Marcus Dec 08 '16 at 13:33