Array Methods in Javascript

constructor : Constructor is used to return the constructor function for an object.

var fruits = ['Apple', 'Mango','Banana']
var cons = fruits.constructor;

//output ƒ Array() { [native code] }

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);

//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"]
// 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"]

forEach() : forEach method is used to call a function for each element in an array in some order.

let ages = [12, 15, 25, 65]

function testfunction(item, index, arr_num){
arr_num[index] = item * 2;

// output (4) [24, 30, 50, 130]

Keep Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *