Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Passing arguments to a function

my_array.sort(function(a, b) {
    return a - b;

Typically when an argument is passed to a function it is done explicitly when the function is called. Am I to assume that in the code above javascript is passing arguments implicitly from the array? And it is being done two values at a time in their respective order?

Thanks Jeff

2 Answers

You do not have an array - which would be [a,b,c,d]

edit - oh i see, nice.

Jeff Busch,

I use javascript console.log() all the time to figure out JS problems.

[1, 3, 2, 4].sort(function(a, b) { console.log("a is: " + a + " and b is: " + b + ", so a - b is: " + (a-b)); } );

Put that through the console and you'll see the answer. Yes, array items are passed successively to the javascript function(a, b).