Using the || in a console.log()

Good morning everyone. I was reading Eloquent JS, and I came across this code:

function newFunc() {
for (let n = 1; n <= 100; n++) {
  let output = "";
  if (n % 3 == 0) output += "Fizz";
  if (n % 5 == 0) output += "Buzz";
  console.log(output || n);

I was wondering what console.log(output || n) meant. Does this mean if output isn’t defined, then return n?

1 Like


a||b means return b if a is false.

a&&b means return b if a is true.

those are very useful for react and defining objects too.

tests you can run

true && console.log ('run')
false || console.log ('  run')

1 Like

Thank you, I wasn’t sure because I’ve never seen a console.log() written like that.

Yep, that’s how it works.

My favorite way of checking questions like this is to open the Chrome developer console and type the code I want to test. So in this case, you could try writing console.log(null || 1) and observe what happens.

1 Like

you’re welcome. i was confused too first time i saw it.