基本使用
var b;b = true; alert(b); //trueb = new Boolean(false); alert(b); //falseb = 2 > 1; alert(b); //trueb = 2
轉換到布爾
var b;b = Boolean(123); alert(b); //trueb = Boolean('ABC'); alert(b); //trueb = Boolean(0); alert(b); //falseb = Boolean(''); alert(b); //falseb = Boolean(NaN); alert(b); //falseb = Boolean(null); alert(b); //falseb = Boolean(undefined); alert(b); //falseb = Boolean(Infinity); alert(b); //trueb = Boolean('false'); alert(b); //true
直接使用的自動轉換
var X;if (X) {alert('ok')} else {alert('no')}; //noX = 'ABC';if (X) {alert('ok')} else {alert('no')}; //okX = '';if (X) {alert('ok')} else {alert('no')}; //noX = -1;if (X) {alert('ok')} else {alert('no')}; //okX = 0;if (X) {alert('ok')} else {alert('no')}; //noX = new Date();if (X) {alert('ok')} else {alert('no')}; //ok
布爾運算
var b;b = !b; alert(b); //falseb = !b; alert(b); //trueb = true && true; alert(b); //trueb = true && false; alert(b); //falseb = false && true; alert(b); //falseb = false && false; alert(b); //falseb = true || true; alert(b); //trueb = true || false; alert(b); //trueb = false || true; alert(b); //trueb = false || false; alert(b); //false