简体   繁体   中英

Else if and Else in JavaScript

I wrote a small javascript code, but it has a problem, the page can not show anything. I think the problem is in the line "else if..." or "else...", because if I comment these two lines, the code runs without any problem

<html>
<head>
    <script language="javascript">
        var var1 = window.prompt("please input");
        var var2 = window.prompt("please input2");
        var1 = parseFloat(var1);
        var2 = parseFloat(var2);

        if (var1< var2) {document.writeln("the second number is bigger")};
        else if (var1> var2) {document.writeln("the first number is bigger")};
        else {document.writeln("They are the same")};
    </script>
</head>
<body>
</body>
</html>

Your javascript should be like this

 var var1 = window.prompt("please input"); var var2 = window.prompt("please input2"); var1 = parseFloat(var1); var2 = parseFloat(var2); if (var1 < var2) { document.writeln("the second number is bigger"); } else if (var1 > var2) { document.writeln("the first number is bigger"); } else { document.writeln("They are the same"); } 

Should be:

<html>
<head>
    <script language="javascript">
        var var1 = window.prompt("please input");
        var var2 = window.prompt("please input2");
        var1 = parseFloat(var1);
        var2 = parseFloat(var2);

        if (var1 < var2) {
            document.writeln("the second number is bigger");
        }
        else if (var1 > var2) {
            document.writeln("the first number is bigger");
        }
        else {
            document.writeln("They are the same");
        }
    </script>
</head>
<body>
</body>
</html>

your semi colons were wrong

Try removing the semicolon, ; , from after the brackets of your if statements :

    if (var1< var2) {document.writeln("the second number is bigger")}
    else if (var1> var2) {document.writeln("the first number is bigger")}
    else {document.writeln("They are the same")}

Have a look at this SO answer: https://stackoverflow.com/a/17036218/4206206

Essestially, a semicolon isn't used to end a group of statements, but rather to end a single statement.


A point with your code, if you're using HTML5 you don't need the language="javascript" in your script tags:

<script language="javascript">

Can become simply

<script>
if (var1< var2) {document.writeln(" ... ")};
                                           ^

Your semicolons at the end of your if, else if, and else blocks are your issue. They are prematurely ending your if blocks.

Just make sure you parseFloat before you compare, because it just compares as string, and remember, 10 comes before 2, so by string 10 < 2 ! And you don't need the ; at the end:

var var1 = window.prompt("please input");
var var2 = window.prompt("please input2");
var1 = parseFloat(var1);
var2 = parseFloat(var2);

if (var1 < var2) {
  document.writeln("the second number is bigger");
} else if (var1 > var2) {
  document.writeln("the first number is bigger");
} else {
  document.writeln("They are the same");
}

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