0

I want to declare a variable name via template literals How it is possible ?

let z = 3;

let test`${z}` = "hello"; //or = new obj() for example

console.log(test3);
Karim
  • 103
  • 4

2 Answers2

1

Reference: Template literals

It is not possible. Template literals are used for strings only.

It is same as: var 'string_var' = 'myval' // Not accepted.

But, you can do it for object properties.

Such as:

let z = 3;

let obj = {}
obj[`test${z}`] = "hello"

console.log(obj.test3);
Radical Edward
  • 2,082
  • 8
  • 19
0

Yes, is possible. This is my code, referencing: Can you use template literal for function name in javascript?

for (i = 1; i < 10; i++) {
  global[`list_${i}`] = require(`./LP_${i}_Top_Holder.json`);
}