3

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?

2 Answers2

4

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
  • 351,820
  • 24
  • 303
  • 358
1

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

 var s  = { '123': 'string'}
eddyP23
  • 5,544
  • 6
  • 34
  • 78