Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript TypeError – Invalid Array.prototype.sort argument

  • Last Updated : 19 Aug, 2020

This JavaScript exception invalid Array.prototype.sort argument occurs if the parameter of Array.prototype.sort() is not from either undefined or a function which sorts accordingly.

Message:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

TypeError: argument is not a function object (Edge)
TypeError: invalid Array.prototype.sort argument (Firefox)

Error Type:

TypeError

Cause of the Error: The argument is passed to the sort() method should be either undefined or a function which compares its operands.



Example 1: In this example, argument passed to sort() method is not expected, So the error has occurred.

HTML




<script>
[2, 5, 6, 1, 3].sort(3); // error here
</script>

Output:

TypeError: argument is not a function object

Example 2: In this example, argument passed to sort() method is not expected, So the error has occurred.

HTML




<script>
var compareFun = { 
    Asc: (a, b) => a >= b, 
    Dsc: (a, b) => a <= b 
};
  
// TypeError
[1, 2, 3, 6, 5, 8].sort(
    compareFun[this.key] || 'Asc');
</script>

Output:

TypeError: argument is not a function object
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!