-4

Among the following two code, which is the best method to check a null item in jquery. What are the differences? or can you suggest any other best method?.

if(!$('#id').val()){
  //code
}

or

if($('#id').val()==''){
  //code
}
Nidheesh
  • 802
  • 1
  • 21
  • 44

2 Answers2

0

"" is by default a falsy value, and .val() if element exists and the value is blank will return ""

!"" will equate to true so your first approach should be just fine.

void
  • 36,090
  • 8
  • 62
  • 107
-1

I think this below method is best

if($('#id').val()==''){
  //code
}

And Also Try this also

if($('#id').val().length == 0){
      //code
    }