0

We are using below line to export const types in reactjs

export const TEST_VARIABLE = 'TEST_VALUE'

But I want to declare like below

var module_name = 'TEST'
export const module_name + 'VARIABLE' = module_name + 'VALUE';

I tried many combination but it won't work. For example

var module_name = 'TEST'
export const {module_name + 'VARIABLE'} = module_name + 'VALUE';
export const [module_name + 'VARIABLE'] = module_name + 'VALUE';
Naisarg Parmar
  • 759
  • 8
  • 25
  • You can use template literals for it – Ahmad Suddle Sep 21 '20 at 13:53
  • 2
    You cannot form variable names with string operations, but you *can* form object property names. – Pointy Sep 21 '20 at 13:54
  • 2
    You cannot create variable name dynamically, instead you can try creating object and adding dynamic property – Yash Joshi Sep 21 '20 at 13:55
  • `var module_name = 'value'; const result = 'just_result' var i = 'result'; eval('var ' + module_name + i + '= ' +i + ';'); console.log("value0=" + valueresult); ` – Nbody Sep 21 '20 at 14:05

0 Answers0