リチャードJavaScript(8)-ブル関連問題

1885 ワード

基本的な使い方
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