1

I'm trying to learn JavaScript on Code Academy I am facing the following syntax problem. Tells me:

expected an identifier and instead saw 'else'. Missing ';' before statement

Here is the code:

If("Jon".length * 2 / (2+1) === 6);

{
    console.log("The answer makes sense!");

} 
else {

    console.log("Error. Error. Error.");
}
5
  • 1
    remove semicolon after if Commented Dec 16, 2013 at 10:53
  • 1
    Change the capital If to if Commented Dec 16, 2013 at 10:53
  • 2
    this question will attract hundred of answers :P Commented Dec 16, 2013 at 10:54
  • sidenote: console.log is not cross-browser. Old browsers will result in JavaScript error. Commented Dec 16, 2013 at 10:55
  • @ShivanRaptor He says he's working on codecademy so don't worry... Commented Dec 16, 2013 at 10:56

11 Answers 11

2

Omit the ; in If("Jon".length * 2 / (2+1) === 6);

The syntax of if is:

if(condition) {
  // what to happen
} else {
  // what to happen
}
Sign up to request clarification or add additional context in comments.

Comments

0

Remove the semicolon from the if statement.

Comments

0

You have a semicolon ; after you if statement. Remove it. It should be:

if("Jon".length * 2 / (2+1) === 6) {

   console.log("The answer makes sense!");

} else {

    console.log("Error. Error. Error.");

}

The correct usage of the if syntax is:

if (condition) {
   // Do something here
} else {
   // For instances where the condition hasn't met do something else
}

Comments

0

remove ; from

If("Jon".length * 2 / (2+1) === 6);

Comments

0

Remove semicolon ; at the end of if statement.

Change

If("Jon".length * 2 / (2+1) === 6);  

To

if("Jon".length * 2 / (2+1) === 6)

Comments

0

The if is in lowercase, and you need to remove the semicolon at the end of the if line.

if("Jon".length * 2 / (2+1) === 6) {
    console.log("The answer makes sense!");
} else {

    console.log("Error. Error. Error.");
}

Comments

0

you have a semicolon to much. it should be:

if (boolean statement) {
//do sth
}
else { }

currently your if statement ends directly after the boolean statement

2 Comments

That's helpful Vogel612
@user3106983 you don't actually need to thank everyone who posted an answer.. people who post answer generally assume they are helpful. you should only comment, if that is not the case, but also give the reasons why then. they can improve their answer if you do it right, and it probably helps then
0

remove the semicolon behind

if("Jon".length * 2 / (2+1) === 6)**;**

additional write in in lower characters

Comments

0

Semicolons in JavaScript are used to seperate statements!

In that case, remove the semicolon from this line and ensure if is in lowercase:

if("Jon".length * 2 / (2+1) === 6);

More on if/else statement syntax

Comments

0

A semicolon (;) separates JavaScript statements.

Normally you add a semicolon at the end of each executable statement.

Using semicolons also make it possible to write many statements on one line.

You have to remove the semicolon in your if statement.

Change your code

From

If("Jon".length * 2 / (2+1) === 6);

To

If("Jon".length * 2 / (2+1) === 6)

And please check out if.. else statement in javascript.

Comments

0

You may try this:

If ("Jon".length * 2 / (2+1) == 6) {
    console.log("The answer makes sense!");
} else {    
    console.log("Error. Error. Error.");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.