How to use slice() Method In Javascript
The Javascript arr.slice() method returns a new array containing a portion of the array on which it is implemented. The original remains unchanged.
Syntax:
arr.slice(begin, end)
Example: In this example, we will see the use of the slice() method.
let arr = ["w3wiki", "Geek", "GFG", "gfg","G"];
arr = arr.slice(0, 2);
console.log(arr);
Output
[ 'w3wiki', 'Geek' ]
How to truncate an array in JavaScript ?
In JavaScript, there are two ways of truncating an array. One of them is using length property and the other one is using splice() method. In this article, we will see, how we can truncate an array in JavaScript using these methods.
These are the following ways to truncate an array:
Table of Content
- Using length Property
- Using splice() Method
- Using slice() Method
- Using Lodash _.truncate() Method
- Using Array.prototype.pop() in a loop
Contact Us