coffeescript-cookbook / coffeescript-cookbook.github.io

CoffeeScript Recipes, Examples and Tutorials
https://coffeescript-cookbook.github.io
Other
562 stars 178 forks source link

Update object-literal.md #146

Closed kikorb closed 7 years ago

kikorb commented 7 years ago

The example was wrong. It stated that ?= is similar to JS || when the || will affect not only not defined scenario but also any falsy values (values that evaluate as false)

There are way too many differences between the 2:

  1. window.var = false;
  2. window.var = 0;
  3. window.var = '';
  4. window.var = null;

window.var ?= 'something' console.log(window.var) ->

  1. false
  2. 0
  3. ''
  4. 'something'

window.var ||= 'something' console.log(window.var) ->

  1. 'something'
  2. 'something'
  3. 'something'
  4. 'something'