Subhangi Raj
Subhangi Raj

Reputation: 65

Unexpected template string when Creating Object

I create a Object like below,

var s = {
  `1234`:`string`
}

But, It will throw the below Error,

Uncaught SyntaxError: Unexpected template string

How Can I create Such Elements?

Upvotes: 5

Views: 4854

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386654

You could use computed property names with brackets, because the template literal returns a string, which works like a variable.

var object = { [`1234`]:`string` };

console.log(object);

Upvotes: 9

eddyP23
eddyP23

Reputation: 6825

You should be using quotes instead of backticks, try this:

 var s  = { '123': 'string'}

Upvotes: 1

Related Questions