Everything about null in JavaScript

Click for: original source

JavaScript has 2 kinds of types: primitives (strings, booleans, numbers, symbols) and objects. But there are situations when an object cannot be created. For such cases, JavaScript provides a special value null – which indicates a missing object. By Dmitri Pavlutin.

In this post, you’ll learn everything about null in JavaScript: its meaning, how to detect it, the difference between null and undefined, and why using null extensively creates code maintenance difficulties.

The article is split into:

  • The concept of null
  • Real-world analogy of null
  • How to check for null
  • null is falsy
  • typeof null
  • The trap of null
  • Alternatives to null
  • null vs undefined

Avoid if possible returning null or setting variables to null. This practice leads to the spread of null values and verifications for null. Instead, try to use objects with default properties, or even throw errors. And good to remember: typeof is misleading in case of null: typeof null evaluates to ‘object’. Good read!

[Read More]

Tags nodejs javascript programming code-refactoring