InterviewHubPRO
Section Overview
Q384

Question 384 of 521

Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

74%
JavaScript Internals (521 Qs)•Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

What is the easiest multi condition checking

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

javascript
  // Verbose approach
  if (
    input === "first" ||
    input === 1 ||
    input === "second" ||
    input === 2
  ) {
    someFunction();
  }
  // Shortcut
  if (["first", 1, "second", 2].indexOf(input) !== -1) {
    someFunction();
  }