Skip to content
JavaScript: Check for a missing row before splice

JavaScript: Check for a missing row before splice

November 19, 2026

A failed row search returns -1. Passing that value straight to splice removes the last element of a nonempty array. Check for the missing-result value before deleting anything.

Set up

Tested on Debian 13 Linux with Node.js v24.19.0. Use Bash, Node.js on your PATH, a text editor, and the mktemp, rm and rmdir utilities. No packages or network access are needed. Keep the same terminal through cleanup. Stop if setup fails before saving the source.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save as javascript-guard-missing-splice-index.mjs:

import assert from 'node:assert/strict';

function removeFirstById(rows, id) {
  const index = rows.findIndex(row => row.id === id);
  if (index === -1) return false;
  rows.splice(index, 1);
  return true;
}

const unsafe = [{ id: 'a' }, { id: 'b' }];
unsafe.splice(unsafe.findIndex(row => row.id === 'missing'), 1);
assert.deepEqual(unsafe.map(row => row.id), ['a']);

const rows = [{ id: 'a' }, { id: 'b' }];
assert.equal(removeFirstById(rows, 'missing'), false);
assert.deepEqual(rows.map(row => row.id), ['a', 'b']);
console.log('Missing ID leaves rows:', rows.map(row => row.id).join(', '));
assert.equal(removeFirstById(rows, 'a'), true);
assert.deepEqual(rows.map(row => row.id), ['b']);
assert.equal(removeFirstById(rows, 'b'), true);
assert.equal(removeFirstById(rows, 'b'), false);
assert.deepEqual(rows, []);

const duplicates = [{ id: 'a' }, { id: 'a' }, { id: 'b' }];
assert.equal(removeFirstById(duplicates, 'a'), true);
assert.deepEqual(duplicates.map(row => row.id), ['a', 'b']);
console.log('Unguarded missing-ID removal leaves:', unsafe.map(row => row.id).join(', '));
console.log('First, last, empty, and duplicate-ID checks: passed');

Run:

node javascript-guard-missing-splice-index.mjs

Observed output:

Missing ID leaves rows: a, b
Unguarded missing-ID removal leaves: a
First, last, empty, and duplicate-ID checks: passed

Separate a missing result from an array position

The unguarded example searches for an absent ID and deletes the row with ID b. The guarded helper returns false for the same search and leaves both rows intact. A successful removal returns true, including when the matching row is at index zero.

ECMAScript defines findIndex to return the first matching index or a missing-result value of negative one. The splice algorithm treats a negative start as a position measured from the end. Both behaviors are useful separately; combining them requires the explicit check.

Do not use the index’s truthiness as the success test. Index zero is a valid match but is falsy, while negative one is truthy. Testing the missing-result value directly avoids both mistakes.

Define the mutation and duplicate policy

The helper changes the supplied array in place. Other code holding that array sees the removal. It removes only the first matching row, as the duplicate-ID assertion demonstrates. If IDs must be unique, validate that when the records enter the collection.

This example assumes a dense, mutable array of records with an ID field. It is not a schema validator, and it does not handle null records or holes. IDs use strict equality, so a numeric ID and the same digits in a string are different values.

Keep the lookup and mutation together. If other code can change the array between those steps, a saved index may refer to a different row. The shown helper is synchronous and contains no await between searching and deleting.

Clean up

Remove the saved file and empty scratch directory. If the directory is not empty, inspect its contents before removing anything else.

rm -- javascript-guard-missing-splice-index.mjs
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Acknowledgements

This post was created with assistance from AI.

Last updated on