If you are in a browser (i.e. not Node):
var varName = 'myVar'; if (typeof(window[varName]) == 'undefined') { console.log("Variable " + varName + " not defined"); } else { console.log("Variable " + varName + " defined"); }
However, let me say that it is very difficult for me to justify writing this code in a project. You need to know what variables you have if you do not expect people to write plugins in your code or something like that.
source share