w3resource

Javascript Boolean Objects - Properties and Methods

Description

A boolean object is an object which holds boolean values.

Can be created using javascript boolean constructor. new Boolean(value) is how a new boolean object is created.

Boolean primitive value (true and false ) is not same as boolean object values (true and false).

Javascript Boolean Objects Property

Name Description Version
constructor Specifies the function that creates an object's prototype. Implemented in JavaScript 1.1
prototype Use to add new properties and methods to a boolean object. Implemented in JavaScript 1.1

Javascript Boolean Objects Methods

Name Description Version
toSource Returns a string which represents the source code of a boolean object. Implemented in JavaScript 1.1
toString Returns a string representing the specified boolean object. Implemented in JavaScript 1.1
valueof Returns the primitive value of a boolean object. Implemented in JavaScript 1.1

JavaScript constructor Property : Boolean Object

The constructor property of the boolean object specifies the function that creates an object's prototype.

Syntax

object.constructor

Object: The name of the object (Required).

Example:

In the following web document, the constructor property returns the reference to the function that creates the object.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript boolean object - constructor property : example</title>
</head>
<body>
<h1 style="color: red">JavaScript boolean object : constructor property</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
a=new Boolean(false)
document.write("The object is constructed from : "+a.constructor);
//]]>
</script>
</body>
</html>

View the example in the browser

JavaScript prototype Property: Boolean Object

The prototype property is used to add new properties and methods to a boolean object.

Syntax

myobj.prototype.name = value 

myobj: The name of the constructor function object you want to change

name: The name of the property or method to be created.

value: The value initially assigned to the new property or method.

If you add a property to the prototype for an object, then all objects created with that object's constructor function will have that new property, even if the objects existed before you created the new property.

Example:

In the following web document, a method has added to the boolean object which returns new character 'T' or 'F' against 'true' or 'false'.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript boolean object - prototype property : example</title>
</head>
<body>
<h1 style="color: red">JavaScript boolean object : prototype property</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
function new_Boolean_sign( ){
if(this == true)
return("T");
else
return("F");
}
Boolean.prototype.newsign = new_Boolean_sign;
var a = new Boolean(true);
var b = a.newsign();
document.write("The new true sign is : "+b); 
//]]>
</script>
</body>
</html>

JavaScript toSource() Method: Boolean Object

The toSource() method returns a string which represents the source code of a boolean object.

Syntax

toSource()

Example:>

In the following web document toSource() method returns the source code of a boolean object.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript boolean object - toSource() method : example</title>
</head>
<body>
<h1 style="color: red">JavaScript boolean object : toSource() method</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
boolinf = new Boolean(0);
document.write(boolinf.toSource());
//]]>
</script>
</body>
</html>

View the example in the browser

JavaScript toString() method: Boolean Object

The toString() method returns a string representing the specified boolean object. If the boolean value is true, it returns "true" and if the boolean value is false, it returns "false".

Syntax

toString()

Example:

In the following web document toString() method is used in a comparison.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript boolean object - toString() method : example</title>
</head>
<body>
<h1 style="color: red">JavaScript boolean object : toString() method</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
booleanObj = new Boolean(true);
if(booleanObj.toString() == "true")
alert("Both are equal....");
else
alert("Not Equal");</script>
//]]>
</script>
</body>
</html>

View the example in the browser

JavaScript valueOf() Method: Boolean Object

The valueOf() method returns the primitive value of a boolean object. JavaScript calls the valueOf() method internally. This method is not called by code explicitly.

Syntax

valueOf()

Example:

In the following web document valueOf() method is used to get the primitive value of a boolean object.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript boolean object - valueOf() method : example</title>
</head>
<body>
<h1 style="color: red">JavaScript boolean object : valueOf() method</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
bobj = new Boolean(1);
document.write(bobj.valueOf());
//]]>
</script>
</body>
</html>

View the example in the browse

Supported Browser

Internet Explorer 7 Firefox 3.6 Google Chrome 7 Safari 5.0.1 Opera 10
Yes Yes Yes Yes Yes

See also:

JavaScript Core objects, methods, properties.

Previous: JavaScript valueOf() Method: Array Object
Next: Javascript Date Objects - Properties and Methods

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.