Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a more succinct way of converting python boolean to javascript boolean literals?

Tags:

python

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 ?

like image 411
canadadry Avatar asked Oct 18 '11 10:10

canadadry


People also ask

How do you change Boolean in JavaScript?

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.

What Boolean literals does JavaScript provide?

JavaScript boolean type has two literal values true and false .

What are the two Boolean literals in Python?

There are only two Boolean literals in Python. They are true and false.


2 Answers

>>> store['vat'] = json.dumps(store['vat'])
>>> store
{'vat': 'true'}
like image 118
Ignacio Vazquez-Abrams Avatar answered Oct 23 '22 10:10

Ignacio Vazquez-Abrams


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
like image 3
Gorkem Avatar answered Oct 23 '22 10:10

Gorkem