DEV Community

Cover image for JavaScript Array Methods.
Ajay Sharma
Ajay Sharma

Posted on Edited on

JavaScript Array Methods.

Hello Everyone
In This Article We Will be Seeing Different Array Methods.

Follow Me on LinkedIn https://www.linkedin.com/in/ajaysharma12799/

Follow Me on Github https://www.github.com/ajaysharma12799/

Create Array Either Using Square Braces or Using new Array() Class

let arr = [1,2,3,4,5];
let arr = new Array(length or element);
Enter fullscreen mode Exit fullscreen mode

Methods

1. push(element)

arr.push(10); // Insert Element at Last of Array
Enter fullscreen mode Exit fullscreen mode

2. pop()

arr.pop(); // Remove Last Element of Array and Return It
Enter fullscreen mode Exit fullscreen mode

3. shift()

arr.shift(); // Remove First Element of Array and Return It
Enter fullscreen mode Exit fullscreen mode

4. unshift(element)

arr.unshift(20); // Insert Element at First Position in Array
Enter fullscreen mode Exit fullscreen mode

5. length ( Not a Method but a Property )

arr.length; // Return Length of Array
Enter fullscreen mode Exit fullscreen mode

6. fill(item)

arr.fill(0); // Fill Entire Array With 0 or with Given Item
Enter fullscreen mode Exit fullscreen mode

7. concat(array) ( Can Take Single Array or Multiple Arrays and Always Return New Array )

let arr1 = [1,2,3];
let newArray=arr1.concat([4,5,6]); // O/P = [1,2,3,4,5,6]

let arr2 = [1,2];
let newArray=arr2.concat([4,5], [7,8]); // O/P = [1,2,4,5,7,8]
Enter fullscreen mode Exit fullscreen mode

8. find() (Take a Function Which Execute and Return First Value Which Satisfies Condition)

let arr = [1,2,3,4,5];
let found = arr.find(element => element > 2);
console.log(found); // O/P = 3
Enter fullscreen mode Exit fullscreen mode

9. findIndex() (Take a Function Which Execute and Return First Value Index Which Satisfies Condition)

let arr = [1,2,3,4,5];
let found = arr.findIndex(element => element > 2);
console.log(found); // O/P = 2
Enter fullscreen mode Exit fullscreen mode

10. sort() // Sort According to Aplhabet Order

let arr = [1,3,2,4,6];
arr.sort();
console.log(arr); // O/P = [1,2,3,4,6]

Note:- But This Method Will Not Sort Value Which is of 2 Digit, So We Need to Make Our Custom Function and Pass into Sort Method.

let arr = [1,4,2,45,6,7];
arr.sort((a,b) => a-b); // Change a or b according to you if you want in Ascending or Descending Order.
console.log(arr); // O/P = [1,2,4,6,7,45];
Enter fullscreen mode Exit fullscreen mode

11. reverse()

let arr = [1,5,2,4,3];
let reversedArray = arr.reverse(); 
// Reverse Array and return Reference to Same Array
console.log(reversedArray); // O/P = [5,4,3,2,1]
Enter fullscreen mode Exit fullscreen mode

12. slice(start, end) ( Extract Small Chunk of Array in Form of Array )

start => Starting Index From 0 and It is Included
end => Ending Index Which is Length - 1 and It is Not Included

let arr = [1,2,3,4,5];
let slicedArray = arr.slice(0,3);
console.log(slicedArray); // O/P = [1,2,3]
Enter fullscreen mode Exit fullscreen mode

13. splice(start, deleteCount: Optional, element: Optional) ( Remove Element From Array and Can Also Insert New Elements In the Place of Removed Element )

let arr = [1,2,3,4,5];
let splicedArray1 = arr.splice(2); 
console.log(splicedArray1);

/*
Will Remove Array Element From Index 2 to End and Return in Form of Array.
O/P = [3,4,5]
*/

let splicedArray2 = arr.splice(2, 2 10, 30);
console.log(splicedArray2);

/*
Will Remove Array Element From Index 2 to End and Return in Form of Array.
O/P = [3, 4] // Spliced Array
O/P = [1,2,10,30,5] // Original Array After Inserting New Element InPlace of Removed Element.
*/
Enter fullscreen mode Exit fullscreen mode

14. indexOf(element); ( Return Index of Given Element, If Element Not Found Return -1 )

let arr = [1,2,3,4,5];
console.log(arr.indexOf(5)); // O/P = 4
Enter fullscreen mode Exit fullscreen mode

15. includes(element, startIndex: Optional) ( Search For Given Element If Found Return True Otherwise False )

let arr = [1,2,3,4,5];
console.log(arr.includes(5)); // O/P = true

console.log(arr.includes(2, 3)); // O/P = false, Because We Are Searching From Index 3 and 2 is Not Present in Array From Index 3.
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
rorschach profile image
R-Lek • Edited


let arr = [1,3,2,4,6];
arr.sort();
console.log(arr); // O/P = [1,2,3,4,5]

-> 6 changed into a 5?

Collapse
 
ajaysharma12799 profile image
Ajay Sharma

Sorry Brother That was My Typing Mistake, I am Correcting It
Thank You 😁👍