I believe there are a number of incorrect answers to this topic. Contrary to common belief, "undefined" is NOT a keyword in JavaScript and can in fact have a value assigned to it.
Additionally, myVar === undefined will raise an error in the situation where myVar is undeclared.
The most robust way to perform this test is:
This will always return the correct result, and even handles the situation where myVar is not declared.
//
DO NOT USE t
he following bad examples, they are degenerate!
var undefined = false; // Shockingly, this is completely legal!
var myVal;
if (myVar === undefined) {
alert("You have been misled. Run away!");
}
if (myVar == undefined) {
alert("You have been misled. Run away!");
}
The most robust way to perform this test is:
if (typeof myVar === "undefined")
No comments:
Post a Comment