0

How do I check if a given variable has a value? What is the JS equivalent of PHP's isset?

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

2 Comments

Is this how you do if it is not undefined? if (myVariable !== undefined) { // myVariable has not been assigned a value... }
@shummel: Sure, that would work. You could also do: if (!(myVariable === undefined)) { ... }
0
if(typeof variable === "undefined") { //code here } 

3 Comments

this will return true also for non existing variables
Non-existing variables probably don't have a value.
Non-existing variables don't exist, the question was about a given variable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.