What is wrong with this:
var a = "1";
var b = {};
var b[a] = 'test';
According to this SO question, the above is valid. But var b[a] = 'test'
is generating this error in AngularJS (v1):
Uncaught SyntaxError: Unexpected token [
What is wrong with this:
var a = "1";
var b = {};
var b[a] = 'test';
According to this SO question, the above is valid. But var b[a] = 'test'
is generating this error in AngularJS (v1):
Uncaught SyntaxError: Unexpected token [
This line:
var b[a] = 'test';
is not valid, because the characters [
and ]
are not allowed in variable names.
If you are not wishing to declare a new variable on that line, but rather just assign a key/value pair to the object b
, you can just remove the var
:
b[a] = 'test'; //b now equals { "1": "test" }