ES7 brought an elegant way to check if at least one of multiple conditions are true. It's much cleaner in my opinion. 🤩
Both cases are functionally identical. They'd work with number
data types as well.
❗️ There might be a performance cost with large arrays, especially if myVar
is not in the array. This doesn't seem relevant to most uses cases of this pattern, but it's still worth noting.
// 🙂 The old way
if (myVar === "A" || myVar === "B" || myVar === "C") {
}
// 🤩 The new way
if (["A", "B", "C"].includes(myVar)) {
}
- MDN docs for
Array.prototype.includes()
- Another example on dev.to