Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
84
rated 0 times [  91] [ 7]  / answers: 1 / hits: 108520  / 14 Years ago, tue, january 18, 2011, 12:00:00

The jQuery Core Style Guidelines suggest two different ways to check whether a variable is defined.



  • Global Variables: typeof variable === "undefined"

  • Local Variables: variable === undefined

  • Properties: object.prop === undefined


Why does jQuery use one approach for global variables and another for locals and properties?


More From » jquery

 Answers
10

For undeclared variables, typeof foo will return the string literal undefined, whereas the identity check foo === undefined would trigger the error foo is not defined.



For local variables (which you know are declared somewhere), no such error would occur, hence the identity check.


[#94167] Monday, January 17, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tajo

Total Points: 415
Total Questions: 124
Total Answers: 103

Location: North Korea
Member since Tue, Jun 16, 2020
4 Years ago
;