how boolean works in javascript

I don't understand how boolean works.In the following example what is the necessity of the line "var check=false" and how does it work?/when should I use them?what if I write "var check=true" instead??

var hits=[];
for(var n=2;n<101;n++){
    var check=false;
    for(var i=2;i<=9;i++){
        if(n%i===0 && n!=i)
            check=true;
    }
    if(check===false){
        hits.push(n);
    }
}

please explain boolean a bit .I'm stuck here


The boolean datatype is a 1-bit datatype that can either be set to "true" or "false". It is sometimes referred to as a flag variable to check if a certain condition is true.

In javascript, when you declare a variable, you can set it to any datatype, so saying var check=false; sets a variable check to the condition false.

All if() statements and while() statements check a boolean expression to see if that code will run or not.

So, after you declare check, if you said

if(check){...}

that code wouldn't run because check is false.

Hope this helps

链接地址: http://www.djcxy.com/p/13018.html

上一篇: 如果声明与! 变量之前

下一篇: 如何布尔在JavaScript中工作