JavaScript Arrays: Ultimate Guide
All 52+ Topics, Examples, Output & Details

Jump to Topic:
01What is an Array?

An array in JavaScript is a list-like, ordered collection of values. Each value (called an "element") is identified by a numeric index. Arrays can grow or shrink dynamically, and you can store any data type (numbers, strings, objects, functions, etc) in an array—even mixed types!

let fruits = ["apple", "banana", "cherry"];
// Output:
["apple", "banana", "cherry"]
Arrays are objects under the hood, but with special behaviors and methods for handling collections.
02How to Create Arrays?
let arr = [1, 2, 3];
let arr2 = new Array(1, 2, 3);
let empty = [];
// Output:
[1,2,3]
[1,2,3]
[]
Warning: new Array(3) creates an empty array of length 3, not [3].
03Accessing Elements

Access array elements by index, starting from 0. Negative indexing is available via at() (see topic 26).

let colors = ["red", "green", "blue"];
console.log(colors[0]); // first item
console.log(colors[2]); // third item
      
// Output:
red
blue
Accessing an invalid index returns undefined. Use arr.length for bounds checking.
04Adding Elements (push, unshift)

push() adds one or more elements to the end. unshift() adds to the start.

let arr = [1,2];
arr.push(3);      // [1,2,3]
arr.unshift(0);   // [0,1,2,3]
      
// Output:
[1,2,3]
[0,1,2,3]
Both methods return the new length of the array.
05Removing Elements (pop, shift)

pop() removes the last element, shift() removes the first.

let arr = [0,1,2,3];
arr.pop();    // [0,1,2]
arr.shift();  // [1,2]
      
// Output:
[0,1,2]
[1,2]
The returned value is the element that was removed.
06Clone/Copy Arrays (slice)

slice() returns a shallow copy. Also use [...arr] for a quick clone.

let arr = [1,2,3];
let copy = arr.slice();
let copy2 = [...arr];
      
// Output:
[1,2,3]
[1,2,3]
Both methods only make a shallow copy (nested objects are not cloned).
07Determine Array

Use Array.isArray() to check if something is really an array.

Array.isArray([1,2,3]); // true
// Output:
true
typeof [] is "object", so don't use typeof for arrays!
08Array Destructuring

Assign array values to variables in one line.

let [a, b, c] = [1, 2, 3];
// Output:
a = 1, b = 2, c = 3
Destructuring also works with function returns that are arrays.
09Assign Default Value

Provide a fallback value if the element is undefined.

let [x, y = 10] = [5];
// Output:
x = 5, y = 10
Default only applies for undefined (not null or other values).
10Skipping a Value

Use extra commas to skip values during destructuring.

let [first, , third] = [1, 2, 3];
// Output:
first = 1, third = 3
11Nested Array

Arrays can contain other arrays, allowing you to create 2D, 3D, or more complex structures.

let matrix = [[1,2],[3,4]];
// Output:
[[1,2],[3,4]]
Useful for representing grids, matrices, or tables of data.
12Nested Array Destructuring
let [[a, b], [c, d]] = [[1,2],[3,4]];
      
// Output:
a=1, b=2, c=3, d=4
You can destructure as deeply as your array is nested.
13Rest & Spread Operator
// Rest
let [first, ...rest] = [1,2,3,4];
// Spread
let arr1 = [1,2];
let arr2 = [...arr1,3,4];
      
// Output:
first=1, rest=[2,3,4]
arr2=[1,2,3,4]
Spread is used to copy, merge, or pass arrays to functions as arguments.
14Destructuring Swap

Swap two variables with no temp variable!

let a = 1, b = 2;
[a, b] = [b, a];
      
// Output:
a=2, b=1
15Destructuring Merge
let arr1 = [1,2];
let arr2 = [3,4];
let merged = [...arr1, ...arr2];
      
// Output:
[1,2,3,4]
Spread is a modern, clean way to merge arrays!
16Array Length

.length is the total number of elements. It’s mutable: setting arr.length = n can truncate/expand the array.

let arr = [1,2,3];
arr.length;
      
// Output:
3
Tip: arr.length = 0 clears the array instantly!
17concat()

Combines arrays or values into a new array. Does not modify the originals.

let arr1 = [1,2];
let arr2 = [3,4];
let result = arr1.concat(arr2);
      
// Output:
[1,2,3,4]
18join()

Joins all elements into a string, separated by your chosen character.

let arr = [1,2,3];
arr.join("-");
      
// Output:
"1-2-3"
Tip: join("") combines all elements without any separator.
19fill()

Fills an array (or part of it) with a static value.

let arr = [1,2,3];
arr.fill(0);
      
// Output:
[0,0,0]
Can be used to initialize arrays: Array(5).fill(1) → [1,1,1,1,1]
20includes()

Checks if the array contains a given value (strict comparison).

[1,2,3].includes(2);
// Output:
true
21indexOf()

Returns the first index of a value, or -1 if not found.

[1,2,3,2].indexOf(2);
// Output:
1
22lastIndexOf()

Returns the last index of a value, or -1 if not found.

[1,2,3,2].lastIndexOf(2);
// Output:
3
23reverse()

Reverses the array in place.

let arr = [1,2,3];
arr.reverse();
      
// Output:
[3,2,1]
toReversed() returns a reversed copy (see topic 31).
24sort()

Sorts elements in place. By default, sorts as strings (alphabetically).

let arr = [3,1,10,2];
arr.sort();              // ["1","10","2","3"]
arr.sort((a, b) => b-a); // [10,3,2,1]
      
// Output:
[1,10,2,3]
[10,3,2,1]
Always use a compare function for numbers!
25splice()

Add, remove, or replace elements at a specific index.

let arr = [1,2,3,4];
arr.splice(1,2,"x","y");
      
// Output:
[1,"x","y",4]
splice(start, deleteCount, ...items). Always returns the removed elements.
26at()

Accesses array elements using positive or negative indices.

let arr = [1,2,3];
arr.at(-1); // last element
      
// Output:
3
27copyWithin()

Copies part of the array to another location, overwriting existing elements.

let arr = [1,2,3,4];
arr.copyWithin(1,2);
      
// Output:
[1,3,4,4]
28flat()

Flattens nested arrays to the chosen depth (default is 1).

[1,[2,[3]]].flat(2);
      
// Output:
[1,2,3]
29Grouping Data

Arrays are the foundation for lists, tables, queues, stacks, and more.

let users = [
  {name: "A", age: 20},
  {name: "B", age: 25}
];
      
// Output:
[{name:"A",age:20},{name:"B",age:25}]
Arrays can store objects, functions, arrays, or any combination.
30Immutability
Immutable Methods Mutable Methods
slice, concat, map, filter, toReversed, toSorted, toSpliced, with, flatMap push, pop, shift, unshift, splice, sort, reverse, fill, copyWithin
Immutable methods return a new array, leaving the original unchanged.
Mutable methods modify the original array.
31toReversed()
let arr = [1,2,3];
let rev = arr.toReversed();
      
// Output:
rev=[3,2,1], arr=[1,2,3]
32toSorted()
let arr = [3,2,1];
let sorted = arr.toSorted();
      
// Output:
sorted=[1,2,3], arr=[3,2,1]
Tip: Use toSorted((a,b)=>b-a) for descending order.
33toSpliced()
let arr = [1,2,3];
let newArr = arr.toSpliced(1,1,9);
      
// Output:
newArr=[1,9,3], arr=[1,2,3]
34with()

Returns a copy of the array with a value changed at the specified index.

let arr = [1,2,3];
let newArr = arr.with(1, 99);
      
// Output:
[1,99,3]
35Array-like
"Array-like" objects have indexed values and a length property (e.g. arguments object in functions, NodeLists, strings).

They are not real arrays, but you can convert them to arrays with Array.from().

36Array.from()

Converts array-like or iterable objects to real arrays.

Array.from("hello");
// Output:
["h","e","l","l","o"]
37Array.fromAsync()

Converts async iterables to arrays asynchronously (ES2023+).

async function* gen() { yield 1; yield 2; }
let arr = await Array.fromAsync(gen());
      
// Output:
[1,2]
Requires await in an async function context.
38Array.of()

Creates a new array from its arguments (especially useful to avoid new Array() confusion).

Array.of(1,2,3);
// Output:
[1,2,3]
39filter()

Returns a new array of elements that pass the test function.

[1,2,3,4].filter(n => n > 2);
// Output:
[3,4]
40map()

Returns a new array with the results of calling a function for every element.

[1,2,3].map(n => n * 2);
// Output:
[2,4,6]
41reduce()

"Reduces" array to a single value by accumulating results.

[1,2,3].reduce((acc, cur) => acc + cur, 0);
// Output:
6
Tip: Often used for sums, products, or more complex aggregations.
42reduceRight()

Same as reduce() but processes the array from right-to-left.

[1,2,3].reduceRight((acc, cur) => acc + cur, 0);
// Output:
6
43some()

Checks if at least one element passes the test.

[1,2,3].some(n => n > 2);
// Output:
true
44every()

Checks if every element passes the test.

[1,2,3].every(n => n > 0);
// Output:
true
45find()

Returns the first element that passes the test, or undefined if none found.

[1,2,3].find(n => n > 1);
// Output:
2
46findIndex() / findLastIndex() / findLast()
[1,2,3].findIndex(n => n > 1);           // 1
[1,2,3,2].findLastIndex(n => n === 2);   // 3
[1,2,3,2].findLast(n => n === 2);        // 2
      
// Output:
1
3
2
Useful for searching from the end or finding the index, not just value.
47Chaining Methods
let result = [1,2,3,4]
  .filter(n => n % 2 === 0)
  .map(n => n * 10);
      
// Output:
[20,40]
Chaining lets you process data in steps for clear, concise code.
48forEach()

Executes a function for each array element (does not return a value).

[1,2,3].forEach(n => console.log(n));
// Output:
1
2
3
forEach() is for side effects, not for transformation.
49entries()

Returns an iterator of [index, value] pairs.

for (let [i, v] of [10,20].entries()) {
  console.log(i, v);
}
      
// Output:
0 10
1 20
50values()

Returns an iterator of array values. Useful in for...of loops.

for (let v of [1,2,3].values()) {
  console.log(v);
}
      
// Output:
1
2
3
51flatMap()

Maps each element, then flattens the result by one level.

[1,2,3].flatMap(n => [n, n*2]);
// Output:
[1,2,2,4,3,6]
Tip: Use for mapping and flattening in one step, especially with arrays of arrays.
Tip: Practice these code snippets in your browser's console to see real output and deepen your understanding!
Arrays are one of the most important JavaScript data structures—mastering them is key to writing modern, efficient code.

Rafi Ullah 2025 - All rights resereved