Home > Web Front-end > JS Tutorial > Detailed explanation of JavaScript logic Not operator_javascript skills

Detailed explanation of JavaScript logic Not operator_javascript skills

WBOY
Release: 2016-05-16 15:27:27
Original
1835 people have browsed it

In JavaScript, the logical NOT operator is the same as the logical NOT operator in C and Java, represented by an exclamation point (!). Unlike the logical OR and logical AND operators, the logical NOT operator always returns a Boolean value.

The logical NOT operator behaves as follows:

  • If the operand is an object, return false
  • If the operand is the number 0, return true
  • If the operand is any number other than 0, return false
  • If the operand is null, return true
  • If the operand is NaN, return true
  • If the operand is undefined, an error occurs

The test script is as follows:

<script type="text/javascript">
  var bFalse = false;//运算数是bool类型的数
  var sRed = "red";//运算数是字符串
  var iZero = 0;//运算数是0
  var iThreeFourFive = 345;//运算数是 0 以外的任何数字
  var oObject = new Object();//对象
  var oNull=null;//运算数是null
  var oUndefined;//运算数是undifined
  var oNaN=parseInt("abc");//使用parseInt方法把尝试字符串abc转换成整数,因为abc不是数字,因此是转不了的,所以返回的结果就是NaN
 
  /*
  writeln() 方法与 write() 方法几乎一样,差别仅在于是前者将在所提供的任何字符串后添加一个换行符。在HTML中,这通常只会在后面产生一个空格;
  不过如果使用了 <PRE> 和 <XMP> 标识,这个换行符会被解释,且在浏览器中显示。
  */
 document.writeln("<XMP>");
 document.writeln("oNaN=parseInt(\"abc\")返回的结果是"+oNaN);
 document.writeln("bool类型的数false与!运算符运算之后的结果是:" + (!bFalse));
 document.writeln("字符串sRed与!运算符运算之后的结果是: " + (!sRed));
 document.writeln("数字0与!运算符运算之后的结果是:" + (!iZero));//如果运算数是数字 0,返回 true 
 document.writeln("数字345与!运算符运算之后的结果是:" + (!iThreeFourFive));//如果运算数是 0 以外的任何数字,返回 false 
 document.writeln("对象oObject与!运算符运算之后的结果是:" + (!oObject));//如果运算数是对象,返回 false 
 document.writeln("NaN与!运算符运算之后的结果是:" + (!oNaN));//如果运算数是NaN,返回 true 
 document.writeln("null与!运算符运算之后的结果是:" + (!oNull));//如果运算数是 null,返回 true 
 document.writeln("undifined与!运算符运算之后的结果是:" + (!oUndefined));
 //document.writeln("未定义的字符串sBule与!运算符运算之后的结果是:" + (!sBule));//sBule前面没有定义,也就是sBule运算数是 undefined,因此这里发生错误 
 document.writeln("</XMP>");
 </script>
Copy after login

Run result:

 

When judging the Boolean value of a JavaScript variable, you can also use the logical NOT operator. Doing this requires using two NOT operators in one line of code. Regardless of the type of the operand, the first NOT operator returns a Boolean value, and the second NOT operator inverts the Boolean value to give the true Boolean value of the variable. Using the not operator to determine the Boolean value of a JavaScript variable is a very useful technique. As long as you know the Boolean value of the variable, you can quickly know the result of the operation when using the variable to perform && or || operations.
The test script is as follows:

<script type="text/javascript">
  var bFalse = false;//运算数是bool类型的数
  var sRed = "red";//运算数是字符串
  var iZero = 0;//运算数是0
  var iThreeFourFive = 345;//运算数是 0 以外的任何数字
  var oObject = new Object();//对象
  var oNull=null;//运算数是null
  var oUndefined;//运算数是undifined
  var oNaN=parseInt("abc");//使用parseInt方法把尝试字符串abc转换成整数,因为abc不是数字,因此是转不了的,所以返回的结果就是NaN
 /*
 判断JavaScript 变量的 Boolean 值时,也可以使用逻辑 NOT 运算符。这样做需要在一行代码中使用两个 NOT 运算符。
 无论运算数是什么类型,第一个 NOT 运算符返回 Boolean 值。第二个 NOT 将对该 Boolean 值取反,从而给出变量真正的 Boolean 值。
 */
 document.write("<PRE>");
 document.writeln("布尔数false 的逻辑值是 " + (!!bFalse));
 document.writeln("字符串sRed 的逻辑值是 " + (!!sRed));
 document.writeln("数字0 的逻辑值是 " + (!!iZero));
 document.writeln("数字345 的逻辑值是 " + (!!iThreeFourFive));
 document.writeln("对象Object 的逻辑值是 " + (!!oObject));
 document.writeln("NaN的逻辑值是 :" + (!!oNaN));
 document.writeln("null 的逻辑值是 " + (!!oNull));
 document.writeln("undefined 的逻辑值是 " + (!!oUndefined));
 document.write("
");
Copy after login

The above is the detailed information about JavaScript logical Not operator. I hope it will be helpful to everyone's learning.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template