JavaScript Arrays

JavaScript arrays come with a range of methods and properties for manipulation, searching, sorting, iteration, and constants. This guide covers essential array functionalities and examples.

1. Array Methods

JavaScript arrays provide several methods for various operations. Here are some commonly used methods:

- `push()`

Adds one or more elements to the end of an array and returns the new length of the array.


// Example of push()
let arr = [1, 2, 3];
arr.push(4, 5);
console.log(arr); // [1, 2, 3, 4, 5]

    

- `pop()`

Removes the last element from an array and returns that element.


// Example of pop()
let arr = [1, 2, 3];
let last = arr.pop();
console.log(last); // 3
console.log(arr);  // [1, 2]

    

- `shift()`

Removes the first element from an array and returns that element.


// Example of shift()
let arr = [1, 2, 3];
let first = arr.shift();
console.log(first); // 1
console.log(arr);  // [2, 3]

    

- `unshift()`

Adds one or more elements to the beginning of an array and returns the new length of the array.


// Example of unshift()
let arr = [1, 2, 3];
arr.unshift(0);
console.log(arr); // [0, 1, 2, 3]

    

2. Array Search

Searching for elements in arrays can be done using methods like `indexOf()` and `includes()`:

- `indexOf()`

Returns the first index at which a given element can be found, or -1 if it is not present.


// Example of indexOf()
let arr = ['apple', 'banana', 'cherry'];
console.log(arr.indexOf('banana')); // 1
console.log(arr.indexOf('grape'));  // -1

    

- `includes()`

Determines whether an array includes a certain value among its entries, returning true or false.


// Example of includes()
let arr = [1, 2, 3];
console.log(arr.includes(2)); // true
console.log(arr.includes(4)); // false

    

3. Array Sorting

Sorting arrays can be done using the `sort()` method. By default, it sorts elements as strings:

- `sort()`

Sorts the elements of an array in place and returns the array. The sort is not necessarily stable.


// Example of sort()
let arr = [3, 1, 4, 1, 5, 9];
arr.sort((a, b) => a - b);
console.log(arr); // [1, 1, 3, 4, 5, 9]

    

4. Array Iteration

Iterating over arrays can be done using methods such as `forEach()`, `map()`, and `filter`:

- `forEach()`

Executes a provided function once for each array element.


// Example of forEach()
let arr = [1, 2, 3];
arr.forEach(item => console.log(item));
// 1
// 2
// 3

    

- `map()`

Creates a new array with the results of calling a provided function on every element in the calling array.


// Example of map()
let arr = [1, 2, 3];
let newArr = arr.map(item => item * 2);
console.log(newArr); // [2, 4, 6]

    

- `filter()`

Creates a new array with all elements that pass the test implemented by the provided function.


// Example of filter()
let arr = [1, 2, 3, 4, 5];
let evenArr = arr.filter(item => item % 2 === 0);
console.log(evenArr); // [2, 4]

    

5. Array Constants

JavaScript arrays also use constants for array properties:

- `Array.isArray()`

Determines whether the passed value is an array.


// Example of isArray()
console.log(Array.isArray([1, 2, 3])); // true
console.log(Array.isArray('hello'));  // false

    

- `Array.from()`

Creates a new array instance from an array-like or iterable object.


// Example of from()
let str = 'hello';
let arr = Array.from(str);
console.log(arr); // ['h', 'e', 'l', 'l', 'o']

    

- `Array.of()`

Creates a new Array instance with a variable number of arguments, regardless of the number or type of the arguments.


// Example of of()
let arr = Array.of(1, 2, 3);
console.log(arr); // [1, 2, 3]