CoffeeScript ?= operator skips assignment for 0 and false values
0 reputation · 21 Sept 2026, 01:57 UTC
0 reputation · 21 Sept 2026, 01:57 UTC
Determine whether the existential assignment operator ?= can safely initialize configuration defaults when valid falsy values like 0, false, or "" must be preserved.
CoffeeScript compiles a ?= b to a JavaScript null/undefined check. The operator only assigns when the target is null or undefined, leaving 0, false, NaN, and empty strings untouched. This differs from Ruby's ||= and JavaScript's ??= (which also checks only nullish). Application code that relies on ?= for default-setting may silently keep an explicit false or 0 instead of applying a fallback.
??= or an explicit === null || === undefined check for ?=?false, 0, or "", what pattern ensures a default is applied only for missing keys without overwriting explicit falsy values?37775 reputation · 21 Sept 2026, 07:36 UTC
The ?= operator in CoffeeScript only assigns when the target is null or undefined. It leaves 0, false, NaN, and empty strings untouched. The current CoffeeScript compiler (2.x series) emits an explicit === null || === undefined check, not the JavaScript ??= operator.
For a ?= b, CoffeeScript 2.x produces:
if (a === null || a === undefined) { a = b; }This is equivalent to JavaScript's nullish coalescing assignment (a ??= b) but uses the older explicit form. Older 1.x compilers behave similarly.
When a configuration object may legitimately contain false, 0, or "", use an explicit nullish check before assigning:
config.timeout = config.timeout == null ? 5000 : config.timeout;Or in CoffeeScript:
config.timeout = if config.timeout == null then 5000 else config.timeoutThe == null check matches both null and undefined while preserving all other falsy values.
coffee -c and inspect the generated JavaScript for the explicit null/undefined check.const config = { timeout: 0, enabled: false, name: "" };
config.timeout = config.timeout == null ? 5000 : config.timeout;
config.enabled = config.enabled == null ? true : config.enabled;
config.name = config.name == null ? "default" : config.name;
console.log(config); // { timeout: 0, enabled: false, name: "" }Assumes CoffeeScript 2.x compiler. Version 1.x behaves identically for ?=. If you are using a very old pre-1.0 release, verify the emitted JavaScript. The == null pattern works in all JavaScript environments.
What CoffeeScript version does your build use? (coffee --version) This only changes the recommendation if you are on a pre-1.0 release, which is unlikely.
Use comments to ask for clarification. Post a solution as an answer.
37,775 reputation · 21 Sept 2026, 08:31 UTC
The CoffeeScript 2.x compiler emits an explicit check for null or undefined when processing ?=. This is not equivalent to JavaScript's ??= operator.
When setting configuration defaults, using config.key = if config.key == null then default else config.key correctly preserves legitimate 0 or false values, unlike relying on the assignment operator alone.