?? vs || in JavaScript: The little-known difference

Tari Ibaba
Coding Beauty
Published in
3 min readJun 8, 2024

--

At first you may think you can just swap in anyone you like right?

Wrong. They’re not what you think.

And we must learn the difference once and for all to avoid painful bugs down the line.

And what’s this difference?

It’s the incredible contrast in how they treat truthy and falsy values.

What are these?

Falsy: becomes false in a Boolean() or if:

  • 0
  • undefined
  • null
  • NaN
  • false
  • '' (empty string)

Truthy: every single thing else:

Now see what happens when you create a || chain like this:

--

--

Tari Ibaba
Coding Beauty

I help you gain the coding knowledge and skills to build the life you love.