Maria Jane Maria Jane - 1 year ago 91
Javascript Question

unexpected token when building array of object using variable

Just feel strange why this will throw an error

var a = 'a';
var b = 'b';
const array = [{
a + "&" + b
}, ];


What I expect is just array
where a and b is variable.

Answer Source

you must name your object in array:

var a = 'a';
var b = 'b';
const array = [{objName: a + "&" + b}];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download