Languages
[Edit]
EN

JavaScript - Truthy and Falsy values

3 points
Created by:
Admin Dirask Community
4380

In this article, we would like to show you Truthy and Falsy values in JavaScript.

 

Falsy values / expressions
undefined
null (object)
false (boolean)

empty string e.g '', "", ``

zero number e.g 0, +0, -0, 0n
NaN (number)

Note:

Any other value / expression is truthy (returns true).

 

Practical example

In the below example, we check if a value is truthy or falsy using a simple if statement.

Runnable example:

// ONLINE-RUNNER:browser;

const x = {};

if (x) {
    console.log('Truthy');
} else {
    console.log('Falsy');
}

Note:

Change the x value and run the example to check if the value is truthy or falsy.

 

See also

  1. JavaScript - convert string to boolean

Alternative titles

  1. JavaScript - Truthy and Falsy expressions
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join