
In JavaScript, array is a single variable that is used to store different elements. It is often used when we want to store list of elements and access them by a single variable.
Now lets start to learn about list of array methods:-
1.Push()
The Push() method is used to add one or more elements at end of the array.
Example:-
const array=["apple","mango","grapes"]; array.push("pineapple","kiwi"); console.log(array); // ["apple","mango","grapes","pineapple","kiwi" ]
2.Unshift()
The Unshift() method is used to add one or more elements at beginning of the array.
Example:-
const subjects=["maths","english"]; subjects.unshift("tamil","science" ); console.log(subjects);//["tamil","science", "maths","english"]
3.Splice()
The Splice() method is used to add and replace one or more elements in needed place of the array.
Example:-
const days=["monday","tuesday","thursday"]; days.splice(2,0,"wednesday"); console.log(days);//["monday","tuesday","wednesday" ,"thursday"]
4.Pop()
The Pop() method removes the last element of the array.
Example:-
const marks=[95,84,92,99,85]; marks.pop(); console.log(marks);//[95,84,92,99]
5.Shift()
The Shift() method removes the beginning element of the array.
Example:-
const places=["chennai","kovai","erode","salem"]; places.shift(); console.log(places);//["kovai","erode","salem"]
6.Splice()
The Splice() method is used to remove and replace one or more elements in needed place of the array.
Example:-
const data=['a', 'b', 'c', 'd', 'e','g','h', 'f'] data.splice(5,2); console.log(data);//['a', 'b', 'c', 'd', 'e', 'f']
7.Every(value,index,array )
The Every() method executes a function for each array element.The every() method returns true if the function returns true for all elements. The every() method returns false if the function returns false for one element.
Example:-
const check=[20,25,30,35,15]; const checked=check.every((value)=>value>20) console.log(checked);//false
8.Some(value,index,array )
The Some() method is used to check whether at least one of the elements of the array satisfies the given condition or not. It will return true if any predicate is true.
Example:-
const age=[10,15,12,19,11] const aged=age.some((value)=>value<11) console.log(aged);//true
9.Sort()
The Sort() method sorts the items of an array in a specific order(ascending or descending)
Example:-
const name=["ram","sai","jai","arthi"] name.sort(); console.log(name)//["arthi" ,"jai" ,"ram" ,"sai" ] const names=name.sort((a,b)=>b-a); console.log(names);//["ram","sai","jai","arthi"]
10.Reverse()
The Reverse() method returns the array in reverse order.It overwrites the original array.
Example:-
const number=[1,2,3,4,5] number.reverse(); console.log(number);//[5,4,3,2,1]
Note: By using the Splice() method we can do both , the add and remove elements in the array.
Rest of the array methods we will see in the next Blog.
ThankYou...