JavaScript | Remove the last item from an array - GeeksforGeeks

18 May, 2019
The task is to remove the last item from the array. Here are a few of the most preferred methods discussed.First few functions to understand.
JavaScript Array splice() methodThis method adds/deletes items to/from the array, and returns the deleted item(s).Syntax:array.splice(index, number, item1, ....., itemN)
Parameters:index: This parameter is required. It specifies the integer at what position to add/remove items, Negative values are used to specify the position from the end of the array.number: This parameter is optional. It specifies the number of items to be removed. 0 means, nothing to remove.item1, ....., itemN: This parameter is optional. This specifies the new item(s) to be added tothe array.Return value:Returns a new Array, having the removed items.
array.splice(index, number, item1, ....., itemN)

Parameters:
index: This parameter is required. It specifies the integer at what position to add/remove items, Negative values are used to specify the position from the end of the array.
number: This parameter is optional. It specifies the number of items to be removed. 0 means, nothing to remove.
item1, ....., itemN: This parameter is optional. This specifies the new item(s) to be added tothe array.
Return value:Returns a new Array, having the removed items.
JavaScript Array slice() MethodThis method returns a new array containing the selected elements.This method selects the elements starts from the given start argument and ends at, but excluding the given end argument.Syntax:array.slice(start, end)
Parameters:start: This parameter is optional. It specifies the integer from where to start the selection (first element is at index 0). Negative numbers are used to select from the end of the array. If not used, it acts like “0”end: This parameter is optional. It specifies the integer from where to end the selection. If not used, all elements from the start to the end of array will be included in selection. Negative numbers are used to select from the end.Return value:Returns a new Array, having the selected items.
JavaScript Array slice() MethodThis method returns a new array containing the selected elements.This method selects the elements starts from the given start argument and ends at, but excluding the given end argument.Syntax:
array.slice(start, end)

Parameters:
start: This parameter is optional. It specifies the integer from where to start the selection (first element is at index 0). Negative numbers are used to select from the end of the array. If not used, it acts like “0”
end: This parameter is optional. It specifies the integer from where to end the selection. If not used, all elements from the start to the end of array will be included in selection. Negative numbers are used to select from the end.
Return value:Returns a new Array, having the selected items.
JavaScript Array pop() MethodThis method deletes the last element of an array and returns the element.Syntax:array.pop()
Return value:It returns the removed array item. An array item can be a string, a number, an array, a boolean, or any other object types that are applicable in an array.Example 1: This example removes the last item from the array using splice() method.<!DOCTYPE HTML><html>  <head>    <title>        JavaScript       | Remove last item from array.    </title></head>  <body style="text-align:center;"      id="body">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP"       style="font-size: 16px;">    </p>    <button onclick="gfg_Run()">        remove    </button>    <p id="GFG_DOWN"       style="color:green;               font-size: 20px;              font-weight: bold;">    </p>    <script>        var el_up =             document.getElementById("GFG_UP");        var el_down =             document.getElementById("GFG_DOWN");        var array = [34, 24, 31, 48];        el_up.innerHTML = "Array = [" + array + "]";          function gfg_Run() {            array.splice(-1, 1);            el_down.innerHTML =               "Remaining array = [" + array + "]";        }    </script></body>  </html>Output:Before clicking on the button:After clicking on the button:Example 2: This example removes the last item from the array using pop() method.<!DOCTYPE HTML><html>  <head>    <title>        JavaScript       | Remove last item from array.    </title></head>  <body style="text-align:center;"       id="body">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP"        style="font-size: 16px;">    </p>    <button onclick="gfg_Run()">        remove    </button>    <p id="GFG_DOWN"        style="color:green;              font-size: 20px;               font-weight: bold;">    </p>    <script>        var el_up =             document.getElementById("GFG_UP");        var el_down =             document.getElementById("GFG_DOWN");        var array = [34, 24, 31, 48];        el_up.innerHTML = "Array = [" + array + "]";          function gfg_Run() {            array.pop();            el_down.innerHTML =               "Remaining array = [" + array + "]";        }    </script></body>  </html>Output:Before clicking on the button:After clicking on the button:Example 3: This example does not remove the last item from the array but returns a new array in which the item is removed, using splice() method.<!DOCTYPE HTML><html>  <head>    <title>        JavaScript       | Remove last item from array.    </title></head>  <body style="text-align:center;"      id="body">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP"        style="font-size: 16px;">    </p>    <button onclick="gfg_Run()">        remove    </button>    <p id="GFG_DOWN"        style="color:green;               font-size: 20px;               font-weight: bold;">    </p>    <script>        var el_up = document.getElementById("GFG_UP");        var el_down = document.getElementById("GFG_DOWN");        var array = [34, 24, 31, 48];        el_up.innerHTML = "Array = [" + array + "]";          function gfg_Run() {            el_down.innerHTML =               "Remaining array = [" + array.slice(0, -1) + "]";        }    </script></body>  </html>Output:Before clicking on the button:After clicking on the button:My Personal Notes
arrow_drop_upSave
JavaScript Array pop() MethodThis method deletes the last element of an array and returns the element.Syntax:
array.pop()

Return value:It returns the removed array item. An array item can be a string, a number, an array, a boolean, or any other object types that are applicable in an array.
Example 1: This example removes the last item from the array using splice() method.
<!DOCTYPE HTML><html>  <head>    <title>        JavaScript       | Remove last item from array.    </title></head>  <body style="text-align:center;"      id="body">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP"       style="font-size: 16px;">    </p>    <button onclick="gfg_Run()">        remove    </button>    <p id="GFG_DOWN"       style="color:green;               font-size: 20px;              font-weight: bold;">    </p>    <script>        var el_up =             document.getElementById("GFG_UP");        var el_down =             document.getElementById("GFG_DOWN");        var array = [34, 24, 31, 48];        el_up.innerHTML = "Array = [" + array + "]";          function gfg_Run() {            array.splice(-1, 1);            el_down.innerHTML =               "Remaining array = [" + array + "]";        }    </script></body>  </html>
Output:
Before clicking on the button:
After clicking on the button:
Example 2: This example removes the last item from the array using pop() method.
<!DOCTYPE HTML><html>  <head>    <title>        JavaScript       | Remove last item from array.    </title></head>  <body style="text-align:center;"       id="body">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP"        style="font-size: 16px;">    </p>    <button onclick="gfg_Run()">        remove    </button>    <p id="GFG_DOWN"        style="color:green;              font-size: 20px;               font-weight: bold;">    </p>    <script>        var el_up =             document.getElementById("GFG_UP");        var el_down =             document.getElementById("GFG_DOWN");        var array = [34, 24, 31, 48];        el_up.innerHTML = "Array = [" + array + "]";          function gfg_Run() {            array.pop();            el_down.innerHTML =               "Remaining array = [" + array + "]";        }    </script></body>  </html>
Output:
Before clicking on the button:
After clicking on the button:
Example 3: This example does not remove the last item from the array but returns a new array in which the item is removed, using splice() method.
<!DOCTYPE HTML><html>  <head>    <title>        JavaScript       | Remove last item from array.    </title></head>  <body style="text-align:center;"      id="body">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP"        style="font-size: 16px;">    </p>    <button onclick="gfg_Run()">        remove    </button>    <p id="GFG_DOWN"        style="color:green;               font-size: 20px;               font-weight: bold;">    </p>    <script>        var el_up = document.getElementById("GFG_UP");        var el_down = document.getElementById("GFG_DOWN");        var array = [34, 24, 31, 48];        el_up.innerHTML = "Array = [" + array + "]";          function gfg_Run() {            el_down.innerHTML =               "Remaining array = [" + array.slice(0, -1) + "]";        }    </script></body>  </html>
Output:
Before clicking on the button:
After clicking on the button:
javascript-array
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Difference Between PUT and PATCH Request
Set the value of an input field in JavaScript
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Top 10 Projects For Beginners To Practice HTML and CSS Skills