Understanding JavaScript Truthy and Falsy Values

JavaScript truthy and falsy values are slightly hard to understand, especially when you’re just a beginner.

Knowing what truthy values are is extremely important when it comes to writing strong and dependable JavaScript code.

Today, we will help you understand what truthy and falsy values mean in JavaScript.

What are “JavaScript truthy and falsy values”?

In JavaScript, a truthy value is treated as “true” when used in a Boolean context. On the other hand, a falsy value is treated as “false” in the same context.

It indicates that the values are considered either truthy (evaluated as true) or falsy (evaluated as false) depending on how they are evaluated in a Boolean context.

Every value in JavaScript is either true or false. Understanding truthiness helps us make decisions based on conditions and perform different operations in our code.

What are the examples of “truthy” values in js or JavaScript?

Here is an example of “truthy” values, which includes the following:

📌Numbers

Any non-zero number is considered truthy.

3 == '3';
3 == [3];
'3' == [3];

📌Strings

  • Non-empty strings are truthy.
  • The string zero “0” is truthy.
  • ‘false’ (a string containing the text “false”)

📌Objects

  • All objects, including arrays and functions, are truthy.
  • {} (an empty object)

📌Arrays
Non-empty arrays are truthy or [] (an empty array).

📌Functions

  • Any function is considered truthy.
  • function(){} (an “empty” function).

What are the examples of “falsy” values in js or JavaScript?

Here is an example of “falsy” values, which includes the following:

3 === '3';
3 === [3];
'3' === [3];

A clearer false outcome is obtained when using the strict equality operator (===) for comparisons.

📌 The number 0 (zero) and NaN (Not a Number).

  • A number or BigInt that represents zero (0, -0, 0.0, -0.0, 0n).

📌 The empty string (” “), (‘ ‘)

📌 The keyword value false.

📌 The special value null.

📌 The special value undefined.

📌 document.all

Please take a look at this example below:

var x = 0;

The number zero is “falsy.” However, the string zero “0” is truthy.

var x = 50 == 20;

This example is falsy.


var x = 10; 

var x = -10;

In JavaScript, any number that is not zero, including negative numbers, is considered truthy.

Here’s the complete example code of JavaScript truthy and falsy values:

let isMorning = false;
let isAfternoon = true;
let isEvening = false;

function example() {
  if (isMorning === false && isAfternoon === true && isEvening === false) {
    return `Good afternoon, itsourcecoders!`;
  } else if (isMorning === false && isAfternoon === true && isEvening === true) {
    return `Good afternoon, friend!`;
  } else if (isMorning === false && isAfternoon === false && isEvening === false) {
    return `Good night, friend!`;
  } else {
    return `Hello!`;
  }
}

console.log(example());

Output:

Good afternoon, itsourcecoders!

What are the common error while using Truthy values in JavaScript?

Using the equality operator (==)

When working with truthy values, it’s important to avoid common mistakes that can lead to unexpected outcomes.

One such mistake is using the wrong comparison operator when comparing values.

The equality operator in JavaScript can convert different types of values to make comparisons.

This can cause unexpected results when comparing truthy values that have different types.

For example:

if ('2' == 2) {
  console.log("Welcome to Itsourcecode!");
}

In this example, the string ‘2’ is converted into a number before the comparison is made.

Since both values are numerically equal, the condition is considered truthy, and the code block is executed.

Output:

Welcome to Itsourcecode!

Using double equals (==) vs. triple Equals (===)

To prevent unintended type coercion, it is generally advised to utilize the triple equals (===) operator when comparing values.

The triple equals operator verifies both the value and the type, guaranteeing a precise match.

For example:

if ('2' === 2) {
  console.log("Welcome to Itsourcecode!");
}

There’s is no output because the strict equality operator (===) compares both the value and the type of the values being compared.

In this particular situation, the string ‘2’ and the number 2 have different types. As a result, the condition is considered falsy, and the code block is not executed.

It should be like this:

if (2 === 2) {
  console.log("Welcome to Itsourcecode!");
}

Output:

Welcome to Itsourcecode!

Tips for JavaScript Truthy and Falsy Values

✅ Try to avoid comparing values directly.

✅ To prevent type conversion problems, use strict equality (===) or strict inequality (!==) comparisons when comparing values.

✅ Convert any value to a Boolean value by using either the Boolean constructor or the double-negative (!!) operator.

Conclusion

In conclusion, in this article, we have discussed JavaScript truthy and falsy values along with their examples.

Consequently, you can understand which expressions are considered truthy or falsy by the JavaScript interpreter.

Also, it helps you to enhance your code by utilizing your knowledge of truthy and falsy values, which leads to cleaner and more efficient code.

We are hoping that this article helps you in understanding the truthy JavaScript thoroughly.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Caren Bautista


Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel
 · View all posts by Caren Bautista →

Leave a Comment