I want to convert a python boolean into JS's boolean literal. This is what I am working with:
store = dict(vat=True)
if store['vat']:
store.update({'vat': 'true'})
else:
store.update({'vat': 'false'})
Is there a more less verbose way to replace this code snippet ?
To toggle a boolean, use the strict inequality (! ==) operator to compare the boolean to true , e.g. bool !== true . The comparison will return false if the boolean value is equal to true and vice versa, effectively toggling the boolean.
JavaScript boolean type has two literal values true and false .
There are only two Boolean literals in Python. They are true and false.
>>> store['vat'] = json.dumps(store['vat'])
>>> store
{'vat': 'true'}
In JS a positive integer value is effectively true, and 0 (zero) is false.
You may try passing 0 as a JS false, and 1 as a JS true (do not use negative values)
>1 == true
true
>0 == true
false
>0 == false
true
>1 == false
false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With