Skip to content
Related Articles

Related Articles

Improve Article
JavaScript | String Methods
  • Last Updated : 01 Jul, 2020

Initially, JavaScript language was created for the browser only. But now, the language is used in many other environments also. It is a scripting language used to create and control dynamic website content. It means the webpage can move, refresh, or change on the user’s screen without manually reloading a web page.

String in Javascript: Strings are texts that help in holding data that can be represented.

A JavaScript string stores a series of characters like “GeeksforGeeks”. A string can be any text inside double or single quotes. For example,

 var gfg= "geeksforgeeks";

.

String indexes start from 0. The first character is in position 0 and the second in 1 and the same follows. We can call any of the pre-defined methods of JavaScript as it automatically converts from string primitive to objects.



  1. charAt(indexOfCharacter) method: This method returns the character at the specified index. String in JavaScript has zero-based indexing.
    Parameters: This method accepts single parameter indexOfCharacter that holds the Index of the character of any string.

    Example: This example describes the JavaScipt string charAt() method.




    <script>
        let gfg = 'GeeksforGeeks';
        let geeks = 'GfG is the best platform to "
            + "learn and experience Computer Science.';
      
        // Print the string as it is
        document.write(gfg); 
        document.write("<br>");
          
        document.write(geeks); 
        document.write("<br>");
      
        // As string index starts from zero
        // It will return first character of string
        document.write(gfg.charAt(0)); 
        document.write("<br>");
          
        document.write(geeks.charAt(5));
    </script>

    Output:

    GeeksforGeeks
    GfG is the best platform to learn 
    and experience Computer Science.
    G
    s
    
  2. charCodeAt(indexOfCharacter) Method: This method returns a number that represents the Unicode value of the character at the specified index. This method accepts one argument.

    Example: This example describes the JavaScipt String charCodeAt() Method.




    <script>
        let gfg = 'GeeksforGeeks';
        let geeks = 'GfG is the best platform "
                + "to learn and experience "
                + "Computer Science.';
      
        // Return a number indicating Unicode
        // value of character at index 0 ('G')
        document.write(gfg.charCodeAt(0));
        document.write("<br" >);
        document.write(geeks.charCodeAt(5));
    </script>

    Output:

    71
    115
    
  3. concat( objectOfString ) Method: This method combines the text of two strings and returns a new combined or joined string. To concatenate two strings, we use concat() method on one object of string and send another object of string as a parameter. This method accepts one argument. The variable contains text in double quotes or single quotes.

    Example: This example describes the JavaScipt String concat() method.






    <script>
        let gfg = 'GFG ';
        let geeks = 'stands for GeeksforGeeks';
      
        // Accessing concat method on an object
        // of String passing another object 
        // as a parameter
        document.write(gfg.concat(geeks));
      
    </script>

    Output:

    GFG stands for GeeksforGeeks
    
  4. endsWith(queryString, length) Method: This methods checks whether a string ends with a specified string or characters. This method returns “true” if string ends with the provided string, else it returns “false”. This method is case sensitive. This method accepts two arguments.

    • queryString: The string to be searched for.
    • length: The default value is the length of the string you provided.

    Example: This example describes the JavaScipt String endsWith() method.




    <script>
        let gfg = 'GFG ';
        let geeks = 'stands for GeeksforGeeks';
      
        // Accessing method of object geeks if
        // 'geeks' ends with 'ks', method 
        // return true
        document.write(geeks.endsWith('ks'));
    </script>

    Output:

    true
    
  5. fromCharCode(UNICODE_NUMBER) Method: This method converts UNICODE values to characters. This is a static method of the String object. The first method is not starting with the string variable. This method returns character for the given specific UNICODE. This method accepts a single parameter UNICODE-NUMBER that holds the number for which you want character.

    Example: This example describes the JavaScipt String fromCharCode() method.




    <script>
        let gfg = 'GFG ';
        let geeks = 'stands for GeeksforGeeks';
      
        // Given a number as argument
        // Output will be 'E' as 69 stands for 'E'
        document.write(String.fromCharCode(69));
      
        // Output will be 'a' as 97 stands for 'a'
        document.write(String.fromCharCode(97));
    </script>

    Output:

    Ea
    
  6. includes(queryString) Method: This method checks whether the string variable contains specific string or not. This method returns “true” if the string is present in the variable string variable, else it returns “false”. This method is case-sensitive. This method accepts a single parameter queryString that holds the string that you want to check if it is present or not.

    Example: This example describes the JavaScipt String includes() method.




    <script>
    let gfg = 'GFG ';
    let geeks = 'stands for GeeksforGeeks';
       
    // 'GFG' is present in gfg variable
    // returns "true"
    document.write(gfg.includes('GFG'));
    document.write("<br>");
      
    // It is case-sensitive
    // returns "false"
    document.write(gfg.includes('gfg'));
    </script>

    Output:



    true
    false
    
  7. indexOf(queryString) Method: This method returns the index of first occurrence of given query string. This method returns -1, if given character or string is not present in string variable. This method is case-sensitive. This method accepts single parameter queryString that holds the Character or string for getting index of that string.

    Example: This example describes the JavaScipt String indexOf() method.




    <script>
    let gfg = 'GFG ';
    let geeks = 'stands for GeeksforGeeks';
      
    // 'G' is present at 0 index.
    document.write(gfg.indexOf('G')); 
    document.write("<br>");
      
    // 'GFa' is not in gfg variable
    // returns -1
    document.write(gfg.indexOf('GFa'));
    document.write("<br>");
      
    // Space is present at 3rd index
    document.write(gfg.indexOf(' '));
    </script>

    Output:

     0
    -1
     3
    
  8. repeat(number) Method: This method returns string with the number of copies of the existing string. This method accepts single parameter number that holds the number of copies that you want for a existing string.

    Example: This example describes the JavaScipt String repeat() method.




    <script>
    let gfg = 'GFG ';
    let geeks = 'stands for GeeksforGeeks';
      
    // Return 3 copies of String 
    // present in variable gfg
    document.write(gfg.repeat(3));
    </script>

    Output:

    GFG GFG GFG
    
  9. replace(replaceValue, replaceWithValue) Method: This method returns string with the changes. This method is case-sensitive.
    This method accepts two parameters as mentioned above and described below:

    • replacedValue: This parameter holds the character that you want to replace.
    • replaceWithValue: This parameter holds the character that you want to replace with.

    Example: This example describes the JavaScipt String replace() method.




    <script>
    let gfg = 'GFG ';
    let geeks = 'stands for GeeksforGeeks';
       
    // Replace 'FG' in gfg with
    //'fg'
    documnet.write(gfg.replace('FG', 'fg'));
    </script>

    Output:

    Gfg
    
  10. search(queryString) Method: This method search for specified value or regular expression. This method returns the position of the match if it is found and if it is not found, it returns -1. This method is case-sensitive. This method accepts a single parameter queryString that holds the string that you want to get the position.

    Example: This example describes the JavaScipt String search() method.




    <script>
    let gfg = 'GFG ';
    let geeks = 'stands for GeeksforGeeks';
       
    // 'for' is not present in gfg
    // returns -1
    document.write(gfg.search('for'));
    document.write("<br>");
      
    //'for' is present in geeks
    // return the index from where it starts
    console.log(geeks.search('for'));
    </script>

    Output:

    -1
    7
    
  11. slice(startIndex, endIndex) Method: This method extract a part of string and returns a new string.

    This method accepts two arguments.

    • startIndex: This parameter holds the index from where you want to extract. It is included.
    • endIndex: This parameter holds the index till where you want to extract. It is excluded.

    Example: This example describes the JavaScipt String slice() method.




    <script>
    let gfg = 'GFG ';
    let geeks = 'stands for GeeksforGeeks';
       
    // Starts from 2nd index
    // ends at 7th index
    document.write(geeks.slice(2, 8));
      
    </script>

    Output:

    ands f
    
  12. split(character) Method: This method splits the string into an array of sub-strings. This method returns an array. This method accepts single parameter character on which you want to split the string.

    Example: This example describes the JavaScipt String split() method.




    <script>
    let gfg = 'GFG '
    let geeks = 'stands-for-GeeksforGeeks'
      
    // Split string on '-'. 
    console.log(geeks.split('-'))
      
    </script>

    Output:

    stands,for,GeeksforGeeks
    
  13. startsWith(queryString) Method: This method checks if a string starts with a given query string or not. This method returns “true” if string starts with provided query string else it returns “false”. This method accepts single parameter queryString that you want to check existing string start with it or not.

    Example: This example describes the JavaScipt String startsWith() method.




    <script>
    let gfg = 'GFG ';
    let geeks = 'stands-for-GeeksforGeeks';
      
    // String stored in geeks
    // starts with 'stand'
    // Returns "true"
    document.write(geeks.startsWith('stand'));
      
    </script>

    Output:

    true
    
  14. toLowerCase(stringVariable) Method: This method converts all the character present in string to lower case and returns a new string with all the characters in lower case. This method accepts single parameter stringVariable string that you want to convert in lower case.

    Example: This example describes the JavaScipt String toLowerCase() method.




    <script>
    let gfg = 'GFG ';
    let geeks = 'stands-for-GeeksforGeeks';
      
    document.write(geeks.toLowerCase(geeks));
      
    </script>

    Output:

    stands-for-geeksforgeeks
    
  15. toUpperCase(stringVariable) Method: This method converts all the character present in String to upper case and returns a new String with all character in upper case. This method accepts single parameter stringVariable string that you want to convert in upper case.

    Example: This example describes the JavaScipt String toUpperCase() method.




    <script>
    let gfg = 'GFG '
    let geeks = 'stands-for-GeeksforGeeks';
          
    document.write(geeks.toUpperCase(geeks)) ; 
    </script>

    Output:

    STANDS-FOR-GEEKSFORGEEKS
    
  16. trim() Method: This method is used to remove either white spaces from the given string. This method returns a new string with removed white spaces. This method called on a String object. This method doesn’t accept any parameter.

    Example: This example describes the JavaScipt String trim() method.




    <script>
    let gfg = 'GFG    ';
    let geeks = 'stands-for-GeeksforGeeks';
      
    // Storing new object of string
    // with removed white spaces
    var newGfg = gfg.trim();
      
    // Old length
    document.write(gfg.length);
    document.write("<br>");
      
    // New length
    document.write(newGfg.length)
    </script>

    Output:

    7
    3
    
My Personal Notes arrow_drop_up
Recommended Articles
Page :