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

I’m new to javascript and I was solving an equation here, I tried alot to write a || b < 0 but it keeps telling me it is wrong until I found in the hint that it should be a < 0 || b < 0 but I didn’t undersand why

|| is used to separate two logical tests.

Intended use of or:
test1 || test2

In your code:
a || b < 0

a is not a test.

As @JeremyLT explained, it is a comparison logical operator. It is usually used in if statements. When it is in a if statements, its declaring OR. ex:

//if a is bigger than b OR (||) b is bigger than C
if(a > b || b > c) {
 //codes here
}

There are a lot of other logical operators out there. You can check it out here
It can also be used for default values ex:


function fruitsILove(fruits) {
  let theFruit = fruits || "strawberry";
return theFruit;
}
fruitsILove("banana"); //outputs "banana"
//but if you do not put anything;
fruitsILove(); //this will output "strawberry" because a default
//value is applied on the function.

In a || b < 0, what happens is that a gets evaluated and if it’s not a boolean, it’s coerced to one.

a < 0 || b < 0 is explicit enough so I won’t get into details.

Usually when people do the first case (just dropping the value inside a boolean operation with or without && and ||; or maybe inside if/while) they are either 100% sure that the variable is Boolean or they rely on the fact that JS is a loosely weakly typed language that can coerce between types. It’s very common to want to represent values like 0, null, undefined, “” and NaN as false (thanks to falsiness) and the rest as true

Hence why some people do this

const money = 0;

if (money) console.log("I have money")

instead of

if (money > 0) console.log("I have money")

I am not a fan of relying on type coercion… in this case if you I wanted to drop variables like that I’d make sure that they’re boolean, for example:

const wantCandy = true;
const money = 45;

if (wantCandy && money > 0) {
  console.log("I want money and have money to buy it");
}

I don’t see any problem making use of falsiness. Its concise and can lead to clearer code if you know what you are doing.