当前位置: 代码迷 >> JavaScript >> [小结] Javascript Boolean运算值测试
  详细解决方案

[小结] Javascript Boolean运算值测试

热度:487   发布时间:2013-01-19 11:41:36.0
[总结] Javascript Boolean运算值测试

测试脚本

<script type="text/javascript">
  <!--
	var _boolean_true = true;
	var _boolean_false = false;
	var _undefined;
	var _null = null;
	var _int = 10000;
	var _int_1 = 1;
	var _int_0 = 0;
	var _int__0 = -0;
	var _NaN = NaN;
	var _string_onestring = "onestring";
	var _string_empty = "";
	var _string_1 = "1";
	var _string_0 = "0";
	var _string_true = "true";
	var _string_false = "false";
	var _string_null = "null";
	var _string_undefined = "undefined";
	var _function = function() {};
	var _array_empty = [];
	var _array_notempty = [1,2];
	var _object_empty = {};
	var _object_notempty = {name:'yourname'};

	
	println("Boolean(_boolean_true) : " + Boolean(_boolean_true));
	println("Boolean(_boolean_false) : " + Boolean(_boolean_false));
	println("Boolean(_undefined) : " + Boolean(_undefined));
	println("Boolean(_null) : " + Boolean(_null));
	println("Boolean(_int) : " + Boolean(_int));
	println("Boolean(_int_1) : " + Boolean(_int_1));
	println("Boolean(_int_0) : " + Boolean(_int_0));
	println("Boolean(_int__0) : " + Boolean(_int__0));
	println("Boolean(_NaN) : " + Boolean(_NaN));
	println("Boolean(_string_onestring) : " + Boolean(_string_onestring));
	println("Boolean(_string_empty) : " + Boolean(_string_empty));
	println("Boolean(_string_1) : " + Boolean(_string_1));
	println("Boolean(_string_0) : " + Boolean(_string_0));
	println("Boolean(_string_true) : " + Boolean(_string_true));
	println("Boolean(_string_false) : " + Boolean(_string_false));
	println("Boolean(_string_null) : " + Boolean(_string_null));
	println("Boolean(_function) : " + Boolean(_function));
	println("Boolean(_array_empty) : " + Boolean(_array_empty));
	println("Boolean(_array_notempty) : " + Boolean(_array_notempty));
	println("Boolean(_object_empty) : " + Boolean(_object_empty));
	println("Boolean(_object_notempty) : " + Boolean(_object_notempty));

	println();
	
	println("!_boolean_true : " + !_boolean_true);
	println("!_boolean_false : " + !_boolean_false);
	println("!_undefined : " + !_undefined);
	println("!_null : " + !_null);
	println("!_int : " + !_int);
	println("!_int_1 : " + !_int_1);
	println("!_int_0 : " + !_int_0);
	println("!_int__0 : " + !_int__0);
	println("!_NaN : " + !_NaN);
	println("!_string_onestring : " + !_string_onestring);
	println("!_string_empty : " + !_string_empty);
	println("!_string_1 : " + !_string_1);
	println("!_string_0 : " + !_string_0);
	println("!_string_true : " + !_string_true);
	println("!_string_false : " + !_string_false);
	println("!_string_null : " + !_string_null);
	println("!_function : " + !_function);
	println("!_array_empty : " + !_array_empty);
	println("!_array_notempty : " + !_array_notempty);
	println("!_object_empty : " + !_object_empty);
	println("!_object_notempty : " + !_object_notempty);

	println();

	println("!!_boolean_true : " + !!_boolean_true);
	println("!!_boolean_false : " + !!_boolean_false);
	println("!!_undefined : " + !!_undefined);
	println("!!_null : " + !!_null);
	println("!!_int : " + !!_int);
	println("!!_int_1 : " + !!_int_1);
	println("!!_int_0 : " + !!_int_0);
	println("!!_int__0 : " + !!_int__0);
	println("!!_NaN : " + !!_NaN);
	println("!!_string_onestring : " + !!_string_onestring);
	println("!!_string_empty : " + !!_string_empty);
	println("!!_string_1 : " + !!_string_1);
	println("!!_string_0 : " + !!_string_0);
	println("!!_string_true : " + !!_string_true);
	println("!!_string_false : " + !!_string_false);
	println("!!_string_null : " + !!_string_null);
	println("!!_function : " + !!_function);
	println("!!_array_empty : " + !!_array_empty);
	println("!!_array_notempty : " + !!_array_notempty);
	println("!!_object_empty : " + !!_object_empty);
	println("!!_object_notempty : " + !!_object_notempty);

	function println(s) { s = s || ""; document.writeln(s + "<br/>"); }
  //-->
</script>

运行脚本



测试结果

Boolean(_boolean_true) : true
Boolean(_boolean_false) : false
Boolean(_undefined) : false
Boolean(_null) : false
Boolean(_int) : true
Boolean(_int_1) : true
Boolean(_int_0) : false
Boolean(_int__0) : false
Boolean(_NaN) : false
Boolean(_string_onestring) : true
Boolean(_string_empty) : false
Boolean(_string_1) : true
Boolean(_string_0) : true
Boolean(_string_true) : true
Boolean(_string_false) : true
Boolean(_string_null) : true
Boolean(_function) : true
Boolean(_array_empty) : true
Boolean(_array_notempty) : true
Boolean(_object_empty) : true
Boolean(_object_notempty) : true

!_boolean_true : false
!_boolean_false : true
!_undefined : true
!_null : true
!_int : false
!_int_1 : false
!_int_0 : true
!_int__0 : true
!_NaN : true
!_string_onestring : false
!_string_empty : true
!_string_1 : false
!_string_0 : false
!_string_true : false
!_string_false : false
!_string_null : false
!_function : false
!_array_empty : false
!_array_notempty : false
!_object_empty : false
!_object_notempty : false

!!_boolean_true : true
!!_boolean_false : false
!!_undefined : false
!!_null : false
!!_int : true
!!_int_1 : true
!!_int_0 : false
!!_int__0 : false
!!_NaN : false
!!_string_onestring : true
!!_string_empty : false
!!_string_1 : true
!!_string_0 : true
!!_string_true : true
!!_string_false : true
!!_string_null : true
!!_function : true
!!_array_empty : true
!!_array_notempty : true
!!_object_empty : true
!!_object_notempty : true



结论

0、-0、null、""、false、undefined 或 NaN:此7种结果经过Boolean运算后结果false,其他情况为true。

  相关解决方案