Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

[deleted]


I'm not sure where you got that idea -- it's not at all true.

"or=" is the same as "||=", and means: Assign the variable on the left to the value on the right if the variable is falsy.

"?=" means: Assign the variable on the left to the value on the right if the variable does not yet exist (ie. is either "null" or "undefined").

Two different things, both useful.


Gotcha, thanks for the correction! I've removed my post. Could you clarify if or= still exists? I was reading the change log for 1.3.1 and I think I may have interpreted this line incorrectly "Conditional assignment of previously undefined variables a or= b is now considered a syntax error.". Thanks!


My interpretation of your quote is that the feature is there, but if the LHS is undefined, then the error is caught by the compiler, rather than at run-time (what is the falsity of an undefined value?)


Yea, you're correct. This has been one of those "duh" moments. Thanks for the reply!


Since ?= looks so familiar, any chance of an elvis or defined-or operator in coffeescript?

    if a? then a else 0
becomes either

    a ?: 0  (if you like groovy)
or

    a // 0 (if you like perl)


You've already got that. To cover my bases... The existential operator can be used infix:

  result = a ? 0
Or postfix (does "result" exist?)

  result?
Or to conditionally assign, as we've seen above:

  result ?= a


Oh, sorry I missed that!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: