Truthiness & Types

Use truthiness and types with Transcrypt.

We'll cover the following

Truthiness

There is a difference in truthiness between Python and JavaScript when it comes to empty lists, dictionaries, and sets. For example, in Python an empty list evaluates to False, whereas in JavaScript it evaluates to True. If we use the condition of a list being empty in an expression to determine program flow, we may not get the result we intended. As previously mentioned, the most straightforward and recommended way to deal with this difference in truthiness is to check if our list or dictionary has a length of zero. For example, the expression len([]) > 0 will always evaluate to False in both Python and JavaScript. The second way is to use a Transcrypt compiler directive that tells the transpiler to explicitly use Python-based truthiness:

Get hands-on with 1200+ tech skills courses.