In JavaScript, every value has an inherent Boolean value, referred to as truthy or falsy. This determines whether the value is treated as true
or false
when evaluated in a Boolean context, such as an if
statement, loops, or ternary operators.
Falsy values are treated as false
when encountered in a Boolean context. There are exactly six falsy values in JavaScript:
false
– The Booleanfalse
itself.0
– The number zero.""
or''
– An empty string (both single and double quotes).null
– Represents the absence of any value.undefined
– A variable that has been declared but not assigned a value.NaN
– "Not-a-Number," typically the result of an invalid mathematical operation.
if (!false) console.log("Falsy: false");
if (!0) console.log("Falsy: 0");
if (!"") console.log("Falsy: Empty String");
if (!null) console.log("Falsy: null");
if (!undefined) console.log("Falsy: undefined");
if (!NaN) console.log("Falsy: NaN");
Output:
Falsy: false
Falsy: 0
Falsy: Empty String
Falsy: null
Falsy: undefined
Falsy: NaN
All other values apart from the six falsy values are considered truthy. These are values that are treated as true
in a Boolean context. Some common truthy values include:
- Non-zero numbers (e.g.,
42
,-1
,3.14
). - Non-empty strings (e.g.,
"hello"
,'false'
," "
– a string with just a space). - Arrays (e.g.,
[]
– even an empty array is truthy). - Objects (e.g.,
{}
– even an empty object is truthy). true
– The Booleantrue
itself.- Special values like
Infinity
and-Infinity
.
if (42) console.log("Truthy: Non-zero number");
if ("hello") console.log("Truthy: Non-empty string");
if ([]) console.log("Truthy: Array");
if ({}) console.log("Truthy: Object");
if (true) console.log("Truthy: true");
if (Infinity) console.log("Truthy: Infinity");
Output:
Truthy: Non-zero number
Truthy: Non-empty string
Truthy: Array
Truthy: Object
Truthy: true
Truthy: Infinity
Truthy and falsy values are integral to JavaScript's dynamic nature. They are used in conditions and logic checks to simplify code and handle edge cases. For example:
let name = ""; // Empty string (Falsy)
let defaultName = name || "Anonymous"; // Default to "Anonymous" if name is falsy
console.log(defaultName); // Output: Anonymous
let isLoggedIn = true; // Truthy value
if (isLoggedIn) {
console.log("Welcome back!");
}
Understanding truthy and falsy values in JavaScript helps in writing concise and efficient code. Knowing how JavaScript evaluates these values ensures fewer bugs and cleaner logic in your programs.