Array Methods In JavaScript

Array Methods In JavaScript

Types of Array methods:

1. shift()

This method removes the first element from an array

let movies = ["ironman", "superman", "spiderman"];

movies.shift();
console.log(movies);
output:
[ 'superman', 'spiderman' ]

ironman has been removed from the start

2. unshift()

This method adds one or more elements to the beginning of an array

let movies = ["ironman", "superman"];

movies.unshift("spiderman");
console.log(movies);
output:
[ 'spiderman', 'ironman', 'superman' ]

spiderman has been added to the start

3. pop()

This method removes the last element from an array

let movies = ["ironman", "superman", "spiderman"];

movies.pop();
console.log(movies);
output:
[ 'ironman', 'superman' ]

spiderman has been removed from the last

4. push()

This method adds elements to the end of the array

let movies = ["ironman", "superman"];

movies.push("spiderman");
console.log(movies);
output:
[ 'ironman', 'superman', 'spiderman' ]

spiderman has been added to the end

5. slice()

This method returns a new array containing a portion of the array on which it is implemented


syntax: slice(starting index, ending index)
It captures only the starting index but does not capture the ending index

let numbers = [1, 2, 4, 8, 10, 12, 14];

let num = numbers.slice(1, 5);
console.log(num);
output:
[ 2, 4, 8, 10 ]

6. splice()

It changes the original array by removing or replacing existing elements of an array

syntax: splice(starting index, number of items to be removed)

let numbers = [1, 2, 4, 6, 8, 10, 12, 14];

numbers.splice(0, 3);

console.log(numbers);
output:
[ 6, 8, 10, 12, 14 ]

1,2,4 has been removed from the original array

7. includes()

This method checks if an array contains a specified element or not

The output of includes method is boolean

let courses = ["html", "css", "javascript"];

console.log(courses.includes("html"));

console.log(courses.includes("python"));
true ---> since html is included in the array hence the output is true

false ---> since python is not included in the array hence the output is false

8. concat()

It merges two or more arrays together

let courses1 = ["html", "css", "javascript"];
let courses2 = ["python", "java", "c++"];

console.log(courses1.concat(courses2));
output:
[ 'html', 'css', 'javascript', 'python', 'java', 'c++' ]

9. reverse()

This method reverses an array

let order = ["one", "two", "three"];

console.log(order.reverse());
output:
[ 'three', 'two', 'one' ]

10. join()

It returns a new string by concatenating all of the elements in an array

let message = ["Learning", "takes", "time"];

console.log(message.join(" "));
output:
learning takes time

11. indexof()

It returns the index number of an array

Index starts form 0

let myName = ["Sam", "Jack", "Micky", "Tom"];

console.log(myName.indexOf("Micky"));
output:
2

12. sort() and tostring()

sort() method sorts the items of an array in a specific order and tostring() method changes the array into normal text

let alphabet = ["d", "a", "b", "c"];

console.log(alphabet.sort());

console.log(alphabet.toString());
output:
[ 'a', 'b', 'c', 'd' ]

a,b,c,d