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

Eddie Lam
Eddie Lam
2,681 Points

Why do you need a return keyword before getJSON(wikiUrl + person.name); ?

I have tried removing the return keyword before the getJSON(wikiURL + person.name); and nothing changes; the code still run as before. Why did we have to include the return there?

1 Answer

If you provide a link to the lesson, it’d be a little easier to provide the most relevant answer, but the return keyword exits out of a function. Take for example...

function addTwelve(x) {
  if (+x) {
    return +x + 12;
  }
  return not a number
}

If you were to call addTwelve and pass in the number 5 as an argument, it would return the number 17, but if you were to remove the first return statement then call addTwelve and pass in the number 5 as an argument, 5 and 12 would be added together, and still the string not a number would be returned from the function. But if the if condition evaluates to true, then the sum of 12 and x is returned, and the interpreter never even gets to the second return statement.

Sometimes a function doesn’t need to return a value, but you’ll want to skip evaluating certain lines of code if a certain condition is true or false, in which case you would just type the return keyword by itself (solely for the purpose of exiting the function).

Does that make sense?