Page 308 - Web Applications (803) Class 11
P. 308

The various methods to explicitly convert data are as follows:

                    Method                               Description                                 Example
               Number( )         Converts any value to number. NaN is returned if the value cannot be  Number(false)
                                 converted.                                                 Output:0
                                                                                            Number(“77”)
                                                                                            Output:77
                                                                                            Number(“69 58”)
                                                                                            Output:NaN

               parseInt( )       This  method  accepts  two  arguments  and  returns  an  integer  value.  parseInt(“30 years”)
                                 Syntax: parseInt(string, radix)                            Output: 30
                                 string—The value that has to be parsed (required)          parseInt(“20.43”)
                                 radix—The  default  value  is  10.  The  number  system  is  specified  by  Output: 20
                                 numbers 2–36. (Optional)                                   parseInt(“He is 30 years”)
                                                                                            Output: NaN
                                                                                            parseInt(“20”, 8)
                                                                                            Output: 16

               parseFloat( )     The parseFloat() method returns the first decimal number after parsing  parseFloat(“20.45”)
                                 a value as a string. NaN is returned if the initial character cannot be  Output: 20.45
                                 converted.                                                 parseFloat(“44 56”)
                                                                                            Output:44

               eval( )           A JavaScript expression is passed as a string argument to this method.  eval(“5*4”)
                                 After that, eval( ) evaluates the expression and returns the result. It  Output: 20
                                 returns undefined if no result is received.

               String            A value is converted to a string.                          String(false)
                                                                                            Output: false
                                                                                            String(Boolean(1))
                                                                                            Output: true String(“65”)
                                                                                            Output: 65

               Boolean           Checks if an expression is true.                           Boolean(20 > 7)
                                                                                            Output: true
                                                                                            X=null; Boolean(X)
                                                                                            Output: false

              Example:

                 <!DOCTYPE html>
                 <HTML>
                 <BODY>
                 <H1>JavaScript Global Methods</H1>

                 <H2>The eval() Method</H2>
                 <SCRIPT>
                 l x = 10;
                 let y = 20;
                 let text = "x * y";
                 let result = eval(text);

                306     Touchpad Web Applications-XI
   303   304   305   306   307   308   309   310   311   312   313