In JavaScript, truthy values are the values that are evaluated to be true when used in a Boolean context, such as in conditional statements or logical operations.
If you’ve ever wondered what JavaScript considers “true,” here’s the simple answer: any value that is not explicitly falsy (like false, 0, null, undefined, NaN, or an empty string "") is considered truthy. This means numbers (except 0), non-empty strings, objects, arrays, functions, and even some quirky values like "0" are all truthy.
List of Truthy Values in JavaScript
Here’s a detailed look at the most common truthy values in JavaScript:
Non-zero Numbers
Any number, whether positive or negative, is truthy.
JavaScript
if (42) console.log("This is truthy!");
if (-3.14) console.log("This is also truthy!");
OutputThis is truthy!
This is also truthy!
Non-Empty Strings
Any string with at least one character is truthy, including strings containing whitespace.
JavaScript
if ("hello") console.log("Non-empty strings are truthy!");
if (" ") console.log("Even whitespace is truthy!");
OutputNon-empty strings are truthy!
Even whitespace is truthy!
Objects
All objects (including empty objects {}) are truthy.
JavaScript
if ({}) console.log("An empty object is truthy!");
if ({ key: "value" }) console.log("Objects with properties are truthy!");
OutputAn empty object is truthy!
Objects with properties are truthy!
Arrays
Similarly, all arrays (including empty arrays []) are truthy.
JavaScript
if ([]) console.log("An empty array is truthy!");
if ([1, 2, 3]) console.log("Non-empty arrays are truthy!");
OutputAn empty array is truthy!
Non-empty arrays are truthy!
Functions
Functions are always truthy, even if they do nothing.
JavaScript
if (function() {}) console.log("Functions are truthy!");
OutputFunctions are truthy!
Dates
All JavaScript Date objects are truthy, regardless of their validity.
JavaScript
if (new Date()) console.log("Dates are truthy!");
Non-Zero BigInt Values
All BigInt values, except 0n, are truthy.
JavaScript
if (1n) console.log("BigInt is truthy!");
Symbols
All Symbol values are truthy.
JavaScript
if (Symbol("id")) console.log("Symbols are truthy!");
OutputSymbols are truthy!
To better understand how truthy values work in JavaScript, Let's see an example
JavaScript
const a = [
1, // Truthy (non-zero number)
-1, // Truthy (negative number)
"hello", // Truthy (non-empty string)
" ", // Truthy (whitespace string)
{}, // Truthy (empty object)
[], // Truthy (empty array)
function () { }, // Truthy (function)
new Date(), // Truthy (date object)
Symbol(), // Truthy (symbol)
10n // Truthy (BigInt)
];
a.forEach(value => {
if (value) {
// Handle Symbol conversion separately
const displayValue = typeof value === "symbol" ? value.toString() : value;
console.log(`${displayValue} is truthy.`);
}
});
Output1 is truthy.
-1 is truthy.
hello is truthy.
is truthy.
[object Object] is truthy.
is truthy.
function () { } is truthy.
Mon Dec 09 2024 08:00:36 GMT+0000 (Coordinated Universal Time) is truthy.
Sym...
Why Truthy Values Matter?
Understanding truthy values helps you write concise and readable code. For example:
Providing default values
JavaScript
let username
const user = username || "Guest";
console.log(user); // If username is falsy, prints "Guest"
Checking for existence
JavaScript
if (config && config.apiKey) {
console.log("API Key exists!");
}
Truthy values in JavaScript include any value that isn’t explicitly falsy. This simple rule makes JavaScript flexible but also requires attention to detail to avoid unexpected behavior. Remember, non-zero numbers, non-empty strings, objects, arrays, functions, symbols, and dates are all truthy—making JavaScript both powerful and occasionally quirky.
Related Articles
Similar Reads
JavaScript Output JavaScript provides different methods to display output, such as console.log(), alert(), document.write(), and manipulating HTML elements directly. Each method has its specific use cases, whether for debugging, user notifications, or dynamically updating web content. Here we will explore various Jav
4 min read
What is JavaScript? JavaScript is a powerful and flexible programming language for the web that is widely used to make websites interactive and dynamic. JavaScript can also able to change or update HTML and CSS dynamically. JavaScript can also run on servers using tools like Node.js, allowing developers to build entire
6 min read
Getting Started with JavaScript JavaScript is a lightweight, single-threaded, dynamically typed, interpreted programming language with object-oriented capabilities. Primarily JavaScript is used to add interactivity and dynamism to web pages. It breathes life into static HTML and CSS, enabling features like dynamic content updates,
8 min read
What is Math in JavaScript? JavaScript provides a built-in Math object that provides a set of methods to perform mathematical operations. These operations range from basic to more complex functions like trigonometry and logarithms. The Math object allows you to perform calculations without the need for writing custom functions
2 min read
JavaScript Quiz These JavaScript quiz questions are designed to help you test and enhance your knowledge of JavaScript, ranging from basic concepts to advanced features. These topic-specific quizzes provide a comprehensive way to practice and assess your understanding of JavaScript concepts.The JavaScript quizzes a
2 min read