Jern
Jern

Reputation: 33

Object shorthand notation { a, b } is accepted by some browsers, but not others – is this valid JavaScript?

My very simple question is that I want to know if the following code is legal:

var a = 1;
var b = 2;
var c = {a,b};

Backstory: I can use the above code in Chrome, but IE 11 does not seem to work with this. Is there a similar (i.e. concise) way to do this that will work in all (recent) browsers?

Upvotes: 0

Views: 60

Answers (2)

JMM
JMM

Reputation: 26827

It's valid ES2015. Next most concise thing for engines that don't support that is regular {a: a, b: b}.

Upvotes: 5

baao
baao

Reputation: 73251

That's ecmascript 6 style object declaration. I'm not sure if IE will ever implement modern standards, but at the time being now, use this instead:

var c = {a:a,b:b}

Upvotes: 1

Related Questions