• 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

null or undefined?

 
author
Posts: 284
35
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, we use null for a reference to no object. JavaScript gives us two "bottom" values: null and undefined.

When I first learned JavaScript, I dutifully tried to make a distinction between "intentionally non-existent" (null) and "non-existent for some other reason" (undefined). But I always ended up in pointless metaphysical discussions and gave up. Now I always use undefined, except of course, when an API gives me null.

I am curious how others feel about this.

Cheers,

Cay

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

Cay Horstmann wrote:Now I always use undefined, except of course, when an API gives me null.


I do the same -- undefined seems to capture the spirit of a, well, undefined value better than null; hair-splitting perhaps, philosophical perhaps, but a real distinction in my mind.

However, lots of code treats them interchangeably, and I cannot count the number of bugs I've had to deal with when code specifically checks for one or the other, but not both, in situations where a distinction is not made.

In this topic, I pointed out the fairly new nullish coalescing operator which helps a lot in this regard.
 
reply
    Bookmark Topic Watch Topic
  • New Topic