Difference between a || b < 0 and a < 0 || b < 0 in JavaScript ?

• Last Updated : 09 Jan, 2023

Both expression almost looks the same when we focused on the || (Or) operator, but both expressions are different from each other. To know the final conclusion, we have to get the knowledge of the || (Or) operator first.

JavaScript || (Or) Operator: The ‘OR’ operator is the opposite of the ‘AND’ operator. It does evaluate the operand from left to right. For each operand, it will first convert it to a boolean. If the result is true, it stops and returns the original value of that operand. Otherwise, if all the values are false, it will return the last value.

For in-depth content about other Logical Operators in JavaScript, you can check JavaScript Course Logical Operators in JavaScript

Expression 1:

`a < 0 || b < 0`

Example: The expression a < 0 || b < 0 gets evaluated and if it’s not a boolean, it’s coerced to one. Both ‘a’ and ‘b’ get compared with 0.

Javascript

 ``

Output:

`GFG`

Expression 2:

`a || b < 0`

Example: The expression gets evaluated and if it’s not a boolean, it’s coerced to one. The value of ‘a’ gets compared not the ‘b’.

Javascript

 ``

Output:

```The value of a and b
2
5```

Difference between a || b < 0 and a < 0 || b < 0:

My Personal Notes arrow_drop_up