how to make a string a variable name javascript code example
Example 1: variable name to string javascript
//Get variable name as string
function getVariableName(v) {
for (var key in window) {
if (window[key] === v)
return key;
}
}
//testing
var someValue = "something not important right now";
console.log(getVariableName(someValue)); //>> prints "someValue"
//also works in functions
function print(a) {
console.log(getVariableName(a)); //>> prints "someValue" because of line 16
//you'd think it would print "a" but it doesnt
}
print(someValue);//>> look at line 14
Example 2: how to name a javascript variable
//variables can include any letter, any number, or the underscore
//NO SPACES!!! USE UNDERSCORES!!!
//variable names are case sensitive
//example:
var change_this = 'whatever you want here'
//change_this is a variable (change it to your variable name)
//'whatever you want here' is a string (you'll learn about this later)