Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


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).