[image] argisht333: My question is : why doesn’t myArray stay =[[“John”, 23], [“cat”, 2]]; Because you are using the pop method const removedElement = myArray.pop(); Here pop not only returns the element to the removedElement it removed from myArray, but pop also mutates the array or modifi… Answer from Cody_Biggs on forum.freecodecamp.org
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › pop
Array.prototype.pop() - JavaScript | MDN
The pop() method of Array instances removes the last element from an array and returns that element. This method changes the length of the array.
🌐
W3Schools
w3schools.com › python › ref_list_pop.asp
Python List pop() Method
Note: The pop() method returns removed value. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
Discussions

Basic JavaScript - Manipulate Arrays With pop Method
Tell us what’s happening: My question is : why doesn’t myArray stay =[[“John”, 23], [“cat”, 2]]; I mean we do the .push() step, but doesn’t it only relate to removedFromMyArray? It’s like the same as I write “const removedFromMyArray = [“cat”, 2]”; so why does this has ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
November 29, 2023
Javascript array pop() function
In the following example i have used the following code but its not working. const myArray =[[“John”, 23], [“cat”, 2]]; i was ask to make myArray equals [[“John”, 23]]; after using pop() function on myArray . then make const removedFromArray equals [[“cat”, 2]]; Your code so ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
October 12, 2022
Array.pop - last two elements - JavaScript - Stack Overflow
I have a question using Array.pop function in JavaScript. Array.pop removes and returns the last element of an Array. My question is then: is it possible to remove and return the last TWO elements of More on stackoverflow.com
🌐 stackoverflow.com
How can I remove a specific item from an array in JavaScript? - Stack Overflow
How do I remove a specific value from an array? Something like: array.remove(value); Constraints: I have to use core JavaScript. Frameworks are not allowed. More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com › jsref › jsref_pop.asp
JavaScript Array pop() Method
The pop() method removes (pops) the last element of an array.
🌐
daily.dev
daily.dev › home › blog › get into tech › js pop: understanding its role
JS Pop: Understanding Its Role
December 22, 2025 - The pop() method in JavaScript takes off the last item from an array and gives it back to you. If there's nothing in the array, it gives back undefined because there's nothing to remove.
Find elsewhere
🌐
Mimo
mimo.org › glossary › javascript › array-pop
JavaScript Array pop() method: Syntax, Usage, and Examples
The pop() method in JavaScript removes the last element from an array and returns that element. It changes the original array by reducing its length by one. The JavaScript array pop method is often used when working with stacks, where the last item added is the first one removed (LIFO – Last ...
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Javascript array pop() function - JavaScript - The freeCodeCamp Forum
October 12, 2022 - In the following example i have used the following code but its not working. const myArray =[[“John”, 23], [“cat”, 2]]; i was ask to make myArray equals [[“John”, 23]]; after using pop() function on myArray . then make const removedFromArray equals [[“cat”, 2]]; Your code so far can’t solve this i need help my code so far: const myArray = [[“John”, 23], [“cat”, 2]]; var ab = myArray.pop(); var removedFromMyArray = myArray.pop();
🌐
W3Schools
w3schools.com › js › js_array_methods.asp
JavaScript Array Methods
Shifting is equivalent to popping, but working on the first element instead of the last. The shift() method removes the first array element and "shifts" all other elements to a lower index.
🌐
Wikipedia
en.wikipedia.org › wiki › JavaScript
JavaScript - Wikipedia
December 31, 2025 - People do pop-ups or those scrolling messages in the old status bar at the bottom of your old browser." In November 1996, Netscape submitted JavaScript to Ecma International, as the starting point for a standard specification that all browser vendors could conform to.
🌐
GitConnected
levelup.gitconnected.com › pop-and-push-learning-javascripts-array-methods-by-building-them-fed7096cf6f0
pop and push: Learning Javascript’s Array Methods by Building Them | by Zakk Fleischmann | Level Up Coding
February 3, 2021 - A Slice in Go is a data structure equivalent to an Array in JavaScript, where an Array in Go acts like the more traditional Array-type. Arrays in Go have a fixed length that can’t be changed after initialization. Otherwise, I haven’t heard a good mnemonic for remembering the difference. At any rate, I can really simplify the implementation of my pop method by using slice:
🌐
Medium
medium.com › an-idea › javascript-arrays-push-pop-shift-unshift-adc8fb815fc0
JavaScript Arrays: push(), pop(), shift() & unshift() | by Amanda M Johnson | An Idea (by Ingenious Piece) | Medium
October 10, 2021 - works from the beginning of the array and removes the first element · like pop(), it returns the removed element or undefined · is destructive · relies on length property · Press enter or click to view image in full size · from MDN web ...
🌐
Bennadel
bennadel.com › blog › 1796-javascript-array-methods-unshift-shift-push-and-pop.htm
Javascript Array Methods: Unshift(), Shift(), Push(), And Pop()
April 21, 2020 - The push() method can take multiple ... array, take a look at the concat() method. The pop() method pulls the last element off of the given array and returns it....
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Array › pop
JavaScript Array pop() - Remove Last Element | Vultr Docs
December 9, 2024 - The pop() method is a fundamental array operation in JavaScript, designed to remove the last element from an array.
🌐
CodingNomads
codingnomads.com › javascript-array-unshift-shift-pop-push
JavaScript Array Essentials: Using pop, push, shift, and unshift
This analogy encapsulates how these ... structures in programming. Using .push() and .pop() on a JavaScript array allows you to simulate stack-like behavior....
🌐
Sabe
sabe.io › blog › javascript-push-pop-shift-unshift-array-methods
Push, Pop, Shift, and Unshift Array methods in JavaScript
February 2, 2022 - JAVASCRIPTlet fruits = ["apple", "banana", "orange"]; fruits.pop(); console.log(fruits); // ['apple', 'banana'] ... Use the shift method to remove an element from the start of an array.
Top answer
1 of 16
17015

Find the index of the array element you want to remove using indexOf, and then remove that index with splice.

The splice() method changes the contents of an array by removing existing elements and/or adding new elements.

const array = [2, 5, 9];

console.log(array);

const index = array.indexOf(5);
if (index > -1) { // only splice array when item is found
  array.splice(index, 1); // 2nd parameter means remove one item only
}

// array = [2, 9]
console.log(array);

The second parameter of splice is the number of elements to remove. Note that splice modifies the array in place and returns a new array containing the elements that have been removed.


For completeness, here are functions. The first function removes only a single occurrence (e.g., removing the first match of 5 from [2,5,9,1,5,8,5]), while the second function removes all occurrences:

function removeItemOnce(arr, value) {
  var index = arr.indexOf(value);
  if (index > -1) {
    arr.splice(index, 1);
  }
  return arr;
}

function removeItemAll(arr, value) {
  var i = 0;
  while (i < arr.length) {
    if (arr[i] === value) {
      arr.splice(i, 1);
    } else {
      ++i;
    }
  }
  return arr;
}
// Usage
console.log(removeItemOnce([2,5,9,1,5,8,5], 5))
console.log(removeItemAll([2,5,9,1,5,8,5], 5))

In TypeScript, these functions can stay type-safe with a type parameter:

function removeItem<T>(arr: Array<T>, value: T): Array<T> {
  const index = arr.indexOf(value);
  if (index > -1) {
    arr.splice(index, 1);
  }
  return arr;
}
2 of 16
2617
  • Do it simple, intuitive and explicit (Occam's razor)
  • Do it immutable (original array stays unchanged)
  • Do it with standard JavaScript functions, if your browser doesn't support them - use polyfill

In this code example I use array.filter(...) function to remove unwanted items from an array. This function doesn't change the original array and creates a new one. If your browser doesn't support this function (e.g. Internet Explorer before version 9, or Firefox before version 1.5), consider polyfilling with core-js.

Be mindful though, creating a new array every time takes a big performance hit. If the list is very large (think 10k+ items) then consider using other methods.

Removing item (ECMA-262 Edition 5 code AKA old style JavaScript)

var value = 3

var arr = [1, 2, 3, 4, 5, 3]

arr = arr.filter(function(item) {
    return item !== value
})

console.log(arr)
// [ 1, 2, 4, 5 ]

Removing item (ECMAScript 6 code)

let value = 3

let arr = [1, 2, 3, 4, 5, 3]

arr = arr.filter(item => item !== value)

console.log(arr)
// [ 1, 2, 4, 5 ]

IMPORTANT ECMAScript 6 () => {} arrow function syntax is not supported in Internet Explorer at all, Chrome before version 45, Firefox before version 22, and Safari before version 10. To use ECMAScript 6 syntax in old browsers you can use BabelJS.


Removing multiple items (ECMAScript 7 code)

An additional advantage of this method is that you can remove multiple items

let forDeletion = [2, 3, 5]

let arr = [1, 2, 3, 4, 5, 3]

arr = arr.filter(item => !forDeletion.includes(item))
// !!! Read below about array.includes(...) support !!!

console.log(arr)
// [ 1, 4 ]

IMPORTANT array.includes(...) function is not supported in Internet Explorer at all, Chrome before version 47, Firefox before version 43, Safari before version 9, and Edge before version 14 but you can polyfill with core-js.

Removing multiple items (in the future, maybe)

If the "This-Binding Syntax" proposal is ever accepted, you'll be able to do this:

// array-lib.js

export function remove(...forDeletion) {
    return this.filter(item => !forDeletion.includes(item))
}

// main.js

import { remove } from './array-lib.js'

let arr = [1, 2, 3, 4, 5, 3]

// :: This-Binding Syntax Proposal
// using "remove" function as "virtual method"
// without extending Array.prototype
arr = arr::remove(2, 3, 5)

console.log(arr)
// [ 1, 4 ]

Try it yourself in BabelJS :)

Reference

  • Array.prototype.includes
  • Functional composition