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 methods return a new string.

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, Code Point]

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

Literal String 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
b";

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

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

String Topic

  1. JS: String Overview
  2. JS: Template String
  3. JS: String Escape Sequence
  4. JS: Unicode Escape Sequence
  5. JS: String Object
  6. JS: String.prototype
  7. JS: String to Number
  8. JS: Encode URL, Escape String
  9. JS: Format Number
  10. JS: JSON
  11. JS: Source Code Encoding
  12. JS: Allowed Characters in Identifier
Liket it? 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 Basics
  4. JS in Depth
  5. JS Reference
  6. DOM
  7. SVG
  8. JS Misc