Array.prototype.reject(): TypeError: ....reject is not a function

Array.prototype.reject does not exist in vanilla JavaScript.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

It seems that you were using the example provided by funfunfunction: https://www.youtube.com/watch?v=BMUiFMZr7vk

Notice he never actually executes any of the code he demonstrates in this video.

As others have said, reject does exist in lodash/underscore, or the same result can be derived from using filter, as Dymos suggests.


The reject function is like .filter but in reverse, so you can. You can use the filter function and reverse the callback with the Logical Operator NOT to reverse the outcome.

Array.prototype.reject = function(fn){return this.filter(x => !fn(x))}
  • fn is the callback

  • this. is the array that the reject function has been called on.

  • x is the current entry of that array


Array.prototype.reject isn't a thing unless a library/custom code adds the reject method to Arrays.

To achieve what you want, you should use the Array.prototype.filter method like you already are. I'm not sure what you think is longer about it because you can write it the same way:

var animals = [
  { name: 'Fluffykins', species: 'rabbit' }, 
  { name: 'Caro', species: 'dog' }, 
  { name: 'Jimmy', species: 'fish' }
];

function noDogs(animal) {
  return animal.species !== 'dog';
}

var otherAnimals = animals.filter(noDogs);
console.log(otherAnimals);

As mentioned above, this was mentioned in a youtube series about functional programming. The video uploader did correct this mistake with an annotation, but you may not have seen that if you were viewing the video on a mobile device.

enter image description here

Tags:

Javascript