Monday, May 20, 2024
117
rated 0 times [  124] [ 7]  / answers: 1 / hits: 16364  / 12 Years ago, sun, july 8, 2012, 12:00:00

I want to boolean to come out of this expression



(task === undefined);


where task is arbitrary and doesn’t appear in the code at all.



However, when I run this in rhino, I get a reference Error. I WANT TRUE



Why don’t I get true?



I want to check if a particular variable has been defined. How do I do it then if this doesn't work?


More From » referenceerror

 Answers
2

Use this:



(typeof task === undefined)


When you use (task === undefined), Javascript needs to find the value of task to see if it is the same as undefined, but it can't look up the name because it doesn't exist, giving you the reference error. typeof is special in that it can safely return the type of a name that doesn't exist.


[#84401] Friday, July 6, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
parker

Total Points: 259
Total Questions: 109
Total Answers: 97

Location: Zambia
Member since Thu, Jun 25, 2020
4 Years ago
;