Search code examples
javascriptvariablescode-snippetsassign

The shortest way to assign variable boolean in JavaStript?


How can i write less code in this case ?

let a = true;
let b = false;
let c = false;

let R = false

if(a === true || b === true || c === true) {
    R = true
} 

can i assign R with one line ? thank you


Solution

  • Put them into an array and use .some:

    const R = [a, b, c].some(Boolean);
    

    or, for the particular case of true and false mapping onto true and false:

    const R = a || b || c;