View Code , Do not speak:
Copy code The Code is as follows: var I = arr. length;
While (I --)
{
// Write something?
}
Above is Google array. jsProgramCode to be improved. Think about why I -- when I is 0, the while loop is over?
The following code is equivalent to it:Copy codeThe Code is as follows: var I = arr. length;
For (; I --;)
{
// I -- is it strange to write it in the middle of two semicolons? Isn't it strange?
}
Shocking? No feeling?
Er. What do we usually write in the middle of two semicolons? This I-according to "Common Sense" should be after the second semicolon. Let's look at the C language code:Copy codeThe Code is as follows: int main ()
{
Int I = 5;
While (I --)
{
Printf ("% d", I );
}
While (1 );
Return 0;
}
The running result is surprisingly consistent with that of JavaScript!
Okay. I don't know why? Only if the value 0 is converted to a Boolean value of false:
VaR I = !! 0;
Other values are converted to a Boolean value of true. The code in the preceding example explicitly converts a value to a Boolean value.
Use the following JS program to test the implicit conversion of 0 to false:Copy codeThe Code is as follows: var I = 0;
If (I)
{
Alert ('if ');
}
Else {
Alert ('else ');
}
Alert ('the if and else programs should all come here ');
If this sample code is not "practical" at all "? Well, the following is a sample code snippet from a front-end developer of Tudou:Copy codeThe Code is as follows: var OBJ = {status: 0, MSG: 'xxxx '};
VaR DATA = obj. Status | 'xxxx ';
This is always "hidden!
once again, it is emphasized that the implicit conversion of the value 0 to the condition where the Boolean value is false is in if (), while (), (;;) conditional expressions for Boolean judgments such as the middle of two semicolons.