Problem script
JS Code
- VaR STR = 'false ';
- If (STR ){
- Alert ('true ');
- } Else {
- Alert ('false ');
- }
VaR STR = 'false'; If (STR) {alert ('right');} else {alert ('false ');}
Run the preceding script and you will find that the prompt box "correct" appears in STR, whether it is "true" or "false. (It seems that this weak JavaScript type still has a type .)
Official notes:
Note:If the value parameter is omitted, or is 0,-0, null, "", false, undefined, Or Nan, the object is set to false. otherwise it is set to true (even with the string "false ")!
The constructors of Boolean are "true" for strings as long as they are not empty ";
Solution:
JS Code
- VaR STR = 'false ';
- If (STR = 'true '){
- Alert ('true ');
- } Else {
- Alert ('false ');
- }
VaR STR = 'false'; If (STR = 'true') {alert ('true');} else {alert ('false ');}
In this way, all values except true are false;