In this article we will learn about some basic array methods and their uses.
The slice() method Will return new array but only with the extracted parts
let arr = ['a', 'b', 'c', 'd', 'e']; arr.slice(2) // ['c'.'d','e'] arr.slice(2, 4) // ["c", "d"] arr.slice(-2) // ["d", "e"] arr.slice(1, -2) // ["b", "c"]
The splice() method works the same as slice but difference is it does actually change the original array and Extracted elemented will gone from origianl array.
Basically we use it to just delete one or more eleemnet frpm an array using splice
arr.splice(2); arr // ["a", "b"]
Remove last element using splice() method:
The reverse method is used to reverse array element.
const arr2 = ['j', 'i', 'h', 'g', 'f']; console.log(arr2.reverse()); console.log(arr2);
const letters = arr.concat(arr2); console.log(letters); console.log([...arr, ...arr2]);
console.log(letters.join(' - '));
Do let me know If you face any difficulties please feel free to comment below we love to help you.
If you have any feedback suggestion then please inform us by commenting.