Wednesday, February 26, 2020

Exploring JavaScript Boolean methods with simple and clear examples


Hello guys! morning and welcome to my new tutorial post on JavaScript, in my previous tutorial we talked about how to implement the JavaScript Boolean Properties with examples. Without wasting much time i will go into what i have in stock for you guys in this tutorial.

In this section of my tutorial, we will be using few examples to demonstrate the usage of the Boolean methods. Make sure you read through carefully for proper understanding and ask questions in areas you don't understand properly.

Boolean toSource() Method

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

Note - This method is not compatible with all browsers.

Syntax

It's syntax is as follows -

boolean.toSource() 

Return Value

Returns the string representating 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("Java",  "Web design tutorialz",  400);          
               document.write(newBook.toSource() );
          </script>
     </body>
</html>

Output

Below is the output of the above example.

( {title: "Java",  publisher: "Web design tutorialz",  price: 400} )      

You can also read our tutorial post on: Html Blocks


Boolean toString() Method

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

Syntax

It's syntax is as follows -

boolean.toString() 

Return Value

Returns the string representating the specified 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

Below is the output of the above example.

flag.toString is : false 

You can also read our tutorial post on: Html Image Link

Boolean valueOf() Method
JavaScript boolean valueOf() method returns a primitive value of the specified boolean object.

Syntax
It's 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 valueOf() Method</title>
     </head>

     <body>
          <script type = "text/Javascript
               var  flag = new Boolean(false);          
               document.write("flag.valueOf is : " + flag.valueOf() );     
          </script>
     </body>
</html>

Output
Below is the output of the above example.

flag.valueOf is : false 

Alright guys! this is the end of this tutorial post on JavaScript Boolean methods, and i hope you guys understood every bit of it very well?

In my next tutorial post, we are going to be discussing about JavaScript String Object so stay tuned. Like i always say, feel free to ask your questions via the comment box below and they will be answered as soon as possible. Also follow us on our various social media handles to stay updated with our latest tutorials.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.