Empty an array

Remove every item in place, so all references see an empty array.

JavaScriptNode.js & browsers Version 1.0.0 · MIT · .md

empty-an-array.js
function empty(values) { values.length = 0; return values; }

Usage

usage.js
const queue = ['ana@example.com', 'rui@example.com'];
const sameQueue = queue;
empty(queue);
sameQueue.length; // → 0
let list = [1, 2];
const alias = list;
list = [];
alias; // → [1, 2]

Lines marked → were executed in Node.js 22 and in headless Chromium, and each result was compared with the value shown. Lines marked e.g. are illustrative.

Know the limits

  • Setting length mutates the array. Assigning [] only rebinds one variable, as the second example shows.
  • The original returned 0, the assigned length; this version returns the array.

The function above is the code the usage lines run. It does not validate its arguments; add checks where it receives untrusted input.

Source & maintenance

Based on Empty an array from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.

Returns the emptied array instead of 0. Documented as a reference with executed usage lines. Original MDX retained as inert source evidence.

Maintained by Oveyon. Original source commit: 14b481cf60df. This edition’s file: contents/empty-an-array.mdx in akamind/1loc, commit 0a77f131b22d. Read the license.

Usage lines executed in Node.js 22 and headless Chromium; no external services · reviewed October 28, 2026.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.