JavaScript: String Overview

By Xah Lee. Date: . Last updated: .

String is one of the type of JavaScript values.

[see Value Types]

// example of string
console.log( "hello" );

String is a Primitive Value

Primitive Value

String is Immutable

JavaScript string is immutable. That means, any time you join string or get a substring or replace parts of the string, a new string is created.

All JavaScript string functions return a new string.

[see String.prototype]

In practice, this means, if you need to create a long string, such as repeatedly append to a string one thousand times, you should not use a loop with append to do that. Because, the process of creating a long string is expensive. (The workaround in this case is to use a array. Push string to the array, then use Array.prototype.join to convert it to string.)

String is 16 Bits Unit Sequence

Technically, each character in string is a 16 bits unit, not a β€œcharacter”, nor a Unicode codepoint. This is important when your string contains Unicode character such as emoji πŸ˜ƒ .

[see Character, Code Unit, Codepoint]

String Escape Sequence

Backslash sequence such as \n is called escape sequence.

console.log( "a\nb" );
// prints a and b each on its own line
// the \n is a escape sequence. It means newline

[see String Escape Sequence]

Unicode Escape Sequence

Unicode Escape Sequence

Create String

String Literal Expression

Use double quote or single quote for string. There's basically no difference between the two.

const s1 = "Mary's cat";
// double quote

const s2 = 'Mary\'s cat';
// single quote, with escape

Use \n for newline.

// use \n for newline
const ss = "cat\ndog";

console.log(ss); // prints 2 lines

Literal newline in string is not allowed. Use backslash to continue a line.

// illegal syntax. Literal newline is not allowed
const s = "a

// use backslash to continue a line
const s = "c\

console.log(s); // prints cd

Template String

String can also be quoted by using GRAVE ACCENT `, and have embedded expression.

[see Template String]

String Constructor

String value can also be created by using the constructor function String.

[see String Constructor]

Join String

The Plus Operator + is also used to join string.

console.log( "a" + "b" + "c" ===  "abc"); // true

String Methods


JavaScript String

JavaScript in Depth

JS in Depth

JS Obj Reference


JS in Depth

Basic Syntax

Value Types





Object and Inheritance



Iterable 🌟