Lesson 31 +10 XP

String Basics

String Basics

A string is a sequence of characters like letters, numbers, and symbols.

Writing strings

You can use single quotes, double quotes, or backticks:

let a = "Hello";
let b = 'Hello';
let c = `Hello`;

All three create the same string.

String length

let text = "Hello";
text.length; // 5

Access characters

text[0];      // "H"
text.charAt(1); // "e"

Concatenation

Join strings with +:

"Hello " + "World"; // "Hello World"

Escaping quotes

Use a backslash to put quotes inside a string:

let msg = "She said \"Hi\"";

Common escapes

  • \n newline
  • \t tab
  • \\ backslash
  • \" and \' quotes

TL;DR

  • Strings are text wrapped in ', ", or `.
  • .length gives the number of characters.
  • + joins strings.
  • Backslash escapes special characters.