How do I check if a given variable has a value? What is the JS equivalent of PHP's isset?
Asked
Active
Viewed 638 times
2 Answers
1
if (myVariable === undefined) {
// myVariable has not been assigned a value...
}
Also, this same question was asked and answered here on Stackoverflow. See, How can I determine if a JavaScript variable is defined in a page?

Community
- 1
- 1

Scott Mitchell
- 8,659
- 3
- 55
- 71
-
Is this how you do if it is not undefined? if (myVariable !== undefined) { // myVariable has not been assigned a value... } – sehummel Dec 23 '10 at 22:02
-
@shummel: Sure, that would work. You could also do: `if (!(myVariable === undefined)) { ... }` – Scott Mitchell Dec 23 '10 at 22:03
0
if(typeof variable === "undefined") {
//code here
}

Shurdoof
- 1,719
- 13
- 16
-
-
-
Non-existing variables don't exist, the question was about a given variable. – Dr.Molle Dec 24 '10 at 03:59