
JavaScript Strings and Text Processing
Working with Textual Data Effectively
Catalog
Click on this table of contents to jump to the corresponding section
Strings are for storing text. Strings are written with quotes.
Using Quotes
A JavaScript string is zero or more characters written inside quotes.
You can use single or double quotes:
Strings created with single or double quotes works the same. There is no difference between the two.
Quotes Inside Quotes
You can use quotes inside a string, as long as they don’t match the quotes surrounding the string:
Template Strings
Templates were introduced with ES6 (JavaScript 2016). Templates are strings enclosed in backticks (This is a template string). Templates allow single and double quotes inside a string:
Templates are not supported in Internet Explorer.
String Length
To find the length of a string, use the built-in length property:
Escape Characters
Because strings must be written within quotes, JavaScript will misunderstand this string:
The string will be chopped to "We are the so-called ". To solve this problem, you can use an backslash escape character. The backslash escape character (\) turns special characters into string characters:
| Code | Result | Description |
|---|---|---|
| \' | ' | Single quote |
| \" | " | Double quote |
| \\ | \ | Backslash |
Examples
\” inserts a double quote in a string:
\' inserts a single quote in a string:
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 Lines
For readability, programmers often like to avoid long code lines. A safe way to break up a statement is after an operator:
A safe way to break up a string is by using string addition:
Template Strings
Templates were introduced with ES6 (JavaScript 2016). Templates are strings enclosed in backticks (This is a template string). Templates allow multiline strings:
JavaScript Strings as Objects
Normally, JavaScript strings are primitive values, created from literals:
But strings can also be defined as objects with the keyword new:
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:
When using the === operator, x and y are not equal:
Note the difference between (x==y) and (x===y). (x == y) true or false ?
(x === y) true or false?
Lily and 4 people like this
Prev Post
Space The Final Frontier
Next Post
Telescopes 101
0 Comments
Leave a Reply
Recent Post

SMOTE for Imbalanced Classification with Python
12:28:00 16/05/2026

CLAHE Histogram Equalization with OpenCV: A Python Guide
12:06:00 16/05/2026

Histogram Equalization in Digital Image Processing
10:56:00 16/05/2026

Gaussian Noise Explained: What It Is and How It Works
10:36:00 16/05/2026

Python Image Blurring with OpenCV: Gaussian, Median & Bilateral Filter Guide
09:59:00 16/05/2026
Related Topics
Feeds
Don't miss what's next 👋
Enjoyed this content? Leave your email to get notified when we publish new insights, tutorials, and updates — no spam, ever.

