
JavaScript Array keys Explained With Clear Examples
Array keys() returns an iterator over indexes, including empty slots of a sparse array. See how it differs from Object.keys with exact output.
Tag
9 posts

Array keys() returns an iterator over indexes, including empty slots of a sparse array. See how it differs from Object.keys with exact output.

Array join() turns an array into one string with a separator. See how it treats null, undefined, nested arrays and sparse slots, with exact output.

Array indexOf() returns the first position of a value or -1. See how fromIndex, strict equality, NaN and sparse arrays change the result.

Array slice() copies part of an array without changing it. Learn start and end rules, negative indexes, shallow copies, holes and the splice mix-up.

Array splice() removes, inserts and replaces items in place and returns the removed ones. See the one-argument trap, negative start and loop pitfalls.

Shuffle an array fairly with Fisher-Yates, not sort with Math.random. See a seeded test that proves the bias, sampling, crypto randomness and off-by-one bugs.

Compare arrays by length and items with every, since === compares identity. See NaN, nested arrays, any-order checks and why JSON.stringify fails.

Remove by value with filter for a new array, or indexOf plus splice to change it in place. See the splice -1 trap, NaN, objects and removing inside loops.

Sort an array of objects by a property in JavaScript with sort and toSorted, handle strings, numbers, missing values and several keys, and avoid mutation.