JavaScript Strings
JavaScript Strings
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
JavaScript Strings
❮ Previous Next ❯
Using Quotes
A JavaScript string is zero or more characters written inside quotes.
Example
Try it Yourself »
Example
let carName1 = "Volvo XC60"; // Double quotes
let carName2 = 'Volvo XC60'; // Single quotes
Try it Yourself »
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
Note
Strings created with single or double quotes works the same.
Example
let answer1 = "It's alright";
let answer2 = "He is called 'Johnny'";
let answer3 = 'He is called "Johnny"';
Try it Yourself »
Template Strings
Templates were introduced with ES6 (JavaScript 2016).
Example
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
Note
Templates are not supported in Internet Explorer.
String Length
To find the length of a string, use the built-in length property:
Example
Try it Yourself »
Escape Characters
Because strings must be written within quotes, JavaScript will misunderstand this string:
let text = "We are the so-called "Vikings" from the north.";
The backslash escape character ( \ ) turns special characters into string characters:
Examples
\" inserts a double quote in a string:
let text = "We are the so-called \"Vikings\" from the north.";
Try it Yourself »
Try it Yourself »
Try it Yourself »
Code Result
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Horizontal Tabulator
\v Vertical Tabulator
NoteTutorials
Exercises Services Sign Up Log in
Example
document.getElementById("demo").innerHTML =
"Hello Dolly!";
Try it Yourself »
Example
document.getElementById("demo").innerHTML = "Hello " +
"Dolly!";
Try it Yourself »
Template Strings
Templates were introduced with ES6 (JavaScript 2016).
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
Example
let text =
`The quick
brown fox
jumps over
the lazy dog`;
Try it Yourself »
Note
Templates are not supported in Internet Explorer.
let x = "John";
But strings can also be defined as objects with the keyword new :
Example
let x = "John";
let y = new String("John");
Try it Yourself »
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
The new keyword complicates the code and slows down execution speed.
let x = "John";
let y = new String("John");
Try it Yourself »
let x = "John";
let y = new String("John");
Try it Yourself »
(x == y) true or false?
Try it Yourself »
(x ===Tutorials Exercises
y) trueor false? Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
let x = new String("John");
let y = new String("John");
Try it Yourself »
The reference contains descriptions and examples of all string properties and methods.
Exercise:
Use the length property to alert the length of txt .
Submit Answer »
Start the Exercise
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
❮ Previous Next ❯
W3schools Pathfinder
Track your progress - it's free! Sign Up Log in
COLOR PICKER
How To Tutorial
Tutorials
SQL Tutorial
Exercises Services SQL Reference
Python Reference
Sign Up Log in
Python Tutorial W3.CSS Reference
HTML
CSS W3.CSS Tutorial
JAVASCRIPT SQL PYTHON Bootstrap
JAVA Reference
PHP HOW TO W3.CSS C C++ C
Bootstrap Tutorial PHP Reference
PHP Tutorial HTML Colors
Java Tutorial Java Reference
C++ Tutorial Angular Reference
jQuery Tutorial jQuery Reference
Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.