Code: Select all
let someStringVar = someStringArray[4] ?? '';
Moderators: trava90, athenian200
Code: Select all
let someStringVar = someStringArray[4] ?? '';
This is a https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operatorUCyborg wrote: ↑2020-11-22, 01:53I recently came across some JavaScript code that looks like this:
Has anyone seen this before? UXP browsers don't seem to support it, but behavior seems the same as if "??" is replaced with "||".Code: Select all
let someStringVar = someStringArray[4] ?? '';
It appears to be a big pile of nothing to save a few keystrokes instead of typingContrary to the logical OR (||) operator, the left operand is returned if it is a falsy value which is not null or undefined. In other words, if you use || to provide some default value to another variable foo, you may encounter unexpected behaviors if you consider some falsy values as usable (eg. '' or 0). See below for more examples.
Code: Select all
let foo = (bar === undefined || bar === null) ? baz : bar