4

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?

Subhangi Raj
  • 55
  • 1
  • 5

2 Answers2

8

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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

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

 var s  = { '123': 'string'}
eddyP23
  • 6,420
  • 7
  • 49
  • 87