Is it ok to create vars with spaces and forward slashes?
Like so:
var PICKUPS / TRUCKS = {};
No, variable names cannot have spaces, but some special characters are allowed.
To reference the list of valid characters you can refer to this answer.
In your case, this is invalid in Javascript:
// INVALID!
var PICKUPS / TRUCKS = {};
First, the / operator is the divide operator, so the Javascript interpreter will look at this like a Mathematical operation, trying to "divide PICKUPS with TRUCKS", which will cause an error, especially after the var keyword, it won't know how to make sense of that (for example, first it will see that you are trying to create a variable, and then it will see that instead of creating a variable, you are trying to do some math, which will confuse the javascript interpreter).
But you can do something like this:
// Valid Javascript; No spaces or illegal characters
var pickups_trucks = {};
Also, if the names are embedded in javascript objects, you can name objects using string identifiers, where anything legal in a string can work:
var Automobiles = {
"Trucks & Pickups": [],
"Three Wheelers": []
};
console.log(Automobiles["Trucks & Pickups"]);
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With