Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions documentation/examples/soak_assignment.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
el = document.getElementById 'a'
el?.href = 'b'
9 changes: 8 additions & 1 deletion documentation/sections/existential_operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ The accessor variant of the existential operator `?.` can be used to soak up nul
codeFor('soaks')
```

Unlike JavaScript’s [optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining), which cannot appear on the left-hand side of an assignment, CoffeeScript allows the existential accessor in that position. The assignment only runs when the base value exists:

```
codeFor('soak_assignment')
```

For completeness:

| Example | Definition |
Expand All @@ -34,4 +40,5 @@ For completeness:
| `a ? b` | returns `a` if `a` is in scope and `a != null`; otherwise, `b` |
| `a?.b` or `a?['b']` | returns `a.b` if `a` is in scope and `a != null`; otherwise, `undefined` |
| `a?(b, c)` or `a? b, c`  | returns the result of calling `a` (with arguments `b` and `c`) if `a` is in scope and callable; otherwise, `undefined` |
| `a ?= b` | assigns the value of `b` to `a` if `a` is not in scope or if `a == null`; produces the new value of `a` |
| `a ?= b` | assigns the value of `b` to `a` if `a` is not in scope or if `a == null`; produces the new value of `a` |
| `a?.b = c` or `a?[b] = c` | assigns `c` to `a.b` (or `a[b]`) if `a` is in scope and `a != null`; otherwise does nothing |