0

Iam using following lines of code to check whether the JS variable 'userName' is null or not. But am always getting alert "not empty"

Please check my onready method:

$("document").ready(function (){
    var userName = "";
    if (userName == undefined || userName == null) {
        alert('empty');
    } else {
        alert('not empty');
        var div = document.getElementById('title b');
        div.innerHTML = div.innerHTML + ','; 
    }
});
2

5 Answers 5

1

You need typeof keyword to check it's type. !userName will take care of the rest, like null or empty values.

if ( typeof userName === 'undefined' || !userName ) {
    alert('empty');
}
Sign up to request clarification or add additional context in comments.

4 Comments

No need to use === here, because the return of typeof is always a string and == is faster in this case ;)
@Beterraba: some js linters tend to yell if non-strict equalities are used. So it's a habit for me. But in general you're right :)
Actually, can you explain the point of type check, since !undefined is true anyways?
I put it there to correct the original code chunk. Logically it has no use since userName gets initialized as an empty string, thus not undefined. But we never know what is going on behind the scenes.
1

You're setting var userName to "". It will never be null.

Declare your variable as var userName;, instead of var userName = "";

However, unless you actually do something with userName, the if / else will be pretty pointless.

4 Comments

so the null in json is equal to 'var userName' instead of 'var userName="" '???
A value of null in a JSON string will be parsed as null, not as an empty string, indeed.
so how do i detect 'null ' here (!userName)??
Simple: if(userName == null)
1

No your variable is neither undefined nor null.

What you could do though is this replace this line

if (userName == undefined || userName == null) {

by this line

if (!userName) {

3 Comments

If username === 0, that check will pass.
If the input is read from an html element, it will always be a string, right ?
Yes, but that is assuming it's actually read from a input element.
1

Empty string is not null nor undefined.

You can check using the ! operator:

if (!userName) {
   // Do the stuff
}

For completeness:

!"" == true
!null == true
!undefined == true
!"hi" == false

Note that:

!0 == true
!1 == false
!"0" == false
!"1" == false

2 Comments

If userName happens to be 0, !userName will be true; !1 -> false. Don't just cast the username to a boolean like that.
@Cerbrus As the var tend to be a string, I do not see the problem of this being a number 0. But yes, I'll expose this on the answer. Thank you for the reminder.
1

Try

var userName = "";
    if (userName.length < 1) {
        alert('empty' +"\n"+ userName.length);
    } else {
        alert('not empty');
        var div = document.getElementById('title b');
        div.innerHTML = div.innerHTML + ','; 
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.