Truthy and Falsy values. Python vs JavaScript

Python vs JS

Whether you are an experienced web developer or someone who is just entering the field, chances are you have come across Python and JavaScript.

In web development, Python is used for back-end development. What makes Python a great language for beginners is its simple, easy-to-use syntax. JavaScript can be used to make web pages dynamic, add animations, pop-up menus, clickable buttons, etc. In addition to that, frameworks like Node.js allow for server-side development which makes JavaScript a very versatile programming language.

So what are truthy and falsy values?

It can be either an individual value or an expression, which is evaluated to be either true or false. They are often used with conditional statements where we check if a particular statement is true and then we run our code.

Let’s explore the differences between truthy and falsy values between Python and JavaScript and some of the potential issues that may arise during development.

Python

In Python, the following values evaluate to False:

  • None
  • False
  • Zero (Integer: 0, Float 0.0)
  • Empty string “”
  • Empty lists [ ]
  • Empty tuples ( )
  • Empty dictionaries { }
  • Empty sets set ()
  • Empty range range(0)

Example:

my_list = []

print(bool(my_list))
# Prints False

JavaScript

In JavaScript the following values evaluate to False:

  • false
  • Zero (Integer: 0, Float 0.0)
  • Empty String “”
  • Null
  • Undefined
  • NaN

Example:

const a = "";
console.log('It is ' + Boolean(a));
// Logs It is false

So what are the things to keep in mind?

As you can see in Python all of the lists, dictionaries, tuples, and sets will evaluate to false if empty. This, however, is not the case when it comes to JS.

1.Lists

So let’s say we want to check if a page has any blog posts and if there are none, we want to display a message ‘No posts to show’. The below code will evaluate to true

const posts = [];

if (posts) {
    console.log('List Posts');
} else {
    console.log('No posts to list');
}
// Logs List Posts

The solution here is to check the length of the array

const posts = [];

if (posts.length > 0) {
    console.log('List Posts');
} else {
    console.log('No posts to list');
}
// Logs No posts to list

2.Objects

Empty objects are also truthy in JavaScript.

const user = {};

// Always true, even when no properties
if (user) {
  console.log('List User');
} else {
  console.log('No User');
}
// Logs List User

One of the common solutions is to use Object.keys() on the object. This will create an array with all of the keys from this object. We can then check the length to evaluate the expression.

const user = {};

if (Object.keys(user).length > 0) {
  console.log('List User');
} else {
  console.log('No User');
}
// Logs No User

3.Empty functions

Here is an interesting fact about how empty functions are evaluated. This applies to both Python and JavaScript. In the code below we have an empty function. The first console.log() refers to the function itself. In JavaScript all functions are objects hence why it is a truthy value. The second console.log() calls the function. Since the function is empty it returns undefined which as we know from the list above always evaluates to false. The same applies to Python with the only difference being the function returns None.

JavaScript

function myFunction(){
}
console.log(Boolean(myFunction)); // true
console.log(Boolean(myFunction())); // false

Python

def my_function():
    pass


print(bool(my_function))  # True
print(bool(my_function()))  # False

Having a good understanding of the types of values we are working with can help us avoid errors and bugs in our code and save a lot of valuable time. I hope you found this article helpful. Please feel free to share it with your friends.

Web DevelopmentPythonJavaScript
Avatar for Dayana

Written by Dayana

Currently studying Full stack software development. I love coding, cars and music :)

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.