JavaScript Strings and Text Processing

JavaScript Strings and Text Processing

Working with Textual Data Effectively

Author
Nguyen Bao Huy
09:29:00 21/02/2026
2 min read
0 comments

Strings are for storing text. Strings are written with quotes.

Using Quotes

A JavaScript string is zero or more characters written inside quotes.

javascript
let text = "John Doe"; 

You can use single or double quotes:

javascript
let carName1 = "Volvo XC60";  // Double quotes
let carName2 = 'Volvo XC60';  // Single 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:

javascript
let answer1 = "It's alright";
let answer2 = "He is called 'Johnny'";
let answer3 = 'He is called "Johnny"';

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:

javascript
let text = 'He's often called "Johnny"'; 
Templates are not supported in Internet Explorer.

String Length

To find the length of a string, use the built-in length property:

javascript
let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = text.length;

Escape Characters

Because strings must be written within quotes, JavaScript will misunderstand this string:

javascript
let text = "We are the so-called "Vikings" from the north.";

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:

CodeResultDescription
\''Single quote
\""Double quote
\\\Backslash

Examples

\” inserts a double quote in a string:

javascript
let text = "We are the so-called \"Vikings\" from the north.";

\' inserts a single quote in a string:

javascript
let text= 'It\'s alright.';

Six other escape sequences are valid in JavaScript:

CodeResult
\bbackspace
\fform feed
\nnew line
\rcarriage 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:

javascript
document.getElementById("demo").innerHTML = "Hello Dolly!";

A safe way to break up a string is by using string addition:

javascript
document.getElementById("demo").innerHTML = "Hello " + "Dolly!";

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
let text =
`The quick
brown fox
jumps over
the lazy dog`;

JavaScript Strings as Objects

Normally, JavaScript strings are primitive values, created from literals:

javascript
let x = "John"; 

But strings can also be defined as objects with the keyword new:

javascript
let y = new String("John"); 
let x = "John";
let z = 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:

javascript
let x = "John";
let y = new String("John");

When using the === operator, x and y are not equal:

javascript
let x = "John";
let y = new String("John");

Note the difference between (x==y) and (x===y). (x == y) true or false ?

javascript
let x = new String("John"); 
let y = new String("John"); 

(x === y) true or false?

First Lesson

You are at the beginning of this curriculum.

Latest Tutorial

More chapters coming soon to this topic.

Author avatar

Nguyen Bao Huy

Lead Fullstack & AI Solutions Engineer

Specializing in Next.js App Router, React 19, TypeScript, and modern design systems. Passionate about creating seamless user experiences.

Discussion

0

No comments yet

Be the first to share your thoughts, question a concept, or provide additional tips!

Leave a Reply

Share your insights, questions, or solutions with the developer community.

Your avatar
0 / 500 characters