var @foo = 'bar';
// SyntaxError: missing variable name.
{ '@foo' : 'bar' };
// SyntaxError: invalid label.
var obj = { '@foo' : 'bar' };
obj.@foo;
// TypeError: can't convert AttributeName to string
var obj = { '@foo' : 'bar' };
obj['@foo'];
// "bar"
Can anyone explain to me why the ‘@’ symbol is not allowed to be used in variable names and what I should be using it for?
It’s not reserved or special, it’s just not a valid javascript identifier character. For the same reason this works:
And this does not:
Valid javascript identifiers must start with a letter or
$, and may only contain letters, numbers,$, and_. Anything else in a property name will force you to use bracket notation.Related question.