Array Method In JavaScript

Array Method In JavaScript

What is Array?

·

4 min read

Arrays are generally described as "list-like objects" they are single objects that contain multiple values stored in a list. the array consists of square brackets and items that are separated by commas.

 const shopping = ['bread', 'milk',..];
//or 
let shopping = [ bread, milk,...];
// for printing an array
console.log(shopping);

In an array, we can store various data types:- strings, numbers, objects, etc.

const sequence = [1, 2, 4, 3, 5, 7, 15];

Items in an array are numbered, starting from zero. This number is called the item's index. So the first item has an index of 0, the second has an index of 1, and so on.

const shopping = ['bread', 'milk', 'cheese', 'sugar', 'noodle'];
console.log(shopping[0]);
// returns "bread"

an array inside an array is called a multidimensional array. you can access an item inside an array that is itself inside another array by chaining two sets of square brackets together.

for ex.

const random = ['tree', 795, [0, 1, 2]];
random[2][2];

Length:

The length property sets the number of elements in an array.


 let fruits = ["Banana", "Orange", "Apple", "Mango"];
 console. log(fruits);                                                                                                                       console. log(fruits.length);

Push:

The push method adds new items to the end of an array.


const fruits = ["Banana", "Orange", "Apple", "Mango"];
 fruit.push("Kiwi");
 console. log(fruits.push);

here, kiwi will be added at the end.

pop:

The pop method removes the last element of an array.


const fruits = ["Banana", "Orange", "Apple", "Mango"];
console. log(fruit.pop);

In this example, the last element will be removed (popped).

unshift():

The unshift method adds new elements at the beginning of an array.


const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruit.unshift("Lemon","Pineapple");
 console. log(fruits.unshift);

here two fruits( lemon and pineapple) will be added before the banana.

shift:

the shift method removes the first item of an array.

const fruits = ["Banana", "Orange", "Apple", "Mango"];
 console. log(fruits.shift);

slice():

The slice method returns selected elements in an array, as a new array. it selects from a given start, up to a (not inclusive) given end. this method does not change the original array.

let fruits = ["Banana", "Orange", "lemon", "Apple", "Mango"];
 console. log(fruits.slice(1,3));

splice():

The splice method adds/removes array elements.

At position 2, add 2 elements:

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

At position 2, remove 2 items:


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

here, the two items after position 2 will be removed.

Concat():

The concat method concatenates i.e joins two or more arrays.

let arr1 = ["banana", "orange"];
let arr2 = ["apple]", "mango", "kiwi"];
console. log(arr1.concat(arr2);

fill():

The method fills specified elements in an array with a value.

let fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.fill("Kiwi", 2, 4);

includes():

The includes method returns true if an array contains a specified value. the includes method returns false if the value is not found. this method is case-sensitive.


let fruits = ["Banana", "Orange", "Apple", "Mango"];
 console.log (fruits.includes("Mango"));

here, the value will be true.

indexOf():

The indexOf method returns the first index (position) of a specified value. the indexOf method returns -1 if the value is not found.

let fruits = ["Banana", "Orange", "Apple", "Mango"];
console. log (fruits.indexOf("Apple"));

isArray():

The isArray() method returns true if an object is an array, otherwise false.

let fruits = ["Banana", "Orange", "Apple", "Mango"];
console. log( fruits. Array.isArray(fruits));

join():

The method returns an array as a string. the join method does not change the original array. any separator can be specified. The default is comma (,).


let fruits = ["Banana", "Orange", "Apple", "Mango"];
 console. log(fruits.join(" or "));

LastIndexOf:

The lastIndexOf method returns the last index (position) of a specified value. This method returns -1 if the value is not found. the lastIndexOf starts at a specified index and searches from right to left.

let fruits = ["Apple", "Orange", "Apple", "Mango"];
console. log(fruits.lastIndexOf("Apple"));

reverse():

The reverse method reverses the order of the elements in an array. method overwrites the original array.

let fruits = ["Banana", "Orange", "Apple", "Mango"];
 console. log(fruits.reverse());

sort():

The sort sorts the elements of an array. this overwrites the original array & sorts the elements as strings in alphabetical and ascending order.

let fruits = ["Banana", "Orange", "Apple", "Mango"];
 console. log(fruits.sort());

split():

The split method splits a string into an array. returns the new array. this method does not change the original string.

 let text = "how are you?";
 console. log(text.split(" "));

map():

The map creates a new array by calling a function for every array element. map() calls a function once for each element in an array and does not execute the function for empty elements.

let numbers = [4, 9, 16, 25];
console. (log numbers.map(math.sqrt));

toString():

The toString method returns a string with array values separated by commas.

let fruits = ["Banana", "Orange", "Apple", "Mango"];
console. log(fruits.toString());

I hope, this is useful to all! keep exploring!