Here are some JavaScript Arrays’ Methods.
Array push()
The push() method adds a new element to the end of the array.
vehicles.push(“Plane”);
//Output -> [“Car”, “Bus”, “Van”, “Lorry”, “Bike”, “Plane”]
and also you can get new array length by implementing like this,
let length = vehicles.push(“Plane”);
//Output -> 6
Array pop()
The pop() method is used to remove the last element of the array.
vehicles.pop();
//Output -> [“Car”, “Bus”, “Van”, “Lorry”]
If you want to get the pop value you need to implement like this,
let value = vehicles.pop();
//Output -> Bike
Array shift()
By using this shift() method, you can remove the array first element. Refer the below example.
vehicles.shift();
//Output -> [“Bus”, “Van”, “Lorry”, “Bike”]
If you want to get the removed value, use
let value = vehicles.shift();
//Output -> Car
Array unshift()
If you want to add a new element to the beginning, this is the method for that.
vehicles.unshift(“Plane”);
//Output -> [“Plane”, “Car”, “Bus”, “Van”, “Lorry”, “Bike”]
Array concat()
The concat() method creates a new array by merging existing arrays. If you want to merge two arrays, you can use this.
const array02 = [“D”, “E”, “F”];
const newArray = array01.concat(array02););
//Output -> [“A”, “B”, “C”, “D”, “E”, “F”]
Here is another example,
//Output -> [“A”, “B”, “C”, “D”]
Array join()
//Output -> A-B-C
Here is another example,
//Output -> A and B and C
Array slice()
The slice() method slices out elements. If you use slice(1), array will remove first element. If you use slice(3), array will remove first 3 elements.
//Output -> [“B”, “C”]
const array01 = [“A”, “B”, “C”].slice(2);
//Output -> [“C”]
Array splice()
This is splice, not slice. Do not mess with this. Lol. The splice() method can be used to add new items to an array:
array01.splice(2, 0, “C”, “D”);
//Output -> [ ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ ]
In here, “2” means the position where new elements should be added and the “0” means, how many elements should be removed. If you replace 0 with any number, let assume you will replace 0 with 1, you will get,
array01.splice(2, 1, “C”, “D”);
//Output -> [ ‘A’, ‘B’, ‘C’, ‘D’ ]
It means last element will remove from the array. If you add 3, the output will be [ ‘A’, ‘B’, ‘C’, ‘D’ ].
array01.splice(2, 3, “C”, “D”);
//Output -> [ ‘A’, ‘B’, ‘C’, ‘D’ ]
Because there is no 3 elements after you added position 2 (It means after “B” only has “E”. if your array has [“A”, “B”, “E”, “F”, “G”], then you will see after position 2, elements will remove).
Array indexOf()
If you want to get the index of an element, you can use this method.
//Output -> 1
Array includes()
If you want to find out an element is in your array or not, you can use this method.
//Output -> True
Array find()
//Output -> 6
Also you can use this method like this,
function checkAge(age) {
return age > 18;
}
const array01 = ages.find(checkAge);
//Output -> 20
Array findIndex()
Find the index that you need to find something.
//Output -> 3
Another example,
function checkAge(age) {
return age > 18;
}
const array01 = ages.findIndex(checkAge);
//Output -> 3
Array Map()
I am using map() method to multiply by 2 array elements.
//Output -> [ 4, 8, 6, 10 ]
function multiple(No) {
return No * 2;
}
const array01 = No.map(multiple);
//Output -> [ 4, 8, 12, 16 ]
Array filter()
//Output -> [6, 8]
Here is some documentation for more details – Click here
Array reduce()
//Output -> 16
Here is another perfect example for this JavaScript Arrays’ Methods – Click here
Array every()
I am checking my every array element greater than 6 or not.
//Output -> true
Array some()
I am checking my array has element that greater than 6.
//Output -> true
Array reverse()
If you want to reverse your array element you can use this method.
//Output -> [8, 6, 5, 3]
This is the end os “JavaScript Arrays’ Methods” article. Thank you for reading. If you are interesting on my article, make sure to follow my other articles as well and also make sure to leave a comment.
- Android Studio Articles – https://builditmasters.com/category/android-studio/
- Android Studio Firebase Tutorial – https://builditmasters.com/category/android-studio-firebase-tutorial/
- C Programming – https://builditmasters.com/category/programming/
- Flutter – https://builditmasters.com/category/flutter/
- GitHub Tutorials – https://builditmasters.com/category/github/
- Java Programming – https://builditmasters.com/category/java-programming/
- MERN / MEVN Stacks – https://builditmasters.com/category/mern_mevn_stacks/
- Tech News – https://builditmasters.com/category/tech-news/
- Theory Lessons – https://builditmasters.com/category/theory-lessons/
- Adobe Tutorials – https://builditmasters.com/category/adobe-tutorials/