JS: String Overview

By Xah Lee. Date: . Last updated: .

String is one of the type of JavaScript values.

[see JS: Value Types]

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

String is a Primitive Value

JS: 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 JS: 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.

This is important when your string contains Unicode character such as emoji 😸.

[see JS: 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 JS: String Escape Sequence]

Unicode Escape Sequence

JS: 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 `, like this:

let x = `something`;

Expression of the form ${expression} in template string is evaluated.

console.log ( `I have ${1 + 1} cats` );
// I have 2 cats

[see JS: Template String]

String Constructor

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

But it's not a good practice, because there is no use of it that's not better done with string literal expression.

[see JS: String Constructor]

[see JS: String Object]

+ for Join String

JavaScript's plus operator + is also used to join string, like this:

"a" + "b" + "c"

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

String Methods

JS: String.prototype

Convert Character To/From Codepoint

JS: Convert Character To/From Codepoint

JS String

  1. String Overview
  2. Template String
  3. Char, Code Unit, Codepoint
  4. String Escape Sequence
  5. Unicode Escape Sequence
Liket it? I spend 2 years writing this tutorial. Help me spread it. Tell your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Object Reference
  5. DOM Scripting
  6. SVG
  7. Blog