简体   繁体   中英

JavaScript IF / ELSE error - expected an identifier

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.");
}

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

The syntax of if is:

if(condition) {
  // what to happen
} else {
  // what to happen
}

从if语句中删除分号。

You may try this:

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

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
}

remove ; from

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

Remove semicolon ; at the end of if statement.

Change

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

To

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

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.");
}

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

remove the semicolon behind

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

additional write in in lower characters

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM