`truthy` and `falsy` in JavaScript
This article explains truthy and falsy in JavaScript.
YouTube Video
truthy and falsy in JavaScript
The concepts of truthy and falsy in JavaScript play a very important role, especially in the evaluation of conditional statements. Truthy and falsy determine whether a value is evaluated as true (true) or false (false), but these values are not necessarily the boolean true or false themselves.
Falsy Values
Falsy values refer to values that are evaluated as false in JavaScript conditionals. Below is a list of falsy values in JavaScript.
1const values = [
2 false,
3 0,
4 -0,
5 0.0,
6 -0.0,
7 0n,
8 -0n,
9 "",
10 null,
11 undefined,
12 NaN
13];
14
15for (let value of values) {
16 if (value) {
17 console.log(`${value} is truthy`);
18 } else {
19 console.log(`${value} is falsy`);
20 }
21}In this example, all falsy values will be output as "falsy". Below is a list of falsy values in JavaScript.
false—falsein Boolean type0— The number zero (decimal0.0is alsofalsy)-0— Negative zero (technically distinct from0, but stillfalsy)0n— Zero in BigInt type""— An empty string of length 0null— No value presentundefined— Undefined valueNaN— Not a Number
These values are treated as false in logical operations and conditional statements (such as if statements).
Truthy Values
All values that are not falsy are truthy. This refers to values that are evaluated as boolean true. In JavaScript, many values are treated as truthy.
1const values = [
2 true,
3 1,
4 -1,
5 "Hello",
6 " ",
7 [],
8 {},
9 function() {},
10 Symbol(),
11 Infinity
12];
13
14for (let value of values) {
15 if (value) {
16 console.log(value, ' is truthy');
17 } else {
18 console.log(value, ' is falsy');
19 }
20}In this code, truthy values are output as "truthy". For example, the following values are truthy.
true—truein Boolean type- Numbers (other than 0) — For example,
1and-1are alsotruthy. - Strings (non-empty) — for example,
"Hello"or" "(even strings with only spaces aretruthy) - Objects — Even empty objects (
{}) and arrays ([]) aretruthy. - Functions — Functions themselves are also
truthy. Symbol— Values of typeSymbolare alsotruthy.Infinity— Both positive and negative infinity aretruthy.
Applications of truthy and falsy
The concepts of truthy and falsy are often used in conditionals and logical operations. For example, when setting a default value using the || (logical OR) operator, the first truthy value is returned as the result.
How to check for falsy values
To check for falsy values, using the Boolean function is convenient. The Boolean function explicitly converts a given value to a boolean type.
Examples of the Boolean function
1const values = [
2 false,
3 0,
4 "",
5 null,
6 undefined,
7 NaN
8];
9
10for (let value of values) {
11 console.log(`${value} is ${Boolean(value) ? 'truthy' : 'falsy'}`);
12}Conclusion
Truthy and falsy in JavaScript are important for understanding how non-boolean values are evaluated in conditional expressions. Falsy values include false, 0, an empty string, null, undefined, and NaN, while all other values are evaluated as truthy. Understanding these concepts enables you to write more flexible and efficient code.
You can follow along with the above article using Visual Studio Code on our YouTube channel. Please also check out the YouTube channel.