Skip to content

Latest commit

 

History

History
27 lines (20 loc) · 587 Bytes

File metadata and controls

27 lines (20 loc) · 587 Bytes

And Or, With All the Ewoks

Now that you know about truthiness, you can understand the Boolean operators.

'a' && 'b'  // 'b'
 0  || 'b'  // 'b'

As you might have noticed, && (and) and || (or) don't return Boolean values. They short-circuit the value that breaks or completes the logic.

 0  && 'b' // 0
'a' &&  0  // 0
 0  || 'b' // 'b'
'a' ||  0  // 'a'

You can also use these operators for null coalesce.

var userUnit = metrics && metrics.unit; // null safety example
var realUnit = userUnit || 'meters';    // default value example