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
| Value | JavaScript | Python |
|---|---|---|
Empty array/list: [] | true | False |
Ordinary empty object / dictionary: {} | true | False |
Empty string: "" | false | False |
Zero: 0 | false | False |
Text: "false" | true | True |
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:
| Input | Expected 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:
- If
titleisNone, return"Untitled". - 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 Nonechecks specifically forNone.
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.
course-revision:dc6e2178febf204d1f3b071f9a1a72c3a4b52252f604047e1a51528880aed174
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 HereHow 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 HereNext.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 HereGit 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 HereReact 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