Array: Arrays are used to store elements. Array is a collection of elements.
eg: let arr = [5, 44, 87, 9]
arr[0] = 5;
arr[1] = 44;
length is a method of array which returns total no of elements in the array.
//Arrays Methods:
i. push: push is used to insert a value into the array as last value
e.g: array.push(values)
ii. pop(): pop is used to remove only the last value of the array
eg: array.pop()
iii. unshift : unshift is used to insert value or values into the array as first value
e.g: array.unshift(values)
iv. shift(): shift is used to remove only starting vlaue of the array
eg: array.shift()
v. splice: splice(starting_index, deleteCount, insertValues)
It is used to delete any number of values or insert any number of values to the array
let arr = [5,4,6,7];
arr.splice(1,1)
arr.splice(1) -- will delete whole array from index 1
arr.splice(1, 2) -- will delete 2 values from index 1
arr.splice(1, 2, "shekhar") -- will delete 2 values from index 1 and add shkhar at index 1
vi. reverse is used to reverse the array.
eg: array.reverse();
arrays are mutable it changes the original array.
vii. includes : includes is used to check whether the value is included in the array or not.
viii. forEach(callback) : forEach is a method of arrays which accepts callback fxns as parameter and will apply the fxn to each and every element of array
Syntax : forEach(value, index)
Top comments (1)
Keep going:
splice,sort,map,filterandreduce.And then there are the non-mutating methods!