DEV Community

Lucas Paganini
Lucas Paganini

Posted on • Originally published at lucaspaganini.com

3 3

Falsy and Truthy in JavaScript


See this and many other articles at lucaspaganini.com

Falsy

Falsy values are values that are considered false when encountered in a boolean context. That means values that become false if you try to convert them to a boolean.

Boolean('');
//=> false

Boolean(0);
//=> false

Boolean(null);
//=> false
Enter fullscreen mode Exit fullscreen mode

The list grows over time, but currently, those are the falsy values in JavaScript:

  1. false
  2. 0 -0 0n representations of zero 3. ```""''` empty string
  3. null
  4. undefined
  5. NaN not a number
  6. document.all possibly

NOTE: document.all can also be falsy in a very specific edge case.


markdown
**document.all**
Objects are falsy if and only if they have the [[IsHTMLDDA]] internal slot.
That slot only exists in 'document.all' and cannot be set using JavaScript.


Enter fullscreen mode Exit fullscreen mode

Truthy

Truthy values are the opposite. They are values that are considered true when encountered in a boolean context.


ts
Boolean('abc');
//=> true

Boolean(1);
//=> true

Boolean([]);
//=> true


Enter fullscreen mode Exit fullscreen mode

All values that are not falsy, are truthy.

Conclusion

References are in the references.

We release web development tutorials every two weeks. Consider subscribing if you're interested in that.

Have a great day, and I'll see you soon!

References

  1. Truthy definition on MDN
  2. Falsy definition on MDN
  3. document.all edge case on MDN

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay