🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #29

JS Strings

Strings are for storing text

Strings are written with quotes

Using Quotes

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

Note: Strings created with single or double quotes work 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:

Example

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:

Example

let text = `He's often 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 Characters

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 ".

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

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

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

Note: 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:

Example

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

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

Example

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

Template Strings

Templates were introduced with ES6 (JavaScript 2015).

Templates are strings enclosed in backticks (This is a template string).

Templates allow multiline strings:

Example

let text =

`The quick

brown fox

jumps over

the lazy dog`;

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");

Note: Do not create String objects. The new keyword complicates the code and slows down execution speed. String objects can produce unexpected results:

let x = "John";

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

let y = new String("John");

Note: the difference between (x==y) and (x===y).

let x = new String("John");

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

let y = new String("John");

Note: Comparing two JavaScript objects always returns false.


Note: Learn More: JavaScript String Methods JavaScript String Search JavaScript String Reference

Want to go beyond the notes?

Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

JS Strings – FAQs

Quick answers about learning JS Strings in JavaScript.

This free note from CodingNow 2.0 explains JS Strings in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on CodingNow 2.0, including JS Strings, is 100% free with no signup required.
With focused practice, most students grasp JS Strings in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now