JavaScript Strings
Posted: Fri Oct 27, 2023 9:07 am
JavaScript Strings
JavaScript strings are for storing and manipulating text.
A JavaScript string is zero or more characters written inside quotes.
Example
let text = "John Doe";
Try it Yourself »
You can use single or double quotes:
Example
let carName1 = "Volvo XC60";
// Double quotes
let carName2 = 'Volvo XC60'; // Single quotes
Try it Yourself »
You can use quotes inside a string, as long as they don't match the quotes
surrounding the string:
Example
let answer1 = "It's alright";
let answer2 = "He is called 'Johnny'";
let answer3 = 'He is called "Johnny"';
Try it Yourself »
String Length
To find the length of a string, use the built-in length property:
Example
let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = text.length;
Try it Yourself »
Escape Character
Because strings must be written within quotes, JavaScript will misunderstand this string:
let text = "We are the so-called "Vikings" from the north.";
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
The sequence \" inserts a double quote in a string:
Example
let text = "We are the so-called \"Vikings\" from the north.";
Try it Yourself »
The sequence \' inserts a single quote in a string:
Example
let text= 'It\'s alright.';
Try it Yourself »
The sequence \\ inserts a backslash in a string:
Example
let text = "The character \\ is called backslash.";
Try it Yourself »
Six other escape sequences are valid in JavaScript:
Code
Result
\b
Backspace
\f
Form Feed
\n
New Line
\r
Carriage Return
\t
Horizontal Tabulator
\v
Vertical Tabulator
The 6 escape characters above were originally designed to control
typewriters, teletypes, and fax machines. They do not make any sense in HTML.
Breaking Long Code Lines
For best readability, programmers often like to avoid code lines longer than
80 characters.
If a JavaScript statement does not fit on one line, the best place to break
it is after an operator:
Example
document.getElementById("demo").innerHTML ="Hello Dolly!";
Try it Yourself »
You can also break up a code line within a text string with a single backslash:
Example
document.getElementById("demo").innerHTML =
"Hello \Dolly!";
Try it Yourself »
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:
Example
document.getElementById("demo").innerHTML = "Hello " + "Dolly!";
Try it Yourself »
You cannot break up a code line with a backslash:
Example
document.getElementById("demo").innerHTML = \ "Hello Dolly!";
Try it Yourself »
JavaScript Strings as Objects
Normally, JavaScript strings are primitive values, created from literals:
let x = "John";
But strings can also be defined as objects with the keyword new:
let y = new String("John");
Example
let x = "John";
let y = new String("John");
Try it Yourself »
Do not create Strings objects.
The new keyword complicates the code and slows down execution speed.
String objects can produce unexpected results:
When using the == operator, x and y are equal:
let x = "John";
let y = new String("John");
Try it Yourself »
When using the === operator, x and y are not equal:
let x = "John";
let y = new String("John");
Try it Yourself »
Note the difference between (x==y) and (x===y).
(x == y) true or false?
let x = new String("John");
let y = new String("John");
Try it Yourself »
(x === y) true or false?
let x = new String("John");
let y = new String("John");
Try it Yourself »
Comparing two JavaScript objects always returns false.
Complete String Reference
For a complete String reference, go to our:
Complete JavaScript String Reference.
The reference contains descriptions and examples of all string properties and methods.
Test Yourself With Exercises
Exercise:
Use the length property to alert the length of txt.
let txt = "Hello World!";
let x = ;
alert(x);
Submit Answer »
Start the Exercise
★
+1
Reference: https://www.w3schools.com/js/js_strings.asp
JavaScript strings are for storing and manipulating text.
A JavaScript string is zero or more characters written inside quotes.
Example
let text = "John Doe";
Try it Yourself »
You can use single or double quotes:
Example
let carName1 = "Volvo XC60";
// Double quotes
let carName2 = 'Volvo XC60'; // Single quotes
Try it Yourself »
You can use quotes inside a string, as long as they don't match the quotes
surrounding the string:
Example
let answer1 = "It's alright";
let answer2 = "He is called 'Johnny'";
let answer3 = 'He is called "Johnny"';
Try it Yourself »
String Length
To find the length of a string, use the built-in length property:
Example
let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = text.length;
Try it Yourself »
Escape Character
Because strings must be written within quotes, JavaScript will misunderstand this string:
let text = "We are the so-called "Vikings" from the north.";
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
The sequence \" inserts a double quote in a string:
Example
let text = "We are the so-called \"Vikings\" from the north.";
Try it Yourself »
The sequence \' inserts a single quote in a string:
Example
let text= 'It\'s alright.';
Try it Yourself »
The sequence \\ inserts a backslash in a string:
Example
let text = "The character \\ is called backslash.";
Try it Yourself »
Six other escape sequences are valid in JavaScript:
Code
Result
\b
Backspace
\f
Form Feed
\n
New Line
\r
Carriage Return
\t
Horizontal Tabulator
\v
Vertical Tabulator
The 6 escape characters above were originally designed to control
typewriters, teletypes, and fax machines. They do not make any sense in HTML.
Breaking Long Code Lines
For best readability, programmers often like to avoid code lines longer than
80 characters.
If a JavaScript statement does not fit on one line, the best place to break
it is after an operator:
Example
document.getElementById("demo").innerHTML ="Hello Dolly!";
Try it Yourself »
You can also break up a code line within a text string with a single backslash:
Example
document.getElementById("demo").innerHTML =
"Hello \Dolly!";
Try it Yourself »
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:
Example
document.getElementById("demo").innerHTML = "Hello " + "Dolly!";
Try it Yourself »
You cannot break up a code line with a backslash:
Example
document.getElementById("demo").innerHTML = \ "Hello Dolly!";
Try it Yourself »
JavaScript Strings as Objects
Normally, JavaScript strings are primitive values, created from literals:
let x = "John";
But strings can also be defined as objects with the keyword new:
let y = new String("John");
Example
let x = "John";
let y = new String("John");
Try it Yourself »
Do not create Strings objects.
The new keyword complicates the code and slows down execution speed.
String objects can produce unexpected results:
When using the == operator, x and y are equal:
let x = "John";
let y = new String("John");
Try it Yourself »
When using the === operator, x and y are not equal:
let x = "John";
let y = new String("John");
Try it Yourself »
Note the difference between (x==y) and (x===y).
(x == y) true or false?
let x = new String("John");
let y = new String("John");
Try it Yourself »
(x === y) true or false?
let x = new String("John");
let y = new String("John");
Try it Yourself »
Comparing two JavaScript objects always returns false.
Complete String Reference
For a complete String reference, go to our:
Complete JavaScript String Reference.
The reference contains descriptions and examples of all string properties and methods.
Test Yourself With Exercises
Exercise:
Use the length property to alert the length of txt.
let txt = "Hello World!";
let x = ;
alert(x);
Submit Answer »
Start the Exercise
★
+1
Reference: https://www.w3schools.com/js/js_strings.asp