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 + ','; 
    }
});
kemiller2002
  • 113,795
  • 27
  • 197
  • 251
Bangalore
  • 1,572
  • 4
  • 20
  • 50
  • I believe this is what you're asking: http://stackoverflow.com/questions/2647867/how-to-determine-if-variable-is-undefined-or-null – user3733165 Jun 17 '14 at 13:42
  • Possible duplicate of [How do you check for an empty string in JavaScript?](https://stackoverflow.com/questions/154059/how-do-you-check-for-an-empty-string-in-javascript) – T.Todua Dec 04 '18 at 09:33

5 Answers5

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.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
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
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Beterraba
  • 6,515
  • 1
  • 26
  • 33
  • 1
    If `userName` happens to be `0`, `!userName` will be `true`; `!1` -> `false`. Don't just cast the username to a boolean like that. – Cerbrus Jun 17 '14 at 13:44
  • @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. – Beterraba Jun 17 '14 at 13:46
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) {
axelduch
  • 10,769
  • 2
  • 31
  • 50
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');
}
Ingmars
  • 998
  • 5
  • 10
  • No need to use `===` here, because the return of `typeof` is always a string and `==` is faster in this case ;) – Beterraba Jun 17 '14 at 13:44
  • @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 :) – Ingmars Jun 17 '14 at 13:48
  • Actually, can you explain the point of type check, since `!undefined` is true anyways? – Beterraba Jun 17 '14 at 13:50
  • 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. – Ingmars Jun 17 '14 at 13:54
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 + ','; 
    }
guest271314
  • 1
  • 15
  • 104
  • 177