I have noticed when using the OR logic or the nullish coalescing together with the includes method in a ternary fashion.
const result = 'Hello' || ['e', 'z'].includes('e') ? 'in-array' : 'not-in-array';
console.log(result) // 'in-array'
const result = 'Hello' ?? ['e', 'z'].includes('z') ? 'in-array' : 'not-in-array';
console.log(result) // 'in-array'
const result = 'Hello' || ['e', 'z'].includes('xx') ? 'in-array' : 'not-in-array';
console.log(result) // 'in-array'
const result = 'Hello' ?? ['e', 'z'].includes('xx') ? 'in-array' : 'not-in-array';
console.log(result) // 'in-array'
To make it work, you will have to add parentheses/round brackets to the right end condition.
const result = 'Hello' || (['e', 'z'].includes('e') ? 'in-array' : 'not-in-array');
console.log(result) // 'Hello'
const result = 'Hello' ?? (['e', 'z'].includes('z') ? 'in-array' : 'not-in-array');
console.log(result) // 'Hello'
Is this an expected behaviour? Why is this happening?
'Hello' ?? ['e', 'z'].includes('xx')to evaluate to and why?'Hello' ?? ['e', 'z'].includes('xx')is truthy because it evaluate to"Hello", so the positive side of the ternary is taken, which gives you"in-array". The.includes()call is never executed.