Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top