Skip to content

Latest commit

 

History

History
24 lines (16 loc) · 866 Bytes

combine-conditions-using-array.md

File metadata and controls

24 lines (16 loc) · 866 Bytes

Combine conditions using array

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.

Code

// 🙂 The old way
if (myVar === "A" || myVar === "B" || myVar === "C") {
}

// 🤩 The new way
if (["A", "B", "C"].includes(myVar)) {
}

Resources