4

I want to get the name of constant :

const MY_CONSTANT = 1;

I want to show the name of my constant (MY_CONSTANT) not value (1).

collo21
  • 105
  • 1
  • 10

1 Answers1

7

You cannot get the name of the constant or a variable in JavaScript. The closest thing to what you want to do would be setting a property inside an object. Then, you can get the names of all keys.

var obj = { myFirstName: 'John' };
obj.foo = 'Another name';
for(key in obj)
    alert(key + ': ' + obj[key]);

See karim79's answer here

Community
  • 1
  • 1
nuway
  • 2,324
  • 4
  • 27
  • 48