How to test whether a javascript variable exists
Now and again, especially when working with different files and dependencies in Javascript, it's useful to be able to check whether a variable is defined without risk of getting a 'x is not defined' error. There is a neat trick to do it, which makes use of the javascript window object.
var foo = "bar";
if ( typeof( window[ 'foo' ] ) != "undefined" ) {
alert("foo is defined")
}
else {
alert("foo is undefined")
}
If you found this post interesting, you might also like our blog post, Clipping or adding a mask to a shape in Kinetic JS
