Page 163 - DCAP408_WEB_PROGRAMMING
P. 163

Unit 5: Scripting Language




          And all the following lines of code create Boolean objects with an initial value of true:  Notes
                 var myBoolean=new Boolean(true);
                 var myBoolean=new Boolean(“true”);
                 var myBoolean=new Boolean(“false”);

                 var myBoolean=new Boolean(“Richard”);
                                    Table 5.5:  Boolean Object Methods

               Method               Description

               toString()           Converts a Boolean value to a string, and returns the result
               valueOf()            Returns the primitive value of a Boolean object



                 Example: Check if a Boolean object is true or false.
                 <html>
                 <body>
                 <script  type=”text/javascript”>
                 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”);
                 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>
                 </body>
                 </html>
          Output

          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




                                           LOVELY PROFESSIONAL UNIVERSITY                                   157
   158   159   160   161   162   163   164   165   166   167   168