This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
4.1 JS Strings
- JavaScript strings are used for storing and manipulating text.
- A JavaScript string is zero or more characters written inside quotes.
var x = "John Doe";
You can use single or double quotes:
var carName1 = "Volvo XC60"; // Double quotes
var carName2 = 'Volvo XC60'; // Single quotes
You can use quotes inside a string, as long as they don't match the quotes surrounding the string:
var answer1 = "It's alright";
var answer2 = "He is called 'Johnny'";
var answer3 = 'He is called "Johnny"';
String Length
To find the length of a string, use the built-in length property:
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
Escape Character
Because strings must be written within quotes, JavaScript will misunderstand this string:
var x = "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:
var x = "We are the so-called \"Vikings\" from the north.";
The sequence \' inserts a single quote in a string:
var x = 'It\'s alright.';
The sequence \\ inserts a backslash in a string:
var x = "The character \\ is called backslash.";
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
document.getElementById("demo").innerHTML =
"Hello Dolly!";
You can also break up a code line within a text string with a single backslash:
document.getElementById("demo").innerHTML = "Hello \
Dolly!";
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 = "Hello " +
"Dolly!";
You cannot break up a code line with a backslash:
document.getElementById("demo").innerHTML = \
"Hello Dolly!";
Strings Can be Objects
- Normally, JavaScript strings are primitive values, created from literals:
- var firstName = "John";
- But strings can also be defined as objects with the keyword new:
- var firstName = new String("John");
var x = "John";
var y = new String("John");
// typeof x will return string
// typeof y will return object
Don't create strings as objects. It slows down execution speed.
The new keyword complicates the code. This can produce some unexpected results:
When using the == operator, equal strings are equal:
var x = "John";
var y = new String("John");
// (x == y) is true because x and y have equal values
When using the === operator, equal strings are not equal, because the === operator expects equality in both type and value.
var x = "John";
var y = new String("John");
// (x === y) is false because x and y have different types (string and object)
Or even worse. Objects cannot be compared:
var x = new String("John");
var y = new String("John");
// (x == y) is false because x and y are different objects
var x = new String("John");
var y = new String("John");
// (x === y) is false because x and y are different objects
JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon
Example files created in this module:
JS Strings part 1
JS Strings part 2
JS Strings part 3
JS Strings length Property
JS Strings the escape sequence \"
JS Strings the escape sequence \'
JS Strings the escape sequence \\
JS Statements
JS Strings line break
JS Strings line break +
JS Strings line break NOT
JS Strings typeof
JS Strings - equal value
JS Strings - not equal type
JS Strings - cannot compare Objects
JS Strings - Objects cannot be compared
JS Strings slice()
JS Strings The slice() Method part 1
JS Strings The slice() Method part 2
JS Strings The slice() Method part 3
JS Strings the substring() method
JS Strings the substr() method part 1
JS Strings the substr() method part 2
JS Strings the substr() method part 3
JS Strings the replace() method part 1
JS Strings the replace() method part 2
JS Strings the replace() method part 3
Mobile test of using JS CSS
JS Strings the replace() method part 4
JS Strings the replace() method part 5
JS Strings Convert string part 1
JS Strings Convert string part 2
JS Strings Convert string part 3
JS Strings Convert string part 4
JS Strings The padStart() Method part 1
JS Strings The padStart() Method part 2
JS Strings The padStart() Method part 3
JS Strings The padEnd() Method part 1
JS Strings The padEnd() Method part 2
JS Strings The padEnd() Method part 3
JS Strings The charAt() Method
JS Strings The charCodeAt() Method
JS Strings Property Access part 1
JS Strings Property access part 2
JS String Methods String split() part 1
JS String Methods String split() part 2
JS String The indexOf() Method
JS Numbers part 1
JS Numbers part 2
JS Numbers part 3
Floating Point Precision part 1
Floating Point Precision part 2
Numbers and Strings part 1
Numbers and Strings part 2
Numbers and Strings part 3
Numbers and Strings part 4
Numbers and Strings part 5
Numbers and Strings part 6
Numbers and Strings part 7
Numbers and Strings part 8
JS Numbers, NaN (Not a Number) part 1
JS Numbers, NaN (Not a Number) part 2
JS Numbers, NaN (Not a Number) part 3
JS Numbers, NaN (Not a Number) part 4
JS Numbers, NaN (Not a Number) part 5
JS Numbers, NaN (Not a Number) part 6
JS Numbers Infinity part 1
JS Numbers Infinity part 2
JS Numbers Infinity part 3
JS Numbers - Hexadecimal part 1
JS Numbers - Hexadecimal part 2
JS Numbers As Objects part 1
JS Numbers As Objects part 2
JS Numbers As Objects part 3
JS Numbers As Objects part 4
JS Number Methods part 1
JS Number Methods part 2
JS Number Methods part 3
JS Number Methods part 4
JS Number Methods part 5
JS Number Methods part 6
JS Number Methods part 7
JS Number Methods part 8
JS Number Methods part 9
JS Number Methods MAX_VALUE Property
JS Number Methods MIN_VALUE Property
JS Number Methods POSITIVE_INFINITY Property part 1
JS Number Methods POSITIVE_INFINITY Property part 2
JS Number Methods NEGATIVE_INFINITY Property part 1
JS Number Methods NEGATIVE_INFINITY Property part 2
JS Number Methods NaN part 1
JS Number Methods NaN part 2
JS Number Methods Number Properties
JS Arrays part 1
JS Arrays part 2
JS Arrays part 3
JS Arrays part 4
JS Arrays part 5
JS Arrays part 6
JS Arrays part 7
JS Arrays part 8
JS Arrays part 9
JS Arrays part 10
JS Arrays part 11
JS Arrays part 12
JS Arrays part 13
JS Arrays part 14
JS Arrays part 15
JS Arrays part 16
JS Arrays part 17
JS Arrays part 18
JS Arrays part 19
JS Arrays part 20
JS Arrays part 21
JS Arrays isArray() Method
JS Arrays isArray() function
JS Arrays The instanceof Operator
JS Arrays The toString() Method
JS Arrays The join() Method
JS Arrays The pop() Method part 1
JS Arrays The pop() Method part 2
JS Arrays The push() Method part 1
JS Arrays The push() Method part 2
JS Arrays The shift() Method part 1
JS Arrays The shift() Method part 2
JS Arrays The shift() Method part 3
JS Arrays The shift() Method part 4
JS Arrays Bracket Indexing
JS Arrays The length Property
JS Arrays The delete Method
JS Arrays The splice() Method part 1
JS Arrays The splice() Method part 2
JS Arrays The splice() Method part 3
JS Arrays The concat() Method part 1
JS Arrays The concat() Method part 2
JS Arrays The concat() Method part 3
JS Arrays The slice() Method part 1
JS Arrays The slice() Method part 2
JS Arrays The slice() Method part 3
JS Arrays The slice() Method part 4
JS Arrays The toString() Method part 1
JS Arrays The toString() Method part 2
JS Arrays The sort() Method part 1
JS Arrays Sort in Reverse
JS Arrays The sort() Method part 2
JS Arrays The sort() Method part 3
JS Arrays The sort() Method part 4
JS Arrays The sort() Method part 5
JS Arrays The sort() Method part 6
JS Arrays The sort() Method part 7
JS Arrays The sort() Method part 8
JS Array Sort The Highest Number
JS Array Sort The lowest number
JS Arrays sort the highest number
JS Arrays sort the lowest number
JS Arrays sort car objects by age
JS Arrays sort car objects by type
JS Arrays The forEach() Method part 1
JS Arrays The forEach() Method part 2
JS Arrays The map() Method part 1
JS Arrays The map() Method part 2
JS Arrays The filter() Method part 1
JS Arrays The filter() Method part 2
JS Arrays The reduce() Method part 1
JS Arrays The reduce() Method part 2
JS Arrays The reduce() Method part 3
JS Arrays The reduceRight() Method part 1
JS Arrays The reduceRight() Method part 2