Checking javascript empty or null (is it correct?)

I use javascript to check if the input field is empty as follows:

if($("#nu_username").val() == null || $("#nu_username").val() == ""){ 
    do action...
}

This works well, but I'm wondering if it’s worth it to use both conditions. Do you know that?

+5
source share
2 answers

Both nulland ""are "false".

You can write

if (!$('#nu_username').val())
+10
source

Never compare with == or! = when true / false is involved. Always use === and! ==.

In addition, according to jQuery api , val () is guaranteed to return null only if there are no matches for this selector; and returns "" (empty string) if one field matches, but empty.

+1
source

All Articles