check truthy or falsy javascript

Check truthy or falsy JavaScript

Truthy and falsy are terms used to describe the Boolean value of an expression. In JavaScript, every value can be evaluated as either truthy or falsy.

Truthy Values:

  • Any non-empty string
  • Any number except 0 (including negative numbers and decimals)
  • true (boolean)
  • Any object
  • Arrays (even if empty)

Falsy Values:

  • 0
  • null
  • undefined
  • false (boolean)
  • '' (empty string)

To check if a value is truthy or falsy, you can use a simple if statement:

if (value) {
    // code to execute if value is truthy
} else {
    // code to execute if value is falsy

Note that the else block will be executed only if the value is falsy. If the value is truthy, the else block will be skipped.

You can also use the Boolean() function to convert a value to its Boolean equivalent (true or false):


This will return true if the value is truthy and false if it's falsy.

Another way to check for truthy or falsy values is to use the double negation (!!) operator:


This will return true if the value is truthy and false if it's falsy. The double negation operator converts any value to its Boolean equivalent.

It's important to keep in mind that the truthiness or falsiness of a value depends on its context. For example, a string with only whitespace characters is considered truthy if it's not empty:

if (' ') {
    console.log('This string is truthy');
} else {
    console.log('This string is falsy');

The above code will output "This string is truthy".

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]