1

I'm curious if the code

var $d = $('#person_data[document_type]');
if ($d.length != 0) {
  if ($d.val().length != 0 ) {...}
}

that was taken from SO jQuery: checking if the value of a field is null (empty) is the best of the best?

Can I use

var $d = $('#person_data[document_type]');
if ($d.length) {
  if ($d.val().length) {...}
}

instead?

Which one is better?

Community
  • 1
  • 1
Haradzieniec
  • 9,086
  • 31
  • 117
  • 212

1 Answers1

2

The second example is fine, it relies on 0 being falsey. It will spare a few extra bytes going over the wire which is always a good thing. The two scripts will result in the same output, so I would take the more concise one.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189