Unexpected token else


#1

Hello! I feel like I’m stumbling on the first hurdle here… what am I doing wrong? It prints just fine without the else statement, but returns the good ol’ “SyntaxError: Unexpected token else” message once I add the second bit.

I know versions of this question have been asked several times but I can’t seem to get it right… thanks in advance!

let moonPhase = ‘full’;
{
if (moonPhase === ‘full’);
console.log(‘Howl!’);
} else {
console.log(‘I swear I am not a werewolf.’);
}


#2

Else is an optional second part of an if-statement, it isn’t allowed anywhere else.
Therefore, whatever you have before your else else isn’t an if-statement.
Also, mind where you place semi-colons, they aren’t line terminators, they are part of the syntax of certain statements, and you shouldn’t put them anywhere else (so if you’re wondering if you should have one somewhere, what you’d do is look up what that statement looks like and whether it ends in semi-colon or not. To add to the confusion, javascript adds them automatically if you don’t, but you’d still need to know where they go or at least stick to writing in a way that they only end up where you’d want them


#3

Thank you for the pointers!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.