Skip to content
Related Articles

Related Articles

JavaScript | Strings

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 07 Jun, 2022
View Discussion
Improve Article
Save Article

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

Example: 

javascript




<!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>                   

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. 

javascript




<!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>                   

Output:

 

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

javascript




<!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>                               

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: 

javascript




<!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>                      

Output:

 

Example: String can be written within single quote. 

javascript




<!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>                         

Output:

 

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

Example: 

javascript




<!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>                   

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: 

javascript




<!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>                   

Output:

  

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

Example: 

javascript




<!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>                   

Output:

 

Supported Browsers:

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 3 and above
  • Opera 3 and above
  • Safari 1 and above

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!