Home > Web Front-end > JS Tutorial > body text

Basic use of Boolean objects in JavaScript programming_Basic knowledge

WBOY
Release: 2016-05-16 15:35:05
Original
1376 people have browsed it

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> 

Copy after login

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
Copy after login

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

var myBoolean=new Boolean();
Copy after login

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

0
-0
null
""
false
undefined
NaN
Copy after login

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

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!