Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  11] [ 5]  / answers: 1 / hits: 33916  / 13 Years ago, sat, june 18, 2011, 12:00:00

In javascript is there a difference between using



if (foo.length > 0) {
//run code involving foo
}


and



if (foo) {
//run code involving foo
}


If so, could someone please explain the difference and an example where they would not be the same?


More From » variables

 Answers
91

Here's an example where they are not the same:



var x = [];
alert(x? 'yes' : 'no'); // displays yes
alert((x.length > 0)? 'yes' : 'no'); // displays no

[#91648] Thursday, June 16, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
collinarnoldp

Total Points: 10
Total Questions: 122
Total Answers: 109

Location: Spain
Member since Thu, Dec 23, 2021
2 Years ago
;