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 Functions Return Values

Jean Kaluza
Jean Kaluza
3,522 Points

When did we talk about passing in an array to a function?

So I'm totally lost on this one, guys. I just learned what an array was and am barely grasping what a function is. Which particular video talks about passing in an array to a function? I must have missed it.

2 Answers

Erik Nemesis
Erik Nemesis
13,356 Points

A function is a list of operations to execute in a row.

For instance, let's say you want to wash your clothes. That operation consists in three operations:

  • Take your clothes
  • Put them in the washing machine
  • Run the machine

In code, that would result in a function like such:

function wash(clothes) {
  var machine = new Machine(clothes);
  run(machine);
}

In this case, wash is a function (and so does "run"), clothes is an argument, and var machine ... is the body of the function. Also, because "clothes" is a set of one or many clothes, it is often represented as an Array, that you would have initialized before as such:

var clothes = ["shirt", "trousers", "pants"];

Hope that helped grasp the idea!

Passing an array to a function is in no way different from passing some other type.

var myArray = [1, 2, 3];

function someFunc (someParam) {
// do something
}

someFunc(myArray);