cod;nn

Python Truthiness vs JavaScript: A Friendly First Lesson

Python truthiness vs JavaScript 🐍

You already know JavaScript. Let's use that knowledge to learn how a value behaves inside an if in Python.

🟦 JavaScript: an empty task list

const tasks = [];
console.log(Boolean(tasks)); // true

Boolean(...) asks whether a value counts as true in a condition. An empty JavaScript array still counts as true.

🟩 Python: same task list, different result

tasks = []
print(bool(tasks))  # False

Python calls this collection a list. print() is like console.log(), and bool() asks the true-or-false question.

An empty Python list counts as false. Add an item:

tasks = ["Learn Python"]
print(bool(tasks))  # True

πŸ’‘ "Truthy" means "counts as true in a condition." "Falsy" means "counts as false."

Compare a few familiar values

ValueJavaScriptPython
Empty array/list: []trueFalse
Ordinary empty object / dictionary: {}trueFalse
Empty string: ""falseFalse
Zero: 0falseFalse
Text: "false"trueTrue

A dictionary is Python's key-value collection. We'll explore it later. The string "false" counts as true because it isn't empty; neither language turns that word into a boolean here.

🟨 Keep zero when the user chose zero

Imagine a task setting where no value means "show 20 tasks" and zero means "show no tasks."

JavaScript

const requestedLimit = 0;
const limit = requestedLimit || 20;
console.log(limit); // 20 β€” but we wanted 0

Python

requested_limit = 0
limit = requested_limit or 20
print(limit)  # 20 β€” but we wanted 0

Both examples choose 20 because zero counts as false. We need a more specific check: is the value missing?

For this example, JavaScript uses null for missing, and Python uses None.

🟦 JavaScript

const requestedLimit = 0;
let limit;

if (requestedLimit === null) {
  limit = 20;
} else {
  limit = requestedLimit;
}

console.log(limit); // 0

🟩 Python

requested_limit = 0

if requested_limit is None:
    limit = 20
else:
    limit = requested_limit

print(limit)  # 0

Read is None as "is this value None?" Python uses a colon and indentation where JavaScript uses braces. Use four spaces for the indented lines here.

Change the Python input to None and run it again. Now the result is 20.

You may know JavaScript's shorter requestedLimit ?? 20. It defaults for both null and undefined. Python doesn't have JavaScript's undefined; we're using None for missing in this example.

✍️ Try a small title helper

Let's return "Untitled" when no title was supplied, and keep other supplied titles as they are:

InputExpected result
None"Untitled"
""""
"Learn Python""Learn Python"

Keep the empty string for this exercise. A real app can choose different rules for empty titles.

Create practice.py and copy this starter:

def resolve_title(title):
    # Replace pass with your code.
    pass


print(repr(resolve_title(None)))
print(repr(resolve_title("")))
print(repr(resolve_title("Learn Python")))

def creates a function. pass means "do nothing for now." Replace it with two decisions:

  1. If title is None, return "Untitled".
  2. Otherwise, return title.

return sends the result back. repr() shows quotes around strings so you can see an empty one.

πŸ’‘ Hint: use the same if ... is None: check as the limit example.

Run your file:

python practice.py

Expected output:

'Untitled'
''
'Learn Python'

If you see None on each line, check whether you've replaced pass and added a return. You can refer to the lesson while you work.

πŸ’¬ Optional interview question

Why does value or 20 replace zero with 20? Try explaining it in your own words using the examples above.

βœ… Three things to remember

  • An empty Python list counts as false; an empty JavaScript array counts as true.
  • Zero and a missing value can mean different things.
  • is None checks specifically for None.

This is Lesson 1 of Python for Frontend Devs: Full-Stack Skills to Crack Interviews in the AI Era. We'll use this idea later in our task app's settings.

Read more in the Python truth-value guide and MDN's JavaScript Boolean reference.

Further Resources

How Long Should a Resume Be? One vs. Two Pages

How long should a resume be? Choose one or two pages with a US resume length guide, before-and-after edits, and a checklist for your next application.

Read Here

How to Prepare for a Phone Interview: Checklist & Answers

Learn how to prepare for a phone interview with a 30-minute prep plan, seven sample answers, and a copyable checklist for US job seekers.

Read Here

Next.js vs React: Performance and SEO Comparison

Discover the key differences between Next.js and React in terms of performance, SEO capabilities, rendering methods, and use cases. Learn which framework is best suited for your project.

Read Here

Git Stash Explained: Save, Apply, Pop & View Changes

Struggling with git stash? Learn how to stash, apply, pop, and view changes with simple commands. Learn Git stash for efficient workflow!

Read Here

React 19: New Features List [Interview Ready]

Get interview-ready with our comprehensive guide on React 19, covering its groundbreaking advancements, new features, improved performance, and backward compatibility.

Read Here
Your feedback is our favorite notification! Share your thoughts about this page and make us smile.