• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between condition checks using if(myVar) and if(myVar=="")

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between the below condition checks in Java Script:
And

And when should be use either ?

Thanks
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, first of all if is a keyword and If is an identifier, so unless you declared a function named If, the second piece of code will throw an exception saying If is undefined.

I'm going to assume you meant to use if in the second snippet as well.

In the first code snippet, you check that myVar's value is truthy. This means that the value is coerced to boolean, and then checked if the result it true. Values false, null, undefined, "", 0 and NaN are falsy. The rest is truthy.

In the second code snippet, you check that myVar's value is equal to the empty string. If myVar is not a string itself, both operands will be coerced to a number and then the numbers will be compared. Because you used an empty string, the expression will return true if myVar is one of 0, "" or false.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the typo.

Thanks. Without typing anything in the text box ,I had clicked submit from my UI and checked using the Java Script condition .
Now I understand that it is the reason  why if(myVar) returned false. However what I fail to understand is why myVar!=null returned false in the same case.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because it's likely that if you submit an empty text, instead of containing a parameter with an empty value, the request will simply omit the entire parameter. The code that's responsible for handling the submission will then see undefined instead of "", and while the empty string is not null, undefined IS equal to null.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So , if the code thus sees myVar as undefined and undefined is equal to null then shouldn't if(myVar!=null) return false ? whereas to my surprise it returns true and returns false only if I instead do a if(myVar).
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want the empty string to be handled the same way as undefined and null, use this code:

If you want the empty string to be handled like any other non-empty string, use this code:

Finally, if you want to continue with a default value if the user submits an empty string, you can do something like this:
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:However what I fail to understand is why myVar!=null returned false in the same case.


Monica Shiralkar wrote:So , if the code thus sees myVar as undefined and undefined is equal to null then shouldn't if(myVar!=null) return false ?


Your first statement asserts that myVar != null returned false.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for mistake.Let me correctly mention.

If(myVar!=null) returned True
If(myVar!='') returned True
If(myVar) is the only one out of the three which returned False.

So the only thing which I did not understand  is that why did the first statement above i e  If(myVar!=null) return  True.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that's only possible if myVar = NaN.

Please show us where you declare and use myVar.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.This is the code
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, above your if statement, can you add console.log(searchKeyword) and also console.log(typeof searchKeyword) and tell us what your developer tools display?
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:So, above your if statement, can you add console.log(searchKeyword) and also console.log(typeof searchKeyword) and tell us what your developer tools display?



Result of statement console.log("searchKeyword"+searchKeyword) ;
Is
searchKeyword

(No value printed )


Result for  console.log(typeof searchKeyword)
Is
String
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then your assertion that:

Monica Shiralkar wrote:If(myVar) is the only one out of the three which returned False.


is false.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am pasting the code as well as results below :

The below code


Returns the below on console :

search keyword
typeof searchKeyword)string
false


Below is the only way using which my code is working fine and thus displaying the alert.

 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah okay, I got confused because I thought "searchKeyword" was the value of your variable, but you are just logging it as a label.

So the value of myVar is an empty string. Then your assertion that:

Monica Shiralkar wrote:If(myVar!='') returned True


is false.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for that. Yes, after putting a console statement for that I can see that is coming true.

I replaced my original if statement with if (searchKeyword!='') and that too works fine. I had tried this before too but don't know what mistake I had done that time that I had not got the expected result. Thanks.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For my original question, I am thinking that if(mySearchParam) is a null check and since mine is an empty string and not null object , using if(mySearchParam) should not work in this case. But it works.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:I am thinking that if(mySearchParam) is a null check


As already pointed out earlier in this topic, it's not a null check, it's a false-y check. And the empty string is false-y.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.So it means that for my case both (searchKeyword!='')  and if(searchKeyword) would work and out of these (searchKeyword!='')  would be the more direct one which one can use in this case as we are dealing with string.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you specifically want to check against the empty string, then be explicit about it: Note the explicit operator: !==

If you want to check for any false-y value:

One should only rarely, and with good reasons and understanding, specifically check for null or undefined.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:If you specifically want to check against the empty string, then be explicit about it: Note the explicit operator: !==



Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic