Syntax
const arr = [item1, item2, item3];
arr[index];
arr.length;Examples
Creating and Accessing Arrays
Basic array creation, indexing, and length.
const fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // "apple" - first element
console.log(fruits[fruits.length - 1]); // "cherry" - last element
console.log(fruits.length); // 3
fruits[1] = "blueberry"; // arrays are mutable
console.log(fruits); // ["apple", "blueberry", "cherry"]Array.isArray() and Array.from()
Checking for arrays and creating them from other iterables.
console.log(Array.isArray([1, 2, 3])); // true
console.log(Array.isArray("hello")); // false
const fromString = Array.from("abc");
console.log(fromString); // ["a", "b", "c"]
const fromMap = Array.from({ length: 5 }, (_, i) => i * 2);
console.log(fromMap); // [0, 2, 4, 6, 8]Best practices
- Use array literal syntax [] rather than new Array() for creating arrays - it is more concise and avoids a quirky single-number-argument edge case
- Use Array.isArray() rather than typeof to correctly identify arrays, since typeof reports "object" for both
- Use Array.from() to convert array-like or iterable values (like NodeLists or strings) into real arrays with full method support
- Remember that arrays are objects, so assigning one array variable to another copies the reference, not the contents
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().indexOf(), includes(), lastIndexOf()
These methods search an array for a specific value. indexOf() returns the first matching index (or -1 if not found), lastIndexOf() searches from the end, and includes() simply returns true or false. All use strict equality (===) for comparison, so they cannot find objects by content, only by exact reference.
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().indexOf(), includes(), lastIndexOf()
These methods search an array for a specific value. indexOf() returns the first matching index (or -1 if not found), lastIndexOf() searches from the end, and includes() simply returns true or false. All use strict equality (===) for comparison, so they cannot find objects by content, only by exact reference.