What is the difference between these two? From Ruby, I am used to apple ||= walrus setting apple equal to walrus only if apple is null. That seems to be the same in JS/CS, though ?= seems to do the same thing. What confuses me is this:
apple = 0
walrus = 9
apple ?= walrus // outputs 0
apple ||= walrus // outputs 9
wat
The best thing to do would be to look at the resulting JS.
Coffeescript:
JavaScript:
As you can see, the
?=explicitly checks is something isnullorundefinedin JS. This is very different from||=which just relies on whetherappleis a falsy value.To expand a bit more
apple ||= walrusis equivalent in meaning toapple = apple || walrus, so any value of apple that is truthy will short-circuit the logic and not change the value of apple.