Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Kevin Becerra
Kevin Becerra
14,243 Points

I thought a return value could only be used once in a function so how does this code actually work when used?

function max(tabs, spaces){

if(tabs > spaces){

 return tabs; 

} else if(tabs < spaces){

 return spaces;

  }
 }

 console.log(max(1, 2));

//How does 2 actually get returned to me?

2 Answers

You are right, but not in this case. Since you have conditional statements set up your code can only go 1 possible route whenever the function is executed. Which in turn will only lead to one return statement.

if(x==1){
    return item1;
} else if (x==2) {
    return item2;
} else {
    return item3
}

You may have as many return statements as you wish. However only one of them will actually execute due to the conditions surrounding them.