What does the || operator do?


What does the || operator do?


The or, ||, operator is used to check if either the left or right operand (the left and right sides of the operator) evaluates to true. If either side of the operand evaluates to true when using this operator, the entire expression evaluates to true.


console.log(true || false); //will print `true` to the console
console.log(false || false); //will print `false` to the console

//the following if statement will run whatever is in between the curly braces
if(5 == '5' || 4 == 5) {
  //do something

The follow question is can it be more than two choices in ‘||’ and ‘&&’ operators like the one following up from my code?

if (number1< 0 || number1> 100 || number2< 0 || number2> 100 || number3< 0|| number3> 100)
return ‘You have entered an invalid grade’

yes, but then you have to keep in mind the order of operations. the and operator (&&) is evaluated before the or operator (||). see the docs

at this point you might want to work with a list + loop or a function to validate your numbers

Does it matter if I write it like this;
if (number1< 0 || number1> 100 || number2< 0 || number2> 100 || number3< 0|| number3> 100)

or like this;
if ((number1< 0 || number1> 100) || (number2< 0 || number2> 100) || (number3< 0||number3> 100))

Is there a difference in terms of the understanding of computer?(Its just extra paranthesis around the code)

Thanks very much

the order in which the conditions are resolved is slightly different, but the outcome is the same

I know that with || operator left side is evaluated first and if its false or lacking the requirement the right side is not even checked, but how does it work when there are multiple I dunno expressions or whatever they called?

what you describe here is the behavior of the and operator (&&).

the or operator (||) evaluates both sides, given False || True results in True.

seems you have questions about the and operator, but use the or operator, which makes it really confusing.

Sorry about confusion, but starting to clear up my mind thanks to you
One last thing though, with multiple of these ‘||’ expressions next to each other like this one;

(number 1 < 0 || number 1 > 100 || number 2 < 0 || number 2 > 100 || number 3 < 0 || number 3 > 100)

How does the order of operations take place?
My thinking is that it evaluates them 2 by 2, like the first two expr. first and then whatever the result of those two expr. computer gets, it compares that to the 3rd expr. and whatever the result is, it compares that to the 4th expr. and so forth?
And/or is it a better practice to wrap every two expression with a parenthesis while comparing them to others in one big comparison which does what I just wrote above automatically anyway thanks to extra parenthesis around every two expr.?
Thanks a lot

I think the evaluation is simply from left to right, given there are no operators taken precedence.

parentheses would be needed if you use and operator, otherwise I would simply look into into functions or list + loop to do eliminate the repetitiveness of these conditions.

Wouldnt putting a if statement like this:

 if(avg < 0 || avg > 100){
    return 'You have entered an invalid grade.'

Simply be easier than the way they did it? I did a calculation for the function in a variable.

avg = (num1 + num2 + num3)/3;

Super late answer to this question, but it is possible to have an average that falls in the range 0 <= x <= 100 with any one of the input grades not being a valid input (i.e. a= -50, b = 100, c = 100 → average is 50, which is valid, but the first grade is not). So even though it appears like more simplistic code, it’s not really implementing the appropriate check.