There are many methods of available in arrays of JavaScript
Array length,toString(), at() join(), pop(), push() shift(), unshift()
The length method is used to check the length of the array
const array = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; console.log(array.length)
The array at() and [] are used to fetch out the index which u want but at() also support negative index
const array = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; console.log(array[2]) console.log(array.at(2))
Join method is used to join elements in an array like this one:
Banana...Orange...Lemon...Apple...Mango
const array= ["Banana", "Orange", "Lemon", "Apple", "Mango"]; let joinedArray = array.join("..."); console.log(joinedArray)
Pop() methods is used to remove the last element of an array
This the output of array the original array code show in the bottom
[ 'Banana', 'Orange', 'Lemon', 'Apple' ]
const array= ["Banana", "Orange", "Lemon", "Apple", "Mango"]; array.pop() console.log(array)
Push() methods is used to add elements last of the array
This the output of array the original array code show in the bottom
[ 'Banana', 'Orange', 'Lemon', 'Apple' 'Kiwi']
const array= ["Banana", "Orange", "Lemon", "Apple", "Mango"]; array.push("Kiwi") console.log(array)
Slice() methods extracts a section of an array and returns a new array. It doesn't effect the original array.
const arr = [1, 2, 3, 4, 5]; const newArr = arr.slice(1, 3); console.log(newArr); // [2, 3] console.log(arr); // [1, 2, 3, 4, 5] (original array remains unchanged)
Adds and remove the items in the last of the array and it changes the original array
const arr = [1, 2, 3, 4, 5]; // Removing elements arr.splice(1, 2); console.log(arr); // [1, 4, 5] // Adding elements arr.splice(1, 0, 'a', 'b'); console.log(arr); // [1, 'a', 'b', 4, 5]
There are many types of functions available in the JavaScript library, few of them are as follows:
function functionName(parameters) { // code to execute }