Falsy values

2 snippets across 2 stacks - JavaScript, Python

JSJavaScript

Falsy Values

JS · Data Types
Syntax
false | 0 | -0 | 0n | "" | null | undefined | NaN
Example
const values = [false, 0, -0, 0n, "", null, undefined, NaN];
const truthyOnes = values.filter(Boolean);
console.log(truthyOnes.length);
Output
0

Note Everything else is truthy, including empty objects {}, empty arrays [], and the string "false". This trips up many developers.

PYPython

Truthiness Gotchas

PY · Common Mistakes
Syntax
# Falsy: None, False, 0, 0.0, '', [], {}, set()
Example
def process(data=None):
    # WRONG: if not data
    # This also catches empty list [], 0, and ""
    if not data:
        print("No data")  # Triggered by empty list too!

    # CORRECT: if data is None
    if data is None:
        print("Data is None")

process([])   # Might not intend to reject []
process(None)
process(0)
Output
No data
No data
Data is None
No data

Note Empty collections, zero, empty string, and None are all falsy. When you specifically mean 'no value provided', check 'is None' rather than relying on truthiness.

Frequently asked questions

How does JavaScript handle falsy values?
This task is covered in 2 stacks on this page: JavaScript, Python. The "Falsy Values" snippet in JavaScript uses `false | 0 | -0 | 0n | "" | null | undefined | NaN`.
Which code does the JavaScript example use?
The "Falsy Values" snippet uses `false | 0 | -0 | 0n | "" | null | undefined | NaN`, from the Data Types section of the JavaScript cheat sheet.
Which stacks cover "falsy values" on this page?
JavaScript, Python. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Falsy Values": Everything else is truthy, including empty objects {}, empty arrays [], and the string "false". This trips up many developers.