Security News
New axobject-query Maintainer Faces Backlash Over Controversial Decision to Support Legacy Node.js Versions
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.
@babel/plugin-proposal-nullish-coalescing-operator
Advanced tools
Package description
The @babel/plugin-proposal-nullish-coalescing-operator package allows developers to use the nullish coalescing operator (??) in their JavaScript code. This operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand. This plugin transforms nullish coalescing operator syntax into equivalent JavaScript code that can be understood by JavaScript engines that do not support this feature natively.
Nullish Coalescing for Default Values
Use the nullish coalescing operator to provide a default value for a variable that might be null or undefined. This is useful for handling API responses that might not return data.
const response = apiResponse ?? 'default value';
Combining with Logical OR for Fallbacks
Combine the nullish coalescing operator with the logical OR operator to provide a default value that is an object if both user settings and default settings are null or undefined. This showcases how nullish coalescing can be used in conjunction with other logical operators for more complex fallback mechanisms.
const settings = userSettings ?? defaultSettings || {};
Similar to the nullish coalescing operator, the optional chaining plugin (@babel/plugin-proposal-optional-chaining) allows developers to safely access deeply nested properties of an object without having to check for the existence of each level in the chain. While nullish coalescing provides a way to handle null or undefined values, optional chaining helps in accessing properties that might not exist. Both enhance code readability and safety.
The lodash.get function from the lodash library allows developers to access properties of an object safely, providing a default value if the property does not exist. While it serves a similar purpose in safely handling data that might be undefined or null, it does so in a different manner compared to the nullish coalescing operator, which is a language feature transformed by the @babel/plugin-proposal-nullish-coalescing-operator.
Readme
Replace
??
with an inline helper.
In
var foo = object.foo ?? "default";
Out
var _object$foo;
var foo = (_object$foo = object.foo) !== null && _object$foo !== void 0 ? _object$foo : "default";
NOTE: We cannot use
!= null
here becausedocument.all == null
anddocument.all
has been deemed not "nullish".
npm install --save-dev @babel/plugin-proposal-nullish-coalescing-operator
.babelrc
(Recommended).babelrc
{
"plugins": ["@babel/plugin-proposal-nullish-coalescing-operator"]
}
babel --plugins @babel/plugin-proposal-nullish-coalescing-operator script.js
require("@babel/core").transform("code", {
plugins: ["@babel/plugin-proposal-nullish-coalescing-operator"]
});
loose
boolean
, defaults to false
.
When true
, this transform will pretend document.all
does not exist,
and perform loose equality checks with null
instead of strict equality checks
against both null
and undefined
.
In
var foo = object.foo ?? "default";
Out
var _object$foo;
var foo = (_object$foo = object.foo) != null ? _object$foo : "default";
FAQs
Remove nullish coalescing operator
The npm package @babel/plugin-proposal-nullish-coalescing-operator receives a total of 9,861,544 weekly downloads. As such, @babel/plugin-proposal-nullish-coalescing-operator popularity was classified as popular.
We found that @babel/plugin-proposal-nullish-coalescing-operator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.
Security News
Results from the 2023 State of JavaScript Survey highlight key trends, including Vite's dominance, rising TypeScript adoption, and the enduring popularity of React. Discover more insights on developer preferences and technology usage.
Security News
The US Justice Department has penalized two consulting firms $11.3 million for failing to meet cybersecurity requirements on federally funded projects, emphasizing strict enforcement to protect sensitive government data.