WHAT'S NEW?
Loading...

Properties of boolean object & method, toSource, toString, valueOf in javascript with example - Tech-n-Savvy Blogger


The Boolean object represents two values, either "true" or "false". If value parameter is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false.

Syntax

Use the following syntax to create a boolean object.


var val = new Boolean(value);


Boolean Properties

Here is a list of the properties of Boolean object:

Property
Description




constructor
Returns  a  reference  to  the  Boolean  function  that

created the object.


prototype
The prototype property allows you to add properties

and methods to an object.



In the following sections, we will have a few examples to illustrate the properties of Boolean object.

constructor ()

Javascript boolean constructor() method returns a reference to the Boolean function that created the instance's prototype.

Syntax

Use the following syntax to create a Boolean constructor() method.

boolean.constructor()




Return Value


Returns the function that created this object's instance.

Example

Try the following example.

<html>

<head>

<title>JavaScript constructor() Method</title>

</head>

<body>

<script type="text/javascript">

var bool = new Boolean( );

document.write("bool.constructor() is : " + bool.constructor);

</script>

</body>

</html>

Output

bool.constructor() is : function Boolean() { [native code] }


Prototype

The prototype property allows you to add properties and methods to any object (Number, Boolean, String and Date, etc.).

Note: Prototype is a global property which is available with almost all the objects.

Syntax

Use the following syntax to create a Boolean prototype.


object.prototype.name = value

Example
  
Try the following example; it shows how to use the prototype property to add a property to an object.

<html>

<head>

<title>User-defined objects</title>


<script type="text/javascript">


function book(title, author){

this.title = title;

this.author     = author;

}

</script>

</head>

<body>

<script type="text/javascript">

var myBook = new book("Perl", "Mohtashim"); book.prototype.price = null; myBook.price = 100;

document.write("Book title is : " + myBook.title + "<br>");

document.write("Book author is : " + myBook.author + "<br>");

document.write("Book price is : " + myBook.price + "<br>");

</script>

</body>

</html>

Output


Book title is : Perl

Book author is : Mohtashim

Book price is : 100


Boolean Methods

Here is a list of the methods of Boolean object and their description.


Method
Description



toSource()          Returns a string containing the source of the Boolean object; you can use this string to create an equivalent object.

toString()            Returns a string of either "true" or "false" depending upon the value of the object.

valueOf()
Returns the primitive value of the Boolean object.



In the following sections, we will have a few examples to demonstrate the usage of the Boolean methods.

toSource ()

Javascript boolean toSource() method returns a string representing the source code of the object.

Note: This method is not compatible with all the browsers.

Syntax

Its syntax is as follows:

boolean.toSource()

Return Value

Returns a string representing the source code of the object.

Example

Try the following example.

<html>

<head>

<title>JavaScript toSource() Method</title>

</head>

<body>

<script type="text/javascript">

function book(title, publisher, price)

{


this.title = title;

this.publisher = publisher;

this.price = price;

}

var newBook = new book("Perl","Leo Inc",200);

document.write("newBook.toSource() is : "+ newBook.toSource());

</script>

</body>

</html>

Output

({title:"Perl", publisher:"Leo Inc", price:200})


toString ()

This method returns a string of either "true" or "false" depending upon the value of the object.

Syntax

Its syntax is as follows:

boolean.toString()

Return Value

Returns a string representing the specified Boolean object.

Example

Try the following example.


<html>

<head>

<title>JavaScript toString() Method</title>

</head>

<body>

<script type="text/javascript">

var flag = new Boolean(false);

document.write( "flag.toString is : " + flag.toString() );

</script>

</body>

</html>

Output

flag.toString is : false


valueOf ()

Javascript boolean valueOf() method returns the primitive value of the specified boolean object.

Syntax

Its syntax is as follows:

boolean.valueOf()

Return Value

Returns the primitive value of the specified boolean object.

Example

Try the following example.

<html>

<head>

<title>JavaScript toString() Method</title>

</head>

<body>

<script type="text/javascript">

var flag = new Boolean(false);

document.write( "flag.valueOf is : " + flag.valueOf() );

</script>

</body>

</html>

Output



flag.valueOf is : false

0 comments:

Post a Comment