On the contrary of @Thomas Eding answer:
If I forget to declare myVar
in my code, then I'll get myVar is not defined
.
Let's take a real example:
I've a variable name, but I am not sure if it is declared somewhere or not.
Then @Anurag's answer will help:
var myVariableToCheck = 'myVar';
if (window[myVariableToCheck] === undefined)
console.log("Not declared or declared, but undefined.");
// Or you can check it directly
if (window['myVar'] === undefined)
console.log("Not declared or declared, but undefined.");