JavaScript Strings
JavaScript strings are used for storing and manipulating text. It can contain zero or more characters within quotes.
Syntax:
var string_name=".."
Example:
Javascript
<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> |
Output:
Welcome to GeeksforGeeks!
Methods to implement strings: There are mainly two methods to implementing strings which are listed below.
Example 1: Use either single or double quotes to write strings.
javascript
<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> |
Output:
GeeksforGeeks A computer science portal
Example 2: Quotes can be used inside a string, as long as they don’t match the quotes surrounding the string.
javascript
<p id= "GFG" ></p> <script> var x = "'GeeksforGeeks'" ; var y = "A 'computer' 'science' portal" ; document.getElementById( "GFG" ).innerHTML = x + "<br>" + y; </script> |
Output:
'GeeksforGeeks' A 'computer' 'science' portal
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
<p id= "GFG" ></p> <!-- Script to use special character --> <script> var x = "\"GeeksforGeeks\" A \'computer science\' portal" ; document.getElementById( "GFG" ).innerHTML = x; </script> |
Output:
"GeeksforGeeks" A 'computer science' portal
Example: String can also be written within a single quote.
javascript
<p id= "GFG" ></p> <!-- Script to use special character --> <script> var x = '\"GeeksforGeeks\" A \'computer science\' portal' ; document.getElementById("GFG").innerHTML = x; </script> |
Output:
"GeeksforGeeks" A 'computer science' portal
String Length: The length of a string can be found using the length property.
Example:
javascript
<p id= "GFG" ></p> <!-- Script to return the length of string --> <script> var string = "GeeksforGeeks" ; // Returns the length of string document.getElementById( "GFG" ).innerHTML = string.length; </script> |
Output:
13
String Breaking: Sometimes we need to divide the string for ease of understanding, the symbol \ can be used but it is not preferred. The preferred method is to use the + symbol between the two strings.
Example:
javascript
<p id= "GFG" ></p> <!-- Script to break the line --> <script> document.getElementById( "GFG" ).innerHTML = "Welcome" + " to GeeksforGeeks!" ; </script> |
Output:
Welcome to GeeksforGeeks!
Strings As Objects: Strings can be used as objects by using the keyword ‘new’.
Example:
javascript
<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> |
Output:
string object
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
We have a Cheat Sheet on JavaScript where we covered all the important topics of JavaScript to check those please go through JavaScript Cheat Sheet – A Basic Guide to JavaScript.
Please Login to comment...