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

String Operations
              You can also use various string methods in JavaScript to work with strings:


                   Method/ Property                 Description                             Example
                  length               Returns the string’s length.           document.write(“Welcome”.length)
                                                                              Output: 7
                  substring            It extracts the substring using the  str1 = “Pineapple is very tasty!”;
                                       starting and ending positions  as      document.write(str1.substring(7,20))
                                       parameters. If you leave the ending    Output: le is very ta
                                       position blank,  the  string will be
                                       extracted from beginning to end.
                  search               returns the position of the first match,  var text = “I have a red umbrella”
                                       -1 otherwise                           var position = text.search(“red”);
                                                                              Output: 9
                                                                              document.write(“<br>”+text.
                                                                              search(“pizza”))
                                                                              Output: -1
                  +                    Concatenates/joins two strings         “Welcome”+ “to” + “Paris”
                                                                              Output: WelcometoParis








              Other Datatypes

              It is possible to declare a variable in JavaScript without giving it a value. The variable’s type is undefined if we
              do this.

                                          Null, an object of type "object" that denotes a purposeful
                                          non-value.

                                          undefined,  an  object  of  type  "undefined"  denotes  an
                                          uninitialised value.



                                          boolean, with values as true or false.


              Following these guidelines will enable any given value to be transformed into a boolean:
                false, 0, and the empty string (“ “) all become false as well as NaN, null, and undefined.
                All alternative values become true.
                           Notes


                          When a variable is declared but not given a value, its type is undefined by default.







                188   Touchpad Web Applications-XII
   185   186   187   188   189   190   191   192   193   194   195