JavaScript Truthy Falsy Visualizer
Inspect and visualize truthy/falsy values and type coercion in JavaScript
JavaScript Truthy Falsy Visualizer is a free online tool to explore how different values behave in Boolean contexts. Inspect types, constructors, and results of Number and String conversion.
What is JavaScript Truthy Falsy Visualizer?
JavaScript Truthy Falsy Visualizer is an educational and debugging tool that helps you understand how JavaScript evaluates different values in a boolean context. In JavaScript, a "truthy" value is a value that is considered true when encountered in a Boolean context, while a "falsy" value is considered false.
Understanding these concepts is critical for writing clean conditional logic and avoiding common bugs related to type coercion.
How to Use
- Enter an Expression: Type any JavaScript literal or expression into the input box (e.g.,
[],0,"",null). - View Summary: The tool immediately shows whether the value is TRUTHY or FALSY.
- Inspect Transitions:
- Type: See the result of the
typeofoperator. - Constructor: Identify the constructor function of the value.
- Coercion: Observe how the value transforms when explicitly converted using
Boolean(),Number(), andString().
- Type: See the result of the
- Use Presets: Click on the "Presets" tab to quickly explore famous "gotcha" cases in JavaScript.
Basic Knowledge: Truthy vs. Falsy
What are Falsy values?
There are exactly eight falsy values in JavaScript:
false: The keyword false.0: The number zero.-0: The number negative zero.0n: BigInt zero (if your environment supports it)."": An empty string.null: The absence of any value.undefined: A variable that has not been assigned a value.NaN: Not-a-Number.
What are Truthy values?
Everything else is truthy. This includes:
'0': The string containing "0" is truthy.'false': The string "false" is truthy.[]: An empty array is truthy.{}: An empty object is truthy.Infinity: Positive infinity is truthy.
Why Does This Matter?
Type coercion can lead to unexpected results in if statements. For example:
const myArray = [];
if (myArray) {
// This code WILL run because [] is truthy!
}
const myNumber = 0;
if (myNumber) {
// This code WILL NOT run because 0 is falsy.
}This tool helps you visualize these behaviors so you can write safer code, such as checking myArray.length > 0 instead of just if (myArray).
FAQ
Q: Is an empty array [] truthy or falsy?
A: An empty array is truthy. However, when compared with == 0, it becomes 0 due to coercion, which is a common source of confusion.
Q: Is NaN truthy?
A: No, NaN (Not-a-Number) is one of the eight falsy values in JavaScript.
Q: Does this tool run my code on a server?
A: No. All expressions are evaluated locally in your browser using the Function constructor. Your data never leaves your computer.
Q: Can I enter complex expressions like 1 + 2 * 3?
A: Yes! The tool can evaluate any valid JavaScript expression and show you the metadata of the resulting value.
Related Tools
- JavaScript Array Methods Explorer: Interactive guide and cheat sheet for JavaScript array methods
- JavaScript Fetch Code Generator: Generate fetch() or axios code snippets for API requests
- JS Minifier Beautifier: Minify or beautify JavaScript code online
- JavaScript Object Path Finder: Quickly find and copy dot-notation paths for properties in large JSON objects
- JavaScript String Escaper: Escape or unescape special characters in JavaScript string literals
- JSON to JSDoc Converter: Automatically generate JSDoc type definitions from JSON data