DEV Community

Cover image for Your values are 'Truthy' or 'Falsy', JavaScript
Milan Mohapatra
Milan Mohapatra

Posted on

2

Your values are 'Truthy' or 'Falsy', JavaScript

"Truthy" and "Falsy" value in JavaScript.



When I am taking about truthy and falsy value, I am talking about Boolean type in JavaScript. As computer only understand 0 or 1, true or false so, JVM also understand only truthy or falsy value.

What are these?

  • JVM only need to understand about truthy or falsy values, at the time of evaluation in Boolean context of expression or statement.
  • These truthy or falsy values may not be strictly Boolean true or false but it treated as Boolean value in conditional statement.

All 6 Falsy values

  1. Empty string (' ')
  2. Boolean false
  3. Numeric zero (0)
  4. NaN (Not a Number)
  5. null
  6. undefined

Except these 6 values all other values are treated as Truthy value in JavaScript.

console. log(0, Boolean(0)) // false
console. log(undefined, Boolean(undefined)) // false
console. log(null, Boolean(null)) // false
console. log(NaN, Boolean(NaN)) // false
console. log('', Boolean('')) // false
console. log(false, Boolean(false)) // false
Enter fullscreen mode Exit fullscreen mode
console.log(1, Boolean(1)) // true
console. log(-1, Boolean(-1)) // true
console. log('dct',Boolean('dct')) // true
console. log([], Boolean([])) // true
console. log({}, Boolean({})) // true
console. log(true, Boolean(true)) // true
Enter fullscreen mode Exit fullscreen mode

Correct me if I am wrong!!!

Linkedin | Twitter | GitHub

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