Array Methods in Javascript
constructor : Constructor is used to return the constructor function for an object.
<script>
var fruits = ['Apple', 'Mango','Banana']
var cons = fruits.constructor;
console.log(cons);
//output ƒ Array() { [native code] }
</script>
length : Length is used for sets the number of array elements
var fruits = ["apple","mango","banana"]
var len = fruits.length;
console.log("Lenght of array ",len);
//output : Lenght of array 3
concat() : Concat is used to join array, using the concat we can join two and more array and return the new array.
var fruits =["Apple","Mango", "Orange"]
var colors = ["Orange","Red","Yellow"]
var concat_array = fruits.concat(colors);
console.log(concat_array)
//output (6) ['Apple', 'Mango', 'Orange', 'Orange', 'Red', 'Yellow']
CopyWithin() : Copywithin array function is used for copy the array elements to another position in the array
it will overwrite the existing value.
var fruits =["Apple","Mango", "Orange"]
fruits.copyWithin(2,0)
console.log(fruits)
// output (3) ['Apple', 'Mango', 'Apple']
fill() is used to fills the specified elements in the array using fill function you can specify the position of where
to start and the filling values if you will not specify the position all elements of array will be filled.
var fruits =["Apple","Mango", "Orange"]
fruits.fill("banana")
console.log(fruits)
forEach() : forEach method is used to call a function for each element in an array in some order.
let ages = [12, 15, 25, 65]
ages.forEach(testfunction)
function testfunction(item, index, arr_num){
arr_num[index] = item * 2;
}
console.log(ages)
// output (4) [24, 30, 50, 130]
Keep Learning 🙂