当前位置: 动力学知识库 > 问答 > 编程问答 >

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: