Is there a way to retrieve a variable's name in JavaScript without it being part of an object? Here's an example of what I'm trying to achieve:
var obj = {//Declared in a global scope
init:function(){
var var1 = /*value from input*/;
var var2 = /*value from input*/;
doCode({name:/*variable name as a string, "var1"*/,val:var1});
doCode({name:/*variable name as a string, "var2"*/,val:var2});
function checkNumber(args){
if(typeof args.val != 'number')
console.error("Error variable '"+arg.name+"' is not a number!");
else
console.log("Variable '"+arg.name+"' is a number!");
}
}
}
obj.init();
checkNumberonce with one variable as its argument or the value is unique among some object you can access from the function, the answer is “no”. Why do you want to do this?var1andvar2declared ?thisreally is, that's why I asked ?data-labelattribute or something, then checkarg.valueand usearg.dataset.label. (Note that yourtypeofcheck probably doesn’t perform as intended if this is user input…)