Warning: session_start(): open(/tmp/sess_1afda6e30875ca1038325068ad034e46, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Remove The Last Element From An Array In JS - LearnShareIT

How To Remove The Last Element From An Array In JS

Remove the last element from an array

Working with an array always is an important thing in JavaScript. How to remove the last element from an array? Let’s go into detail now.

Remove the last element from an array

Solution 1: Use pop() method

Pop method will remove the last element in your array and return it. This method will change your origin array.

Syntax:

array.pop()

Example:

const anArray = ["Tony", "Tommy", "Alex", "Alexa", "Jimmy", "Anna"];
console.log(anArray.pop());
console.log(anArray);

Output:

Anna
[ 'Tony', 'Tommy', 'Alex', 'Alexa', 'Jimmy' ]

Solution 2: Use slice() method

As you know, changing the original array always is a bad practice with developers. With slice method will help you to return a new array that removes the last element, and you can catch that array by assign to a variable.

Syntax:

array.slice(start, end)

Parameters:

  • Start: Position that you want to start to slice your array
  • End: Position that you want to end slice your array

If you don’t pass anything to the parameter slice method will return an array like an origin array. This is also a way to create a clone of your array. And slice method also can get negative numbers. With a negative number, it will count start from the end of the array. For example, with “-1,” it will be the index of the last element of your array. And you can remove the last index of your array in this way:

Example:

const anArray = ["Tony", "Tommy", "Alex", "Alexa", "Jimmy", "Anna"];
const arrChanged = anArray.slice(0, -1);
console.log(anArray);
console.log(arrChanged);

Output:

["Tony", "Tommy", "Alex", "Alexa", "Jimmy", "Anna"]
[ 'Tony', 'Tommy', 'Alex', 'Alexa', 'Jimmy' ]

Solution 3: Use the splice method

Splice method will remove your element in your array or add a new element. This method will change your origin array. I recommend you combine it with the slice method your something to create a new array and work on it.

Syntax:

splice(start, amount , element , elementN)

Parameters:

  • Start: Start the index position that you want to remove or insert a new element
  • Amount: define how many elements you want to remove from the start index
  • Element, elementN: element that you want to insert from the start index

Example:

const anArray = ["Tony", "Tommy", "Alex", "Alexa", "Jimmy", "Anna"];
const newArray = anArray.slice();
newArray.splice(-1, 1);
console.log(anArray);
console.log(newArray);

Output:

[ 'Tony', 'Tommy', 'Alex', 'Alexa', 'Jimmy', 'Anna' ]
[ 'Tony', 'Tommy', 'Alex', 'Alexa', 'Jimmy' ]

Summary

In this tutorial, I showed you some solutions to remove the last element from an array in Javascript. You can use slice(), splice(),pop() method to do it. There are a lot of ways to do it. I believe that you can find out more solutions to finish this work.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *