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 JavaScript Functions Pass Information Into Functions Create a max() Function

Syntax Error when I use else in my if else statement

When I run this code it tells me SyntaxError: unexpected token e. It's the e in my else statement. What's going on?

script.js
function max(vikings, longShips){
  if (vikings > longShips){
    return vikings;}
} else {
    return longShips;
}
max(10, 5);

1 Answer

function max(vikings, longShips){
  if (vikings > longShips){
    return vikings;}
}

In this section of your code, you have two { and two }. Try removing one of the } between return vikings; and else.

Thank you so much, it runs now! I guess when if else statements are inside of functions you don't need curly braces like this: if(){

} else { }

For single lines of code, you don't need { and } around them. If you have multiple lines of code, then you need the { and }.

Your code was not of the form

if(){

} else { }

and was of the form

if(){

}
} else { }

This first has two { and two }. The second has two { and three }.