DEV Community

Madhavan G
Madhavan G

Posted on

Array Methods in JavaScript.

what is array in javascript?

In JavaScript, an array is a specialized object used to store an ordered collection of values, where each value is indexed numerically starting from 0.

Core Definition:
An array is a dynamic, zero-indexed, list-like data structure that can hold elements of any type (heterogeneous), including primitives, objects, and even other arrays.

Example:

const arr = [10, "hello", true, { id: 1 }, [1, 2, 3]];

Here:
•10 → Number (primitive)
•"hello" → String (primitive)
•true → Boolean (primitive)
•{ id: 1 } → Object (non-primitive)
•[1, 2, 3] → Array (special type of object)

What "special type of object" means here
*It’s "special" because, unlike a plain object {}, arrays have:

⇒Ordered elements (indexed numerically starting from 0)

⇒Built-in methods for iteration and manipulation (push, pop, map, filter, etc.)

⇒A length property that updates automatically.

⇒Internal optimizations in JavaScript engines to handle sequential memory access efficiently.

⇒So while { } and [ ] are both objects under the hood, arrays are designed for ordered collection management, making them “special.”


Basic Array Methods in Javascript are:

Arrays come with built-in methods that make data manipulation easy.
•Array length
•Array toString()
•Array at()
•Array join()
•Array pop()
•Array push()
•Array shift()
•Array unshift()
•Array isArray()
•Array delete()
•Array concat()
•Array copyWithin()
•Array flat()
•Array slice()
•Array splice()
•Array toSpliced


∗Arraylength:

Definition:
A property that returns or sets the number of elements in an array. Updating it can truncate or expand the array.

Example:
const arr = [10, 20, 30];
console.log(arr.length);

screenshot of program:


∗Array toString()

Definition:
Returns a string representation of the array by converting each element to a string and joining them with commas.

Example:
const arr = [1, 2, 3];
console.log(arr.toString());

screenshot of program:


∗Array at()

Definition:
Returns the element at a specified index, supporting both positive and negative indices (negative indices count from the end).

Example:

const arr = [10, 20, 30];
console.log(arr.at(0)); // 10

screenshot of program:


∗Array join()

Definition:
Returns a string by concatenating all array elements using a specified separator.

Example:
const arr = ["a", "b"];
console.log(arr.join("*"));

screenshot of program:


∗Array pop()

Definition:
Removes and returns the last element of an array, mutating the original array.

Example:
const arr = [1, 2, 3];
arr.pop(2);
console.log(arr);

screenshot of program:


∗Array push()

Definition:
Adds one or more elements to the end of an array and returns the updated length, mutating the original array.

Example:
const arr = [1, 2, 3];
arr.push(4);
console.log(arr);

screenshot of program:


∗Array shift()

Definition:
Removes and returns the first element of an array, shifting remaining elements and mutating the original array.

Example:
const arr = [1, 2, 3];
arr.shift();
console.log(arr);

screenshot of program:


∗Array unshift()

Definition:
Adds one or more elements to the beginning of an array and returns the new length, mutating the original array.

Example:
const arr = [1, 2, 3];
arr.unshift(0);
console.log(arr);

screenshot of program:


∗Array.isArray()

Definition:
A static method that determines whether a given value is an array.

Example:
const arr = [1, 2, 3];
console.log(Array.isArray(arr));
console.log(Array.isArray([1, 2]));
console.log(Array.isArray({}));
console.log(Array.isArray("hello"));

screenshot of program:


∗Array delete (operator)

Definition:
Removes a property from an array at a given index without changing the array length, leaving an empty slot (sparse array).

Example:
const arr = [1,2,3];
delete arr[1];
console.log(arr);

screenshot of program:


∗Array concat()

Definition:
Returns a new array by merging one or more arrays or values without modifying the original arrays.

Example:
const arr = [1,2,3];
const arr1=[4,5];
let res=arr.concat(arr1);
console.log(res);

screenshot of program:


∗Array copyWithin()

Definition:
Copies a sequence of elements within the same array to another position, mutating the original array.

Example:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.copyWithin(2, 0);
console.log(fruits);

screenshot of program:


∗Array flat()

Definition:
Returns a new array with nested sub-array elements flattened up to a specified depth.

Example:
const myArr = [[1,2],[3,4],[5,6]];
const newArr = myArr.flat();
console.log(newArr);

screenshot of program:


∗Array slice()

Definition:
Returns a shallow copy of a portion of an array into a new array without modifying the original array.

Example:
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const old = fruits.slice(2);
console.log(old);

screenshot of program:


∗Array splice()

Definition:
Adds, removes, or replaces elements in an array at a specified index, mutating the original array.

Example:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");
console.log(fruits);

screenshot of program:


∗Array toSpliced()

Definition:
Returns a new array with elements added, removed, or replaced at a specified index, without mutating the original array.

Example:

const months = ["Jan", "Feb", "Mar", "Apr"];
const spliced = months.toSpliced(0, 1);
console.log(spliced);

screenshot of program:


Top comments (0)