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 AJAX Basics (retiring) AJAX and APIs Displaying the Photos

carine todmia
carine todmia
6,071 Points

function displayPhotos(data)

one thing about javascript im not understanding is when you can randomly name things, or when you have to first declare it as a variable. for example, for the function displayPhotos() where did the word data come from? do you call it data by default when your using the $.getJSON method or could you have name it something else? im also no understanding how this was used in the $.each to name (i, photos). i just dont understand why you can do this.

Blake Feggans
Blake Feggans
11,172 Points

When declaring a function as a variable in JS, you also declare the arguments that a function will receive. In this case (basing this off of memory as there is no code snippet), displayPhotos was declared like this with a data argument. That data argument can then be used as a parameter in the function.

For example, if you call the following function as printName('Carine'); it will log your name to the console. If you call the following without a name, it will log an error to the console.

var printName = function(name){
  if (name){
    console.log(name)
  } else {
    console.log('Enter your name!')
  }
};

Hope this helps!

3 Answers

Blake Feggans
Blake Feggans
11,172 Points

The argument is just a placeholder for data. It does not have any inherent properties. You can define what the argument should contain within the function body.

For example, if you call printName(9); 9 will be printed to the console.

carine todmia
carine todmia
6,071 Points

thanks that helps a lot

carine todmia
carine todmia
6,071 Points

sounds good so far. can you further ellaborate on "declare the arguement". in the example you gave, does "name" have to come in a specific form such as a string? i guess im just not understanding the argument part. does the data "argument" have inherent properties in js, or do you give it properties based on how you use it in your function?

Okay so there are two things, function parameters and function arguments. Parameters are what you set which tells the function what to expect. Arguments are what you actually pass to (and are received by) the function (or at least that's how I understood it... admins / moderators, etc., please correct me if I'm wrong).

function myFunction(parameter1, parameter2, parameter3) {
    // code to be executed
}

So in the example in the project walk-through that Dave is leading us through in the videos:

function displayPhotos(data) {
   // this will be the json data returned by jquery parsed from the server to js
}

"data" is the parameter you set which tells the function what's going to be passed in to/received by the function. Once something is passed in, then there will be (not yet written) code within the function that does something with the data.