0

How could I refacto this code ?

const toto = 1
const isToto = toto === 1 || toto === 12 || toto === 3 || toto === 4

I need toto to be a boolean

Noon
  • 139
  • 1
  • 3
  • 11

2 Answers2

1

For a check with more than one value, you could use Array#includes which looks for a value in an array/string.

const
    toto = 1,
    isToto = [1, 12, 3, 4].includes(toto);

console.log(isToto);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

First, note that = is an assignment operator, for comparison you can use == or ===. Now, one solution could be to create a Set of accepted values and then check if the created set contains the related value stored in toto variable:

let toto = 1;
const acceptedValues = new Set([1, 12, 3, 4]);
console.log(acceptedValues.has(toto));
toto = 2;
console.log(acceptedValues.has(toto));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
Shidersz
  • 16,846
  • 2
  • 23
  • 48