What\'s the best way of checking if an object property in JavaScript is undefined?
Most likely you want if (window.x)
. This check is safe even if x hasn't been declared (var x;
) - browser doesn't throw an error.
if (window.history) {
history.call_some_function();
}
window is an object which holds all global variables as its members, and it is legal to try to access a non-existing member. If x hasn't been declared or hasn't been set then window.x
returns undefined. undefined leads to false when if() evaluates it.