Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Is there a better way to check if variable is null value or not in JavaScript?

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am using only below way to check if variable is null or not.
But is there any better to check in node js?

var a = '' ;
if (a == '') {}

 
Saloon Keeper
Posts: 12630
273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code doesn't check that a is null. It's possible to assign certain values to a so that your condition will evaluate to true anyway, the most obvious of which you have demonstrated yourself by assigning the empty string to a.

There are three approaches you can take, depending on what a represents and what you want your application to do with it:

1) Check that a is exactly null: if (a === null) {

2) Check that a is similar to null: if (a == null) {

3) Check that a is falsey: if (!a) {

The most common approach is the third.
 
author
Posts: 284
35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It may be that the most common approach is to check if (!a), but it does not mean that it is a good approach.

That test also passes if a is 0 or "", which can really bite you if that's not what you expect.

If you write what you mean, your code will work and it will be comprehensible. If you need to check for null, the best solution is if (a === null).

Cheers,

Cay
 
Marshal
Posts: 67497
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cay Horstmann wrote:That test also passes if a is 0 or "", which can really bite you if that's not what you expect.


I have scars to show for that one...

To add another twist to the discussion: frequently null (or undefined) is checked for in order to provide a default if a value is null/undefined. A very recent addition (check your environment for compatibility) is called nullish coalescing and goes like so:

whose value is a if a is not null and not undefined, b otherwise.

This is nice because it avoids the zero and empty string pitfalls that Cay pointed out.
 
SunilK Chauhan
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks.
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic