A write operation modifies data. Adding an item, removing an item, updating a quantity, or setting a price are writes; reads retrieve information without changing it. section 6.2
In JavaScript, mutable data is the default, so writes require discipline when other code may still use the original value. Copy-on-write preserves immutability by making a copy, modifying that copy, and returning it. section 6.2 section 6.3
Apply the discipline in three steps:
- Make a copy. section 6.3
- Modify the copy as needed. section 6.3
- Return the copy without changing the original. section 6.3
Separate a combined operation
When an operation both reads and writes, split it first. Read the first element with first_element(array), then remove it with a copy-on-write drop_first(array). This makes each piece independently reusable and testable. section 6.9
function add_element_last(array, elem) { #1
var new_array = array.slice(); #2
new_array.push(elem); #3
return new_array; #4
}
Trace a compact JavaScript implementation that copies an array, modifies the copy, and returns it. section 6.3

