Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
152
rated 0 times [  155] [ 3]  / answers: 1 / hits: 120621  / 15 Years ago, thu, december 31, 2009, 12:00:00

Possible Duplicate:

Detecting an undefined object property in JavaScript




From the below JavaScript sample,


try {
if(jsVar) {
proceed();
}
}
catch(e) {
alert(e);
}

this jsVar is declared and initialized in another file.


The problem is that code throws undefined error when this code is executed before the other file (where its declared and initialized) is executed. That is why it is surrounded by try and catch.


What's the best way to handle this undefined error than try catch?


More From » javascript

 Answers
462

You can check the fact with


if (typeof jsVar == 'undefined') {
...
}

[#97955] Saturday, December 26, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kentrelle

Total Points: 333
Total Questions: 93
Total Answers: 95

Location: Vietnam
Member since Sun, Oct 18, 2020
4 Years ago
kentrelle questions
;