Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: How to get name of the constant? [closed]

Tags:

javascript

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).

like image 229
collo21 Avatar asked Oct 28 '25 04:10

collo21


1 Answers

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

like image 54
nuway Avatar answered Oct 30 '25 14:10

nuway



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!