We will be discussing the jQuery array to remove the last element with the help of an example. This article will provide you with a simple example of how to remove the last element from the array jQuery.
Let’s see both examples of how to remove the last key in the jQuery array.
Example 1 Remove Last Element from jQuery Array
<!DOCTYPE html> <html> <head> <title>How to Remove Last Element in Jquery Array?</title> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> </head> <body> <script type="text/javascript"> var sites = [ "Thor", "Elvis", "LightYear", "The Blck Phone" ]; sites.pop(); console.log(sites); </script> </body> </html>
Output
[“Thor”, “Elvis”, “LightYear”]
Example 2 Remove First Element from Array
<!DOCTYPE html> <html> <head> <title>How to Remove First Element from Jquery Array?</title> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> </head> <body> <script type="text/javascript"> var sites = [ "Thor", "Elvis", "LightYear", "The Black Phone" ]; sites.shift(); console.log(sites); </script> </body> </html>
Output
[“Elvis”, “LightYear”,”The Black Phone”]