
Research
Node.js Fixes AsyncLocalStorage Crash Bug That Could Take Down Production Servers
Node.js patched a crash bug where AsyncLocalStorage could cause stack overflows to bypass error handlers and terminate production servers.
@humanwhocodes/array-with-default
Advanced tools
If you find this useful, please consider supporting my work with a donation.
A class that represents an array where missing elements return a specified default value.
The built-in JavaScript Array class automatically returns undefined when you attempt to access a missing element. Here's an example:
const items = [1, , 3];
console.log(items[1]); // undefined
In most cases that is okay, but in some cases you may want an alternate default value returned. The ArrayWithDefault class does that:
const items = new ArrayWithDefault({
default: 0
elements: [1, , 3]
});
console.log(items[1]); // 0
The primary use case for this class is when an array is an optional argument for a function, but using an empty array won't work.
npm install @humanwhocodes/array-with-default --save
# or
yarn add @humanwhocodes/array-with-default
Import into your Node.js project:
// CommonJS
const { ArrayWithDefault } = require("@humanwhocodes/array-with-default");
// ESM
import { ArrayWithDefault } from "@humanwhocodes/array-with-default";
Import into your Deno project:
import { ArrayWithDefault } from "https://cdn.skypack.dev/@humanwhocodes/array-with-default?dts";
It's recommended to import the minified version to save bandwidth:
import { ArrayWithDefault } from "https://cdn.skypack.dev/@humanwhocodes/array-with-default?min";
However, you can also import the unminified version for debugging purposes:
import { ArrayWithDefault } from "https://cdn.skypack.dev/@humanwhocodes/array-with-default";
After importing, create a new instance of ArrayWithDefault. The constructor expects one object argument with the following properties:
default (required) - the default value to return for the missing items.elements - an optional iterable object used to populate the array.length - an optional value to set the array's length property to.Here are some examples:
const items = new ArrayWithDefault({
default: 0,
elements: [1, , 3]
});
// missing elements return the default
console.log(items[1]); // 0
// elements after the end of the array return undefined
console.log(items[10]) // undefined
// elements whose value are undefined return the default
items.push(undefined);
console.log(items[3]); // 0
const emptyItems = new ArrayWithDefault({
default: 0,
length: 5
});
// all elements return 0
console.log(emptyItems[0]); // 0
console.log(emptyItems[1]); // 0
console.log(emptyItems[2]); // 0
console.log(emptyItems[3]); // 0
console.log(emptyItems[4]); // 0
// items past the end still return undefined
console.log(emptyItems[5]); // undefined
npm install to setup dependenciesnpm test to run testsApache 2.0
FAQs
An array with default values for missing indices.
The npm package @humanwhocodes/array-with-default receives a total of 21 weekly downloads. As such, @humanwhocodes/array-with-default popularity was classified as not popular.
We found that @humanwhocodes/array-with-default demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.

Research
Node.js patched a crash bug where AsyncLocalStorage could cause stack overflows to bypass error handlers and terminate production servers.

Research
/Security News
A malicious Chrome extension steals newly created MEXC API keys, exfiltrates them to Telegram, and enables full account takeover with trading and withdrawal rights.

Security News
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.