
Security News
CVE Volume Surges Past 48,000 in 2025 as WordPress Plugin Ecosystem Drives Growth
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.
@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 or when an array may have holes.
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.outOfRange - an optional value that, when set to true, indicates that numeric indices after the end of the array should also return the default value.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
const numbers = new ArrayWithDefault({
default: 0,
elements: [1, 2, 3],
outOfRange: true
});
// all elements return 0
console.log(emptyItems[0]); // 1
console.log(emptyItems[1]); // 2
console.log(emptyItems[2]); // 3
console.log(emptyItems[3]); // 0
console.log(emptyItems[4]); // 0
console.log(emptyItems[5]); // 0
npm install to setup dependenciesnpm test to run testsApache 2.0
FAQs
An array with default values for missing indices.
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.

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

Security News
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.

Security News
Tailwind Labs laid off 75% of its engineering team after revenue dropped 80%, as LLMs redirect traffic away from documentation where developers discover paid products.