基本使用
var b; b = true; alert(b); //true b = new Boolean(false); alert(b); //false b = 2 > 1; alert(b); //true b = 2 < 1; alert(b); //false
转换到布尔
var b; b = Boolean(123); alert(b); //true b = Boolean('ABC'); alert(b); //true b = Boolean(0); alert(b); //false b = Boolean(''); alert(b); //false b = Boolean(NaN); alert(b); //false b = Boolean(null); alert(b); //false b = Boolean(undefined); alert(b); //false b = Boolean(Infinity); alert(b); //true b = Boolean('false'); alert(b); //true
直接使用的自动转换
var X; if (X) {alert('ok')} else {alert('no')}; //no X = 'ABC'; if (X) {alert('ok')} else {alert('no')}; //ok X = ''; if (X) {alert('ok')} else {alert('no')}; //no X = -1; if (X) {alert('ok')} else {alert('no')}; //ok X = 0; if (X) {alert('ok')} else {alert('no')}; //no X = new Date(); if (X) {alert('ok')} else {alert('no')}; //ok
布尔运算
var b; b = !b; alert(b); //false b = !b; alert(b); //true b = true && true; alert(b); //true b = true && false; alert(b); //false b = false && true; alert(b); //false b = false && false; alert(b); //false b = true || true; alert(b); //true b = true || false; alert(b); //true b = false || true; alert(b); //true b = false || false; alert(b); //false