Page 188 - Web Applications (803) Class 12
P. 188

3.5 JAVASCRIPT DATATYPES

              A variable or object’s capacity to store different types of data is categorised by its datatype. When computer
              programmers design applications, datatypes must be referred to and used properly on both desktop and web
              applications to ensure the application’s functionalities produce accurate, error-free results.
              There are several datatypes. Some of them are described in detail as follows:

                          Datatypes                        Description                         Example
                  String                      denotes textual data                   “pizza”, “APPLE”
                  Number                      an integer or a floating-point number  3.25, 450

                  Boolean                     true or false value                    true/false
                  undefined                   a datatype with uninitialised variables  var x;

                  null                        represents a null value                var b = null;
                  Object                      Allows to store collection of data     x = {firstname:”Suman”,
                                                                                     lastname:”Nagpal”};

              In this case, all datatypes are primitive with the exception of Object, which is a non-primitive datatype.

              Let us now study the various datatypes in detail.
              Numbers

              JavaScript uses double-precision 64-bit numbers. Addition, subtraction, modulus (or residual) arithmetic, and
              other common numeric operations are provided. Additionally, a built-in object called Math is available to
              handle more complex mathematical constants and functions.

              Syntax:
                  var a = 16;
                  var num=new Number(value);
              The various functions used to explicitly convert data to numbers are as follows:

                            Method                          Description                         Example
                  Number( )                     Converts any value to number. NaN  Number(false) Output:0
                                                is returned  if the  value cannot  be  Number(“34”) Output:34
                                                converted.                            Number(“6 5 7”) Output:NaN

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

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

                186   Touchpad Web Applications-XII
   183   184   185   186   187   188   189   190   191   192   193