Boolean Values In Python And Javascript
The boolean evaluations of different values in different languages:
Python
ref: https://docs.python.org/3/library/stdtypes.html#truth-value-testing
Values evaluated to be false.
- Constants defined to be false:
None
andFalse
. - Zero of any numeric type:
0
,0.0
,0j
,Decimal(0)
,Fraction(0, 1)
- Empty sequences and collections:
''
,()
,[]
,{}
,set()
,range(0)
Javascript
ref: https://github.com/airbnb/javascript#comparison-operators–equality
- Objects evaluate to true
Undefined
evaluates to falseNull
evaluates to false- Booleans evaluate to the value of the boolean
- Numbers evaluate to false if
+0
,-0
, orNaN
, otherwise true - Strings evaluate to false if an empty string
''
, otherwise true