Skip to content

Instantly share code, notes, and snippets.

@techniq
Last active November 26, 2020 15:32
Show Gist options
  • Save techniq/259ca1206ace70f26cb7 to your computer and use it in GitHub Desktop.
Save techniq/259ca1206ace70f26cb7 to your computer and use it in GitHub Desktop.
Convert JSON boolean strings ("true"/"false") to boolean values

Convert undefined to false

JSON.parse(someVariable || false)

Returns

  • "true" => true
  • "false" => false
  • undefined => false

Retain undefined as undefined

someVariable && JSON.parse(someVariable)

Returns

  • "true" => true
  • "false" => false
  • undefined => undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment