Skip to content

JavaScript Arrays

Only getting the second value in an array

[count, setCount];
[, setCount];

Conditional values in an array

const arr = [flag && value].filter(Boolean);

2 arrays: check identical? (same order)

Arr1.length === arr2.length && arr1.every((item, i) => arr2[i] === item);

Sorting

mutates the existing array

  • Alphabetically
    • arr.sort()
  • numerically ascending
    • arr.sort((a, b) => a - b)

Last update: 2022-11-04