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

JavaScript JavaScript Foundations Arrays Methods: Part 2

How do arguments automatically get passed to a function when not manually assigned?

Example: var array = [4, 13, 7, 2]; array.sort(function(a, b){return a-b});

What passes the values of 'a' and 'b' to the function?

2 Answers

Hi alexeiklika,

You're passing that anonymous function into the sort method which it then uses to help sort the array.

The sort method will call that anonymous function internally and it will pass in two elements from the array for a and b.

The sort method will use a particular sorting algorithm to accomplish the sorting and as it's going through the sorting process it will call that anonymous function many times and pass in different elements from the array for a and b.

Thanks for your reply!