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
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
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);
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;}