Let's assume that we want to execute Math.max()
or Math.min()
on a undefined value:
javascriptlet x; let y = 7; Math.min(x, y); // NaN Math.max(x, y); // NaN
We could use the nullish coalescing operator (ES2020) to assign a default value to a variable. Example:
javascriptlet x; let y = 7; Math.min(x ?? 0, y); // 0 Math.max(x ?? 0, y); // 7
Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.