Home  >  Article  >  Web Front-end  >  Basic use of Boolean objects in JavaScript programming_Basic knowledge

Basic use of Boolean objects in JavaScript programming_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 15:35:051418browse

Boolean objects are used to convert non-Boolean values ​​into Boolean values ​​(true or false).

Check Boolean value
Checks whether a boolean object is true or false.
Source code example:

<!DOCTYPE html>
<html>
<body>
&#8203;
<script>
var b1=new Boolean(0);
var b2=new Boolean(1);
var b3=new Boolean("");
var b4=new Boolean(null);
var b5=new Boolean(NaN);
var b6=new Boolean("false");
&#8203;
document.write("0 is boolean "+ b1 +"<br>");
document.write("1 is boolean "+ b2 +"<br>");
document.write("An empty string is boolean "+ b3 + "<br>");
document.write("null is boolean "+ b4+ "<br>");
document.write("NaN is boolean "+ b5 +"<br>");
document.write("The string 'false' is boolean "+ b6 +"<br>");
</script>
&#8203;
</body>
</html> 

Test results:

0 is boolean false
1 is boolean true
An empty string is boolean false
null is boolean false
NaN is boolean false
The string 'false' is boolean true

Create Boolean object
Boolean object represents two values: "true" or "false"
The following code defines a Boolean object named myBoolean:

var myBoolean=new Boolean();

If the Boolean object has no initial value or its value is:

0
-0
null
""
false
undefined
NaN

Then the value of the object is false. Otherwise, its value is true (even when the argument is the string "false")!

Statement:
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