I just need to understand some syntax

Whats the meaning of ‘||’
I encountered this during the " Record Collection" solution where they put

“object[id][prop] = object[id][prop] ||

Hey @jilali2004!

It is called the logical or operator. Here is a link to FCC lesson.

1 Like

Logical OR — “|| - MDN

1 Like

One thing to watch out for for || and && is that they do not necessarily evaluate to true of false - as many people assume (I’ve actually had to explain this to two disbelieving senior devs.) Some languages do do this, but JS doesn’t.

The logical OR will evaluate to the first value if it is truthy (because the truthy-ness of the whole thing will always be true in this case) or (if the first value is falsy) it will evaluate to the second value (because the truthy-ness or falsy-ness of the entire expression will be the same as that of the second value in this case). There is similar logic for the logical AND operator.

The problem is that people assume that “logical” means that this is a boolean operator. No - these are selectors that select one of the values.

console.log(0 || "apple");
// "apple"

console.log(17 || "apple");
// 17

console.log(null && 127);
// null

console.log("banana" && 127);
// 127

It’s a little weird at first, but once you wrap your head around it, it’s a pretty powerful tool.

4 Likes