Skip to content Skip to sidebar Skip to footer

Javascript Question Mark & Double Pipes

I am new to javascript and so far it is my understanding that: ? & : is used for 'if true, do this, if false do this' However, I am having a little more trouble with ||. From m

Solution 1:

|| means "or". The left side of the || is evaluated first. If it resolves to true, then the expression resolves to true. If, on the other hand, the left side of the || operator resolves to false, then the right side will be evaluated and returned.

Example 1:

1 == 1 || 1 == 0

Will evaluate to true, since the left side of the || operator is true.

Example 2:

1 == 2 || 1 == 1

The left side resolves to false, so the right side is evaluated and returned. In this case, 1==1 so the whole expression (1 == 2 || 1 == 1) resolves to true.

Example 3:

1 == 2 || 1 == 3

The left side resolves to false, so the right side is evaluated and returned. In this case, 1 does not equal 3, so the whole expression (1 == 2 || 1 == 3) resolves to false.

To put it more simply, if either of the expressions "held together" by the || operator are true, then the expression will return true. Otherwise, it will return false.

Solution 2:

subset = (subset) ? (description[demoCode] == 0 || description[demoCode] == series[demoCode]) : false;

is equal to

if(subset){
  subset = (description[demoCode] == 0 || description[demoCode] == series[demoCode);
}
else { subset = false; }

The || is an or operator here and evaluates to true or false

Post a Comment for "Javascript Question Mark & Double Pipes"