JavaScript | Strings

JavaScript strings are used for storing and manipulating text. It can contain zero or more characters within quotes.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript Strings
    </title>
</head>    
  
<body>
      
    <h1>GeeksforGeeks</h1>
      
    <h2>JavaScript Strings</h2>
      
    <p id="GFG"></p>
      
    <!-- Script to store string in variable -->
    <script>
      
        // String written inside quotes
        var x = "Welcome to GeeksforGeeks!"
        document.getElementById("GFG").innerHTML = x;
    </script>
</body>
  
</html>                    

chevron_right


Output:

Methods to implement string: There are mainly two methods to implementing strings which are listed below.

  • Example 1: Use either single or double quotes to write strings.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            JavaScript Strings
        </title>
    </head>    
      
    <body>
          
        <h1>GeeksforGeeks</h1>
          
        <h2>JavaScript Strings</h2>
          
        <p id="GFG"></p>
          
        <!-- Script to initialize string -->
        <script>
            var x = "GeeksforGeeks";
            var y = 'A computer science portal';
              
            document.getElementById("GFG").innerHTML =
                        x + "<br>" + y; 
        </script>
    </body>
      
    </html>                    

    chevron_right

    
    

    Output:

  • Example 2: Quotes can be used inside a string, as long as they don’t match the quotes surrounding the string.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            JavaScript Strings
        </title>
    </head>    
      
    <body>
          
        <h1>GeeksforGeeks</h1>
          
        <h2>JavaScript Strings</h2>
          
        <p id="GFG"></p>
      
        <script>
            var x = "'GeeksforGeeks'";
            var y = "A 'computer' 'science' portal";
              
            document.getElementById("GFG").innerHTML =
                        x + "<br>" + y; 
        </script>
    </body>
      
    </html>                                

    chevron_right

    
    

    Output:

Special characters: As stated above, the special character can’t use the same type of quotes within a string, but there is a solution. It uses the backslash escape character. The backslash ‘\’ escape character turns special characters into normal string characters. The sequence (\”) is used to insert a double quote in a string.



  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            JavaScript Strings
        </title>
    </head>    
      
    <body>
          
        <h1>GeeksforGeeks</h1>
          
        <h2>JavaScript Strings for special character</h2>
          
        <p id="GFG"></p>
          
        <!-- Script to use special character -->
        <script>
            var x = "\"GeeksforGeeks\" A \'computer science\' portal";
              
            document.getElementById("GFG").innerHTML = x; 
        </script>
    </body>
      
    </html>                       

    chevron_right

    
    

    Output:

  • Example: String can be written within single quote.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            JavaScript Strings
        </title>
    </head>    
      
    <body>
          
        <h1>GeeksforGeeks</h1>
          
        <h2>JavaScript Strings for special character</h2>
          
        <p id="GFG"></p>
          
        <!-- Script to use special character -->
        <script>
            var x = '\"GeeksforGeeks\" A \'computer science\' portal';
              
            document.getElementById("GFG").innerHTML = x; 
        </script>
    </body>
      
    </html>                          

    chevron_right

    
    

    Output:

String Length: The length of a string can be found using the length property.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript Strings
    </title>
</head>    
  
<body>
      
    <h1>GeeksforGeeks</h1>
      
    <h2>JavaScript Strings length</h2>
      
    <p id="GFG"></p>
      
    <!-- Script to return the length of string -->
    <script>
        var len = "GeeksforGeeks"
          
        // Returns the length of string
        document.getElementById("GFG").innerHTML
                = len.length;
    </script>
</body>
  
</html>                    

chevron_right


Output:

String Breaking: Sometimes we need to divide the string for ease of understanding, the symbol \ can be used but its not preferred. The preferred method is to use the + symbol between the two strings.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript Strings
    </title>
</head>    
  
<body>
      
    <h1>GeeksforGeeks</h1>
      
    <h2>JavaScript Strings break lines</h2>
      
    <p id="GFG"></p>
      
    <!-- Script to break the line -->
    <script>
        document.getElementById("GFG").innerHTML = "Welcome" 
                    + " to GeeksforGeeks!";
    </script>
</body>
  
</html>                    

chevron_right


Output:

Strings As Objects: Strings can be used as objects by using the keyword ‘new’.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript Strings
    </title>
</head>    
  
<body>
      
    <h1>GeeksforGeeks</h1>
      
    <h2>JavaScript Strings as object</h2>
      
    <p id="GFG"></p>
      
    <!-- Script to use string as object -->
    <script>
      
        // Declare a string
        var x = "Great Geek";     
          
        // Declare an object
        var y = new String("Great Geek"); 
          
        document.getElementById("GFG").innerHTML =
                typeof x + "<br>" + typeof y;
    </script>
</body>
  
</html>                    

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.