19

Just wondering why i got an error with the following simple JavaScript function

function highest(){ 
  return arguments.sort(function(a,b){ 
    return b - a; 
  }); 
}
highest(1, 1, 2, 3);

Error messsage : TypeError: arguments.sort is not a function.

I am confused as arguments it is an array (i thought). Please help and explain why. Many thanks

3
  • 1
    That's because arguments is not an array, take a look at this demo. It's an Arguments Object, which only acts like an array. Same goes to many array-like objects returned by native functions such as .getElementsByClassName, for example, which returns an HTMLCollection object. Commented Feb 4, 2015 at 0:13
  • 3
    “I am confused as arguments it is an array.” arguments is not an array. It has a length and indexed properties, but if you check arguments instanceof Array, you’ll notice that it’s false (and that Object.prototype.toString.call(arguments) === '[object Arguments]'). As such, it doesn’t have methods from Array.prototype, including sort. Commented Feb 4, 2015 at 0:14
  • o ok it's not array, i keep thought it was an array, cause i do arguments[0], arguments[1] . why do i get a negative.... but thanks this clear things up for me Commented Feb 4, 2015 at 2:44

3 Answers 3

31

Because arguments has no sort method. Be aware that arguments is not an Array object, it's an array-like Arguments object.

However, you can use Array.prototype.slice to convert arguments to an array; and then you will be able to use Array.prototype.sort:

function highest(){ 
  return [].slice.call(arguments).sort(function(a,b){ 
    return b - a; 
  }); 
}
highest(1, 1, 2, 3); // [3, 2, 1, 1]
Sign up to request clarification or add additional context in comments.

4 Comments

In modern engines, you could also skip the conversion to an array, and just use Array.prototype.sort directly: [].sort.call( arguments, function(a,b) { ...
@Paulpro Personally I don't like altering an Arguments object, and I prefer using arrays than array-like objects. But true, [].sort.call(arguments) should also work (and according to IETester works even on IE5.5).
Thanks it was my bad as i keep thought it was an array, when i use arguments[0], arguments[1]. Things are clear now.
How would one go about sorting on a particular property from JSON array?
6

use spread syntax to convert arguments to a real Array:

   function highest(...arguments){ 
      return arguments.sort(function(a,b){ 
        return b - a; 
      }); 
   }
   highest(1, 1, 2, 3);

Output: (4) [3, 2, 1, 1]

Console

Comments

1

Another way to do this is by declaring the arguments as an array.

var myArguments = [1, 1, 2, 3];
var sortedArguments = [];

Thus the highest() can be defined as;

function highest(myArguments)
{ 
  return myArguments.sort(function(a,b)
  { 
    return b - a; 
  }); 
}
sortedArguments = highest(myArguments); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.