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";
You can use single or double quotes:
Example
let carName1 = "Volvo XC60"; // Double quotes
let carName2 = 'Volvo XC60'; // Single quotes
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"';
String Length
To find the length of a string, use the built-in length
property:
Example
let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = text.length;
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.";
The sequence \'
inserts a single quote in a string:
Example
let text= 'It\'s alright.';
The sequence \\
inserts a backslash in a string:
Example
let text = "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:
Example
document.getElementById("demo").innerHTML =
"Hello Dolly!";
You can also break up a code line within a text string with a single backslash:
Example
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:
Example
document.getElementById("demo").innerHTML = "Hello " +
"Dolly!";
Note: You cannot break up a code line with a backslash:
Example
document.getElementById("demo").innerHTML = \
"Hello Dolly!";
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");
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");
When using the ===
operator, x and y are not equal:
let x = "John";
let y = new String("John");
Note the difference between (x==y)
and (x===y)
.
(x == y)
true or false?
let x = new String("John");
let y = new String("John");
(x === y)
true or false?
let x = new String("John");
let y = new String("John");
Note: Comparing two JavaScript objects always returns false.
42 Responses
levaquin 500mg ca order levofloxacin sale
buy avodart avodart for sale online order ondansetron 8mg without prescription
spironolactone 100mg drug buy spironolactone pills order diflucan 200mg sale
purchase ampicillin generic cephalexin 125mg generic erythromycin buy online
order sildenafil 50mg without prescription brand fildena 50mg methocarbamol 500mg drug
cost suhagra 50mg sildenafil 150mg price order estradiol for sale
lamotrigine medication buy mebendazole 100mg online tretinoin gel canada
buy tadalis 20mg without prescription tadalafil pill diclofenac 50mg without prescription
generic isotretinoin 10mg zithromax 250mg brand order zithromax generic
buy indocin 50mg without prescription buy cefixime 200mg pills trimox 500mg generic
buy tadalafil 20mg online cheap Buy viagra canada order sildenafil online cheap
arimidex 1 mg usa oral anastrozole buy sildenafil 100mg online
acheter 10mg cialis sildenafil 25mg prix viagra 25mg pas cher
order prednisone 10mg sale brand sildenafil sildenafil 50mg drug
tadalafil 20mg für frauen original sildenafil 50mg rezeptfrei sicher kaufen sildenafil 50mg für männer
provigil 100mg usa buy provigil 200mg for sale acetazolamide 250 mg drug
buy doxycycline 100mg for sale levitra for sale online order furosemide generic
altace 10mg price order azelastine 10ml order azelastine 10 ml generic
catapres order buy catapres pill purchase tiotropium bromide online cheap
buy buspar 5mg pill buy dilantin 100 mg without prescription oxybutynin 2.5mg drug
terazosin 5mg pill purchase sulfasalazine for sale sulfasalazine over the counter
cheap fosamax purchase pepcid buy pepcid 20mg for sale
olmesartan canada depakote 500mg uk acetazolamide 250 mg cheap
prograf 5mg usa ursodiol order buy urso 150mg without prescription
buy imdur 40mg generic purchase micardis telmisartan oral
generic zyban 150mg strattera order seroquel pills
order molnupiravir 200mg generic purchase prevacid pill purchase lansoprazole pills
zoloft brand buy lexapro 20mg generic cost viagra
imuran 100 mcg price salbutamol 100mcg canada cost viagra
cialis otc Low price cialis viagra cheap
overnight delivery for cialis buy tadalafil without prescription buy amantadine without prescription
naltrexone usa order generic aripiprazole generic abilify 30mg
avlosulfon cheap order aceon 8mg online aceon drug
order modafinil without prescription order stromectol pills stromectol oral
order luvox 100mg for sale purchase ketoconazole online cheap glucotrol cost
oral accutane 40mg order deltasone 5mg sale deltasone over the counter
piracetam buy online cheap sildenafil generic 50mg viagra
order azithromycin 500mg for sale buy zithromax 250mg generic neurontin us
cialis price walmart buy generic cialis buy viagra tablets
order lasix for sale buy generic lasix 100mg order hydroxychloroquine sale
order tadalafil pills cialis 20 clomipramine 25mg tablet
order chloroquine pill buy baricitinib 2mg online cheap buy olumiant 2mg pills