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 trialKevin Becerra
14,243 PointsI 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
Alexander La Bianca
15,959 PointsYou 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.
matthew jimenez
13,599 Pointsif(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.