How to use while loop In Javascript
By using the while loop we can iterate over the array and we will use pop() method for removing the element from the end.
Example: In this example, we are using while loop.
const arr = [1, 2, 3, 4, 5, 6];
//remove the last 3 elements from an array
let n = 3;
while (n > 0) {
n -= 1;
arr.pop();
}
console.log(arr);
Output
[ 1, 2, 3 ]
How to remove n elements from the end of a given array in JavaScript ?
In this article, we will learn how to remove the last n elements from the end of the given array in JavaScript.
We can remove n elements from the end of a given array in the following ways:
Table of Content
- Method 1: Using splice() Method
- Method 2: Using pop() Method
- Method 3: Using filter() Method
- Method 4: Using Array slice() method
- Method 5: Using while loop
Contact Us