Using && to set a Logical Value

While the double pipe || (logical OR) is useful to set a default value, a double && will set an alternate value. Below is a comparison of the difference:

let name = '' name || 'Brian'

name is now set to ‘Brian’ as the value was null, so the || set ‘Brian’ as the default instead.

let name = 'Brian' name === 'Brian' && 'Jim

In the second example, the alternate will be picked, if the condition is true. name is ‘Brian’ so we swap that value out for ‘Jim’.

let name = '' name === 'Jerry' && 'Jim'

In the above, the name value would be false.

Leave a Reply

Your email address will not be published. Required fields are marked *