Strings in JavaScript are used to store and manipulate text. A JavaScript string consists of zero or more characters enclosed in quotations.
A JavaScript string is zero or more characters written inside quotes.
EXAMPLE
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<p id="demo"></p>
<script>
let text = "Gideon Arias"; // String written inside quotes
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
You can use single or double quotes:
EXAMPLE
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<p>Strings are written inside quotes. You can use single or double quotes:</p>
<p id="demo"></p>
<script>
let carName1 = "Polestar"; // Double quotes
let carName2 = 'Polestar'; // Single quotes
document.getElementById("demo").innerHTML =
carName1 + " " + carName2;
</script>
</body>
</html>
Use the built-in length property to determine the length of a string:
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The length Property</h2>
<p>The length of the string is:</p>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.getElementById("demo").innerHTML = text.length;
Because strings must be placed between quotes, JavaScript will interpret this string incorrectly:
The sequence \" inserts a double quote in a string:
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<p>The escape sequence \" inserts a double quote in a string.</p>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
let text = "We are the so-called \"Vikings\" from the north.";
document.getElementById("demo").innerHTML = text;
The sequence \' inserts a single quote in a string:
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<p>The escape sequence \' inserts a single quote in a string.</p>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
let text = 'It\'s alright.';
document.getElementById("demo").innerHTML = text;
The sequence \\ inserts a backslash in a string:
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<p>The escape sequence \\ inserts a backslash in a string.</p>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
let text = "The character \\ is called backslash.";
document.getElementById("demo").innerHTML = text;
Code lines larger than 80 characters are frequently avoided by programmers for better readability.
If a JavaScript statement cannot fit on a single line, it is better to break it after an operator:
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Statements</h1>
<p>The best place to break a code line is after an operator or a comma.</p>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
document.getElementById("demo").innerHTML =
"Howdy! Partner ;)";
Strings in JavaScript as Objects
Strings in JavaScript are often primitive values derived from literals:
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Statements</h1>
<p>The best place to break a code line is after an operator or a comma.</p>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
// x is a string
let x = "Aron";
// y is an object
let y = new String("Aron");
document.getElementById("demo").innerHTML =
typeof x + "<br>" + typeof y;