-> If the body of the response is truthy and the item field is truthy: assign item = response.body.item. If either of them is falsy assign item = 'default'.
NOTE: in JavaScript the && and || operators work differently than in other languages
left&&right===left// if left is falsyleft&&right===right// if left is truthyleft||right===right// if left is falsyleft||right===left// if left is truthy
Nice Article
Btw, you don't have to use the double not (
!!
) operator in a condition. It is enough to just writebecause an
if
statement checks thetruthy
ness (or thefalsy
ness) of the value.falsy
values are:false
0
(zero)""
(empty string)null
undefined
NaN
Everything else will be treated as
truthy
.The same rules apply to
Boolean(value)
and!!value
.This also works inside other boolean expressions so you can write for example:
->
If the body of the response istruthy
and the item field istruthy
: assignitem = response.body.item
. If either of them isfalsy
assignitem = 'default'
.Nice to know this! Thank you for sharing this it's very interesting Whaison