Arrays in Javascript

Arrays in Javascript

In JavaScript, an array enables us to store a collection of multiple items with different types, and can perform multiple operations through its methods. Array methods are built-in functions that we can use to perform some operations. In this article we will learn about commonly used Array methods you should know as a JavaScript developer.

How to Initialize an array

There are multiple ways by which you can initialize an array.

let array1 = [1, "Zia", true];
let array2 = new Array (2, "Hashnode", null);

1. push()

This methods adds an element at the end of an array and returns new length of the modified array

let array = ["Google", "Hashnode", null, 2];
array.push("Zia");
console.log(array);
//Output: [ 'Google', 'Hashnode', null, 2, 'Zia' ]

2. concat()

This method is used to merge two or more arrays. This method does not modify the original array, but returns the newly created array

let array1 = [1,"Zia","Hashnode","Hyd"];
let array2 =[2,"LCO"];

let newArray = array1.concat(array2);
console.log(newArray);
console.log(array1);
//Output : [ 1, 'Zia', 'Hashnode', 'Hyd', 2, 'LCO' ]
//         [ 1, 'Zia', 'Hashnode', 'Hyd' ]

3. slice()

This methods returns a new array with specified start to end elements.

let array = [1, 2, 3, 4, 5, 6, 7, 8];
let slicedArray = array.slice(2,5);
console.log(slicedArray);
//Output: [ 3, 4, 5 ]

4. includes()

This method returns true if a value is present in the array otherwise returns false.

let array = [10,2,3,4,5,4];
console.log(array.includes(3));
console.log(array.includes(13));
//Output : true      
//         false

5. indexOf()

This method returns the last index of a value otherwise returns -1.

let array = [10,2,3,4,5,4];
console.log(ar1.indexOf(4));
console.log(ar1.indexOf("LCO"));
//Output : 3
//        -1

6. lastIndexOf()

This method returns the index of the first occurence of a value otherwise returns -1.

let array = [10,2,3,4,5,4];
console.log(ar1.lastIndexOf(4));
console.log(ar1.lastIndexOf(14));
//Output : 5
//        -1

7. reverse()

This method modifies the original array and reverses the contents of the array.

let array = [10,2,3,4,5,4];
array.reverse();
console.log(array);
//Output : [ 4, 5, 4, 3, 2, 10 ]

8. sort()

This method sorts the elements of an array.

let a = ["Zia","Hashnode","LCO","Markdown"];
let b = [100,54,62,27,1]
a.sort();
b.sort();
console.log(a);
console.log(b);
//Output : [ 'Hashnode', 'LCO', 'Markdown', 'Zia' ]
//         [ 1, 100, 27, 54, 62 ]

9. toString()

This method returns a string representing its array elements.

let a = ["Zia",1,true,"Hashnode"];
console.log(a.toString());
//Output : Zia,1,true,Hashnode

10. unshift()

This method adds one or more elements to the start of an array and returns the new length of the array.

let a = ["Zia","Hashnode","LCO","Markdown"];
a.unshift("Peter");
console.log(a);
//Output : [ 'Peter', 'Zia', 'Hashnode', 'LCO', 'Markdown' ]

11. shift()

This method removes first element from the array and returns the removed element.

let a = ["Zia","Hashnode","LCO","Markdown"];
console.log(a.shift());
console.log(a);
//Output : Zia 
//         [ 'Hashnode', 'LCO', 'Markdown' ]

12. map()

This method creates a new array from calling a function for every array element and doesn't modify the original array.

let array = [1,4,9,16];
let arrayNew = arrayMap.map(Math.sqrt);
console.log(arrayNew);
//Output : [ 1, 2, 3, 4 ]

13. filter()

This method creates a copy of the array, filtered down to elements that pass a test provided by a function.

let a = ["Zia","Hashnode","LCO","Markdown"];
let b = a.filter(x => x.length>3);
console.log(b);
//Output : [ 'Hashnode', 'Markdown' ]

14. splice()

This methods modifies the original array by removing or replacing existing elements. The first parameter indicates start index, second parameter indicates how many items to delete and then we can give the items we want to add. This method returns removed items from the array

let array = [1, 2, 3, 4, 5, 6, 7, 8];
array.splice(2,3,"Zia","Hashnode","LCO");
console.log(array);
//Output: [ 1, 2, 'Zia', 'Hashnode', 'LCO', 6, 7, 8 ]

Thank you for reading!

#Arrays #Javascript #LCO #IWriteCode #iNeuron #FullStackJavaScriptWebDeveloperBootcamp