How to Remove last 2 Elements from an Array in Javascript?
Sep 19, 2022 . Admin
Hello dev,
This example is focused on how to remove last 2 elements from an array in javascript. it's simple example of how to remove last 2 elements in array using javascript. I explained simply about remove the last 2 elements from an array in javascript. you can understand a concept of remove last two elements from array javascript code example.
In this tutorial, I am going to explain to you to remove the last 2 elements from the array in javascript. Use the splice() method to remove the last 2 elements from an array. The splice method will delete the 2 last elements from the array .
So, let's see bellow solution:
Example: 1<!DOCTYPE html> <html> <head> <title>How to Remove last 2 Elements from an Array in Javascript? - MyWebtuts.com</title> </head> <body> <h1>How to Remove last 2 Elements from an Array in Javascript? - MyWebtuts.com</h1> <script type="text/javascript"> const fruits = ['Mango', 'Banana', 'Apple', 'Orange']; fruits.splice(fruits.length -2,2); //Give output to console... console.log(fruits); </script> </body> </html>Output:
['Mango', 'Banana']I hope it will help you...