JS Coding Questions Logo
JS Coding Questions
#380💼 Interview💻 Code

What is the easiest multi condition checking

Advertisement

728x90

You can use indexOf to compare input with multiple values instead of checking each value as one condition.

javascript
1// Verbose approach
2
3if (
4  input === "first" ||
5  input === 1 ||
6  input === "second" ||
7  input === 2
8) {
9  someFunction();
10}
11// Shortcut
12
13if (["first", 1, "second", 2].indexOf(input) !== -1) {
14  someFunction();
15}

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 38

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
380of476