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 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 }.