关于% 探究

%在JS中是取余的意思   但是%也有其他用法 例子如下

html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>

<script type="text/javascript">
    var i=1;
    var b=2;
    var c=(i++);
    alert(c);
    var d=++i;
//    if ((i++)%2){
//        alert("Y");
//    }else {alert("N");}

//    if (i++%2){
//        alert("Y");
//    }else {alert("N");}
//
//    if (b%2){
//        alert("Y");
//    }else {alert("N");}

//    if (d%2){
//        alert("Y");
//    }else {alert("N");}

//    if (i%2){
//        alert("Y");
//    }else {alert("N");}

script>
body>
html>
c=1

第一个运行的结果是 Y   (1++)%2   这里提一下(i++)并不会先运行++    想先++再运行  必须++i

第二个运行的结果是Y 同上  1%2 的结果是1 在判断中1为true 0为false

第三个运行的结果是N

第四个运行的结果是N

第五个运行的结果是Y

你可能感兴趣的:(关于% 探究)