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 trialalexeiklika
Courses Plus Student 2,589 PointsHow 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
Jason Anello
Courses Plus Student 94,610 PointsHi 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
.
alexeiklika
Courses Plus Student 2,589 PointsThanks for your reply!