nullish

x ?? y

nullish(x, y)

Try it yourself:

See also:

nullish(): provide a fallback for missing values

nullish(a, b) returns a when it is present and otherwise returns b. It is useful when a calculation accepts optional settings, data fields, or defaults.

Missing is not zero

Nullish behavior is deliberately different from ordinary Boolean fallback: zero, false, and an empty string can be meaningful values and should normally remain intact. Use it to supply a default only for null-like absence.

Practical use

Pair nullish() with isNumeric() when a default must still be numeric, format() for display, and equal for a fuller branch. Use isZero() if zero needs special treatment.

Caveat

Decide whether absent, empty, and invalid inputs mean different things in your model before applying one fallback rule.

Try Nullish in Calcul.io

Start with one of the editable examples above, then replace its arguments with your own values. Keeping the function on a separate calculator line makes the input and result easy to compare. For a longer workflow, assign the result to a variable or reference that line in the next expression.

Check the shown signature before adding optional arguments, and use the related-function links to compare operations with similar purposes.

All functions