How to Remove First, Last Element From Array in Javascript?

12-May-2023

.

Admin

How to Remove First, Last Element From Array in Javascript?

In this example, I will show you remove first. I would like to show you last element from array javascript. if you want to see example of to remove first and last element in array then you are a right place. you will learn remove first.

To remove the first element from an array in Javascript, you can use the shift() method. To remove the last element from an array, you can use the pop() method. Here is an example:

Example 1:


<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>How to Remove First, Last Element From Array in Javascript? - NiceSnippets.Com</title>

</head>

<body>

</body>

<script type="text/javascript">

let myArray = [1, 2, 3, 4]; // Create an array

myArray.shift(); // Remove first element

myArray.pop(); // Remove last element

console.log(myArray); // Output: [2, 3]

</script>

</html>

#JavaScript