Page 225 - Web_Application_v2.0_C12_Fb
P. 225

Method/
                     Property                    Description                                  Example

                  replace()       Finds and replaces a specified substring within  var str = "Hello World";
                                  a string                                  var result = str.replace("World", "JavaScript");
                                                                            document.write(result);
                                                                            Output: Hello JavaScript

                  replaceAll()    Finds and replaces all occurrences of a specified  var str = "apple apple apple";
                                  substring within a string.                var result = str.replaceAll("apple", "banana");
                                                                            document.write(result);
                                                                            Output: banana banana banana

                  match()         Searches for a match and returns an array of  var str = "The rain in Spain";
                                  results.                                  var result = str.match(/ain/g); document.
                                                                            write(result);
                                                                            Output: ain,ain

                  toUpperCase()   Converts a string to uppercase letters.   var str = "hello";
                                                                            var result = str.toUpperCase();
                                                                            document.write(result);
                                                                            Output: HELLO

                  toLowerCase()   Converts a string to lowercase letters.   var str = "HELLO";
                                                                            var result = str.toLowerCase();
                                                                            document.write(result);
                                                                            Output: hello

                  concat()        Joins two or more strings.                var result = "Harry".concat("Potter");
                                                                            document.write(result);
                                                                            Output: HarryPotter

                  trim()          Removes whitespace from both ends of a  var str = " Hello World ";
                                  string.                                   var result = str.trim();
                                                                            document.write(result);
                                                                            Output: Hello World

                  charAt()        Returns the character at a specified index.  var str = "JavaScript";
                                                                            var result = str.charAt(4);
                                                                            document.write(result);
                                                                            Output: S

                  search()        returns  the  position  of  the  first  match,  -1  var text = "I have a red umbrella"
                                  otherwise                                 var position = text.search("red");
                                                                            Output: 9
                                                                            document.write("<br>"+text.search("pizza"))
                                                                            Output: -1
                  + Operator      used for  addition  (for  numbers) and  document.write(10 + 20);
                                  concatenation/joins (for strings).        Output: 30
                                                                            document.write("Welcome"+ "to" + "Paris");
                                                                            Output: WelcometoParis





                                                                                                  JavaScript Part 2  223
   220   221   222   223   224   225   226   227   228   229   230