0

I got a newbie question, that is wondering me a bit!

Which way is best? Are there any difference?

Example a

if (number === 1 || number === 2 || number === 3) {
  ...
}

Example b

if (number === 1) {
  ...
} else if (number === 2) {
  ...
} else if (number === 3) {
  ...
}

As these examples shows, the first example is using way less lines of code, so my first thought is, thats the right way in this case. But are there any benefits in example b?

I'm aware you can use a switch statement too, but I can't with the code I'm sitting with, that made me wonder about this

jonask
  • 679
  • 2
  • 6
  • 21
  • 1
    An even better option: once you have 3+ conditions, I'd prefer an array. `if ([1, 2, 3].includes(number)) {`. Your second snippet is a *bad* idea because it's very WET, you'll have to repeat the body of the block for every condition – CertainPerformance Mar 13 '20 at 09:02
  • 1
    First one is suitable if you want to do same task for every true condition, second one is for separate tasks depeding on condition – Kiran Shinde Mar 13 '20 at 09:04
  • both Example a, Example b are different, example 1 is check either number is 1 or 2 or 3, do a process. example 2 is check number values and do the separate processes. – Azad Mar 13 '20 at 09:07
  • Likely of interest: [Check variable equality against a list of values](https://stackoverflow.com/q/4728144) – VLAZ Mar 13 '20 at 09:10

5 Answers5

2
if (number === 1 || number === 2 || number === 3) {
  console.log('here')
}

is different from

 if (number === 1) {
  console.log('here',number)   //here1
} else if (number === 2) {
  console.log('here',number)   //here2
} else if (number === 3) {
  console.log('here',number)    //here3
}

in first example if the number is 1 or 2 or 3 it will work same but in second example if the number is 1 will work different and if number is 2 it will work different and for 3 it will work different

Kunika
  • 81
  • 6
  • 1
    `console.log('here', number)` in the first case would do the same; not a very convincing example… – deceze Mar 13 '20 at 09:16
1

Your 2 examples serve 2 different purposes, the first is when each of the condition has the same outcome / code to be ran, whereas the second example, each condition has a different outcome / code to be ran

andy mccullough
  • 9,070
  • 6
  • 32
  • 55
1

It depends on what your are trying to achieve. In the first example you'll run the same code if one of them is true, and in the second example you'll run probably different lines of code for each equality. This is the difference between the two. So the efficiency in number of lines is not a factor here.

Roy Levy
  • 640
  • 1
  • 11
  • 24
0

the differnece is below:-

first statement will going to print the same result every time if any one or "||" condition is true.

Second statement will going to print the result on the basis of condition inside if and else if which one is true.

0

Most programming language supports short circuit.
Meaning to if any of the earlier conditions are met, it will go into the block immediately without checking the the truth of latter conditions.

In the case where you just need to check if number equals to either 1, 2 or 3, and do one action regardless of the value of number, Example a would be more suitable.

Although the overhead is negligible, you save on the duplication of code, mentioned by all other answers!

jackc
  • 56
  • 4