Page 224 - Web_Application_v2.0_C12_Fb
P. 224

2.9 STRINGS IN JAVASCRIPT

              Strings  are  a  data  type  used  for  storing  text,  and  in  JavaScript,  they  are  treated  as  objects  with  distinct
              properties and methods. A string consists of characters, which can be letters, numbers, special symbols, or
              even spaces. You can define strings using either single or double quotation marks.

              In a string, each character is associated with an index, starting from 0. This index can be positive or negative,
              where the positive index counts from the beginning of the string, and the negative index counts from the end.
                                                             Positive index

                                        0  1   2  3   4  5   6  7   8  9  10  11  12  13  14  15
                                        H  E   L  L   O      J  A   V  A   S  C   R  I   P  T
                                        -16  -15  -14 -13  -12 -11 -10  -9  -8  -7  -6  -5  -4  -3  -2  -1

                                                            Negative index

              A string object consists of properties and methods.

              For example, to find the length of a string, use the built-in length property.
                  var str = "Hello, World!";
                  var lengthOfString = str.length;
                  document.write (lengthOfString);
              Output: 13

              For example, to get each charater of a string by using length of string with a for loop.

                  var str = "Hello, World!";
                  for (var i = 0; i < str.length; i++) {
                  document.write (str[i] + “ “);
                  }
              Output: H e l l o , W o r l d !

                2.10 STRING PROPERTIES AND METHODS

              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("Welcome".length)
                                                                          Output: 7
                slice()         Extracts a part of a string and returns it as a  var str = "Hello World";
                                new string.                               var result = str.slice(0, 5);
                                                                          document.write(result);
                                                                          Output: Hello
                substring()     It extracts the string using  the starting and  var str = "JavaScript";
                                ending  positions as parameters. If  you  leave  var result = str.substring(4, 10);
                                the  ending position blank,  the string  will be  document.write(result);
                                extracted from beginning to end.          Output: Script



                222   Touchpad Web Applications (Ver. 2.0)-XII
   219   220   221   222   223   224   225   226   227   228   229