6

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 ?

1
  • 3
    The same, but shorter would be store['vat'] = 'true' if store['vat'] else 'false' Commented Oct 18, 2011 at 10:46

2 Answers 2

16
>>> store['vat'] = json.dumps(store['vat'])
>>> store
{'vat': 'true'}
Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

If you're using Jinja2, you can do this using: let jsBoolean = {{ my_python_boolean|int }}; This assumes you set my_python_boolean to True or False.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.