game.HUD = game.HUD || {} meaning

116 Views Asked by At

I'm following melon js tutorial. This is at the top of my HUD.js file.

game.HUD = game.HUD || {}

I've seen this before in other examples. What does the namespace.something = namespace.something || {} syntax mean and why is it needed?

1

There are 1 best solutions below

2
On BEST ANSWER

|| is a null coalesce operator so you are saying in that line return my object if it already exists or a new object if it does not (is null)