JavaScript | Strings
JavaScript strings are used for storing and manipulating text. It can contain zero or more characters within quotes.
Example:
<!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.
<!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.
<!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:
<!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.
<!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:
<!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:
<!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:
<!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: