JavaScript Strings
A JavaScript string is zero or more characters written inside quotes and You can also use single or double quotes:
Example(1)
<script>
//You can use quotes inside a string, as long as they don't match the quotes surrounding the string.
var first = " am first";
var second = "am second ";
var third = 'am third';
document.getElementById("demo").innerHTML =
first + "<br>" + second + "<br>" + third;
</script>
String Length
To find the length of a string, use the built-in length
property:
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var string_length = txt.length;
Escape Character
Because strings must be written within quotes, JavaScript will misunderstand this string:
var x = "We are learning about "Java Script" from BajarangiSoft.";
The string will be chopped to "We are the so-called ".
The solution to avoid this problem, is to use the backslash escape character.
The backslash (\
) escape character turns special characters into string characters:
Code | Result | Description |
---|---|---|
\' | ' | Single quote |
\" | " | Double quote |
\\ | \ | Backslash |
Example(2)
The sequence \"
inserts a double quote in a string:
var x = "We are learning about \"Java Script\" in BajarangiSoft site.";
Example(3)
The sequence \'
inserts a single quote in a string:
var x = 'It\'s alright.';
Example(4)
The sequence \\
inserts a backslash in a string:
var x = "The character \\ is called backslash.";
Code | Result |
---|---|
\b | Backspace |
\f | Form Feed |
\n | New Line |
\r | Carriage Return |
\t | Horizontal Tabulator |
\v | Vertical Tabulator |
For best readability, programmers often like to avoid code lines longer than 80 characters.
Example(1)
//If a JavaScript statement does not fit on one line, the best place to break it is after an operator:
document.getElementById("demo").innerHTML =
"Shiva Kumar!";
//You can also break up a code line within a text string with a single backslash:
document.getElementById("demo").innerHTML = "Shiva \
Kumar!";
// The \ method is not the preferred method. It might not have universal support.
Some browsers do not allow spaces behind the \ character.
// A safer way to break up a string, is to use string addition:
document.getElementById("demo").innerHTML = "Shiva " +
"Kumar!";
Strings Can be Objects
Normally, JavaScript strings are primitive values, created from literals:
var firstName = "shiva";
But strings can also be defined as objects with the keyword new
:
var firstName = new String("shiva");
<script>
var x = "shiva"; // x is a string
var y = new String("shiva"); // y is an object
document.getElementById("demo").innerHTML =
typeof x + "<br>" + typeof y;
</script>
Complete code of string with java script
<!DOCTYPE html>
<html>
<head>
<title>JavaScript string</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<style>
h3{
color:red;
}
</style>
<body>
<div class="container">
<br>
<br>
<div class="text-center">
<h1>JavaScript String</h1>
</div>
<br>
<div class="well">
<h1 id="demo"></h1>
</div>
<br>
</div>
</body>
</html>
<script>
//You can use quotes inside a string, as long as they don't match the quotes surrounding the string.
var first = " am first";
var second = "am second ";
var third = 'am third';
document.getElementById("demo").innerHTML =
first + "<br>" + second + "<br>" + third;
</script>