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

4.13 STRING OPERATIONS
                 You already know that concatenation is performed by placing a + sign between two strings. You can also use various
                 string methods in JavaScript to work with strings:


                    Method/                     Description                                    Example
                    Property
                 length        Returns the string’s length.                  document.write(“Constantinople”.length)
                                                                             Output: 14

                 substring     It extracts the substring using the starting and ending  str1 = “Pineapple is very tasty!”;
                               positions as parameters. If you leave the ending position   document.write(str1.substring(7,20))
                               blank, the string will be extracted from beginning  to   Output: le is very ta
                               end.

                 search        It returns the position of the first match, -1 otherwise.  text = "I am studying JavaScript";
                                                                             document.write(text.search("studying"))
                                                                             Output:5
                                                                             document.write(text.search("Math"))
                                                                             Output: -1



                   4.14 TYPE CONVERSIONS IN JAVASCRIPT

                 When two distinct types of data are used in an expression, JavaScript automatically converts the data and evaluates
                 the expression. This action is called type conversion. In JavaScript, there are two ways to convert the type:



                                                               Type Conversion





                                          Implicit Type Conversion            Explicit Type Conversion

                 Implicit Type Conversion

                 When an expression is evaluated and two different types are given that are incompatible to perform the operation,
                 JavaScript automatically converts them. The following are some examples of implicit conversions:

                 Ð ÐIf one value is a number and the other is a string, the expression turns the string to a number and performs the
                   arithmetic operation. For example, 12 * “5” returns the number 60, and 2 – “5” returns -3.

                 Ð ÐIn arithmetic operations, if a value is null, it is converted to the integer value 0. The outcome of null + 8 is 8, for
                   example.

                 Ð ÐIn an arithmetic operation, if one value is Boolean, it is converted to 0 or 1 depending on the provided value. True
                   + 6 yields a result of 7, while false+8 yields a value of 8.

                 Ð ÐIf the value cannot be translated to another type, it is returned as NaN (Not a Number).
                 Explicit Type Conversion

                 The developer does this type of conversion by utilizing the built-in type conversion methods.







                                                                      Introduction to Dynamic Websites Using JavaScript  305
   302   303   304   305   306   307   308   309   310   311   312