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 Asynchronous Programming with JavaScript Asynchronous JavaScript with Callbacks Implement a Callback

How were you allowed to call the data parameter json? Its like he just gave it any random name because he could?

.

3 Answers

Owen Bell
Owen Bell
8,060 Points

The JSON object carries properties and methods useful for handling AJAX requests that send/receive a JSON object; for this reason, JSON is a reserved keyword in JavaScript.

On the other hand, the lower case json is not a reserved keyword: it is valid to use it as the name of an object, including the callback function to be executed when the getJSON function returns a valid status code from the server.

Amandeep Pasricha I'm just a simple country boy but let me take a shot at it in hopes that my explanation is a little simpler and goes into enough detail to provide some clarity to you hopefully.

The LOWERCASE json argument he used as an argument for the addEventListener placed on the button can be confusing but if take a step back from that and linstead ook at the 2nd parameter of the function getJSON() when it was declared he setup the getJSON function so that the 2nd argument is a callback function that in the body of the getJSON function setups up that callback function to have it's own argument which is the data you received once the getJSON gets the data than parses it for you and finally stores it in a variable called data. When the data variable is set up as the getJSON callback function's parameter (aka place holder) it is right there where you can see that the reason why he is able to just drop any random name in place of data when he uses the lowercase json as an argument where he is using getJSON function as a callback for the addEventListener.

Blessings, Jonathan

The reason why it's important/necessary to pay attention to the naming conventions when assigning that argument/parament a name other than what was initially used in the getJSON's declaration is b/c...

"Whenever the getJSON function is called, a new scope is created. So the data variable is uniquely defined within each invocation of the onload function handler insuring that it gets executed with the proper data on each iteration of the map." -Guil Hernandez (transcript ~4:23)

Ivan Frances Alcantara
Ivan Frances Alcantara
4,528 Points

I had the same problem. Completely missed a whole minute of video, so I very confused thinking getJSON only accepted one parameter. This comment definitely saved me some headaches. However, I think this is the first time we see a callback function argument can be passed when called even without explicitly including in in function definition. That's what's happening if I'm not wrong, that is.