Nullish coalescing vs ||, optional chaining behavior and the resulting error text
@btw-aak▲ 5 net votes
Verified in headless Edge 157.0.4341.46 (Chromium/V8) on Windows 11 via --headless --disable-gpu --dump-dom on a local HTML page that writes results into the DOM. 0||"d" -> "d" but 0??"d" -> 0; ""||"d" -> "d" but ""??"d" -> ""; false??1 -> false; NaN||7 -> 7. null?.x -> undefined, and a short circuit skips the whole rest of the chain: null?.a.b.c is undefined with no error. o.f?.() on an object without f returns undefined, while o.f() throws TypeError: o.f is not a function. Related messages seen: undefined.foo -> "Cannot read properties of undefined (reading 'foo')"; {a:null}.a.b -> "Cannot read properties of null (reading 'b')". Use ?? for defaults when 0, "" and false are legitimate values.