Skip to content

Instantly share code, notes, and snippets.

@VitorLuizC
Last active April 14, 2019 03:08
Show Gist options
  • Save VitorLuizC/883e8c28c1dd03088f28f3a2154bec7c to your computer and use it in GitHub Desktop.
Save VitorLuizC/883e8c28c1dd03088f28f3a2154bec7c to your computer and use it in GitHub Desktop.
Operadores e "hacks"

&&

const response = await fetch('https://app.io/user/187927981')
// { data: { user: { name: 'Vitor' } } }

const name = response && response.data && response.data.user && response.data.user.name
// 'Vitor'

||

const name = document.querySelector('input[name=name]').value
// ''

const value = name || 'No name'
// 'No name'

+

const number = +'25.88'
// 25.88

~~

const any = ~~28.954584
// 28

!!

const strictBoolean = !!0
// false

..

const value = 0..toFixed(2)
// '0.00'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment