@putout/plugin-apply-nullish-coalescing
v2.0.0
Published
šPutout plugin add ability to apply nullish coalescing operator (??)
Downloads
2,254
Maintainers
Readme
@putout/plugin-apply-nullish-coalescing
The nullish coalescing operator (
??
) is a logical operator that returns its right-hand side operand when its left-hand side operand isnull
orundefined
, and otherwise returns its left-hand side operand. This can be seen as a special case of the logical OR (||
) operator, which returns the right-hand side operand if the left operand is any falsy value, not onlynull
orundefined
. 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 (e.g.,''
or0
).(c) MDN
šPutout plugin apply nullish coalescing.
Install
npm i @putout/plugin-apply-nullish-coalescing
Rule
{
"rules": {
"apply-nullish-coalescing": "on"
}
}
ā Example of incorrect code
result = result || 'hello';
result = typeof result === 'undefined' ? 'hello' : result;
ā Example of correct code
const result = result ?? 'hello';
License
MIT