JavaScript Array Method
An array is one of the important topic in JavaScript let's learn together
what is Array?
The array is a Primitive datatype that is used to store elements in a continuous memory location, the element may be of the same Datatype or a different datatype. Array Index always starts with 0, it is important.
Why We Array
see if we want to operate on a 1k element we have to declare the var for 1K time and give the unique name to each element and we have to rem the name to access the element, which is very difficult but in the array, we store the elements in the continuous memory location and we can access by using the index values.
How To Declare an Array
We Have 2 Ways.
- By Array literals.
- By using Array Constructor.
Syntax
const arr1=[1,2,3,4,5];
const ar2=new Array(1,2,3,4);
Basic Method You must Know for Array I considered length It is not a method is property many of the coders get confused they use length(), which brings an error so be careful about it and must know how to access the element from the array because its mostly same as all the programming language.
Let's talk about the Importance method of the array in JavaScript.
- push() It is basically appending the element in the last of the array. This method modifies the original array.
code snippets
//Array creation
const arr1=[12,"Abhihsek",true,234]
console.log(arr1.push(23)); // if we print this line it will return the length of he array
console.log(arr1);
- pop() It basically deletes the element in the last of the array. This method modifies the original array.
code snippets
//Array creation
const arr1=[12,"Abhihsek",true,234]
console.log(arr1.pop()); // if we print this line it will return the delete element.
console.log(arr1);
- slice() Before understanding this method just think about what slice word gives the meaning and the game is over, slice method basically displays some portion of the array from the starting index to ending index-1. It basically override the original array
code snippets
//Array creation
const arr1=[12,"Abhihsek",true,234]
console.log(arr1.slice(1,4)); // if we print slice part
console.log(arr1);
- splice() Basically its take 3 paraments splice(start,deletecount,items) Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. This method modifies the original array.
code snippets
//Array creation
const arr1=[12,"Abhihsek",true,234]
console.log(arr1.splice(1,1,"Bhuyan")); // return the delete element
console.log(arr1);
- shift() Basically Removes the first element from an array and returns it. If the array is empty, undefined is returned and the array is not modified. This method modifies the original array.
code snippets
//Array creation
const arr1=[12,"Abhihsek",true,234]
console.log(arr1.shift()); // return the first element
console.log(arr1);
- unshift() Basically Inserts new elements at the start of an array, and returns the new length of the array This method modifies the original array.
code snippets
//Array creation
const arr1=[12,"Abhihsek",true,234]
console.log(arr1.shift()); // return the new length of the array
console.log(arr1);
- concat(array name) Basically, it's used to merge two or more array This method not modify array it returns the new array
code snippets
const arr1=[1,2,3,4];
const arr2=[5,6,7,8];
const arr3=arr3.concat(arr2,arr1);//returns the new array and store in the arr3
console.log(arr3);
- reverse() Basically, Reverses the elements in an array in place. This method mutates the array and returns a reference to the same array.
code snippets
const arr1=[1,2,3,4];
console.log(arr1.reverse()); // return the reference of the same array
- copyWithin(target,starting,end) Basically, This method is used to copy elements in the same array and insert from the target position. The copyWithin() method modifies the original array but it does not change the length of the array.
code snippets
const arr1=[1,2,3,4];
console.log(arr1.copyWithin(1,2,4)); // return the reference of the same array
- join() Basically, This method Join the array element with the separate.
code snippets
const arr1=[1,2,3,4];
console.log(arr1.join("-); // output will be 1-2-3-4
- indexOf() Basically, This method provides you with the first index occurrence of the value pass in this function.
code snippets
const arr1=[1,2,3,1,4];
console.log(arr1.join("-); // Here in the arr1 the first occurrence of the 1 is at first index so out is 0
- lastIndexOf() Basically, This method provides you with the last index occurrence of the value passed in this function.
code snippets
const arr1=[1,2,3,1,4];
console.log(arr1.join("-); // Here in the arr1 the last occurrence of the 1 is at first index so out is 3
Thank You for reading my blog hope you find it helpful