欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js 学习之路6: if...else...条件语句的使用

程序员文章站 2022-07-09 15:07:40
1.1 if (...) { ... } else { ... } 1.2 if (条件1) { ... } else if (条件2) { ... } else { ... } ......

1.1

if (...)

{

...

}

else

{

...

}

<!doctype html>
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<body>

<script charset = "utf-8">

function myfunction(){
    var x = 11;
    var y = x % 2;
    document.write(y + "<br>");
    if (x % 2 == 1){
        document.write("单数");
    }
    else
    {
        document.write("双数");
    }
}

myfunction();

</script>

</body>
</html>

1.2

if (条件1)

{

...

}

else if (条件2)

{

...

}

else

{

...

}

<!doctype html>
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<body>

<script charset = "utf-8">

function myfunction(x){
    var y = x % 2;
    if (x % 2 == 0){
        document.write("双数");
    }
    else if (x > 10)
    {
        document.write("大于10的单数");
    }
    else
    {
        document.write("普通单数");
    }
    
    document.write("<br>");
    document.write(x);
}

myfunction(15);

</script>

</body>
</html>