Error: Unexpected token else in 6/11

I get “SyntaxError: Unexpected token else” in 6/11 when I write the code myself, but it runs just fine if I push “Get code” or if I copy/paste the code “Get code” gives me. I swear the code is identical in both cases.

let moonPhase = ‘full’;

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

ETA: I get the same error in 7/11

When an error is pointed out, look for the cause. In this case, the else is not the cause; therefore, it must be higher up in the code. Check the syntax.

The fact still remains, though, that the code I’ve written is identical to what the “Get code” gives me, but my text won’t compile, unlike the identical corrected version.

Did you locate the syntax error?

Recall that in JS, {} is not just an object, but a code block which is treated as a statement.

No, I cannot find it. Everything seems in order according to all hints and explanations.

Oh. No, I found it. Amazing how I could have sworn it was identical. -_-

To separate statements we use ; (semi-colon) which indicates end of statement. The script is parsed in statements.

if (moonPhase === ‘full’);

The next statement that is parsed has no connection to this one. There is the hidden syntax error. A conditional expression missing its code block.

Yeah, I finally realised. Thanks for the help. I can’t believe I’m that blind…

1 Like

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