TExcelTAD
Home › JavaScript › JS Strings

JavaScript Strings

A string is a sequence of characters — text. Names, messages, HTML snippets, JSON and URLs are all strings. JavaScript strings can be written with single quotes, double quotes or backticks.

js · try it yourself
const single = 'Hello';
const double = "World";
const backtick = `Hi ${single}!`;   // backticks allow embedded expressions
console.log(single, double, backtick);
Hello World Hi Hello!

Single and double quotes behave identically — pick one style and stay consistent. Backticks create template literals, which get their own chapter.

Quotes inside strings

You can use one kind of quote inside a string delimited by another. Otherwise, escape the quote with a backslash:

js · try it yourself
console.log("It's easy");
console.log('She said "hi"');
console.log('It\'s escaped');
console.log("Line one\nLine two");
console.log("Tab\tseparated");
console.log("Backslash: \\");
It's easy
She said "hi"
It's escaped
Line one
Line two
Tab	separated
Backslash: \
Escape Meaning
\' \" quote character
\\ backslash
\n new line
\t tab
é Unicode character (é)

Length and characters

length gives the number of characters (UTF-16 code units). Each character has a position (index) starting at 0:

js · try it yourself
const word = "JavaScript";
console.log(word.length);
console.log(word[0]);
console.log(word[4]);
console.log(word.at(-1));          // last character
console.log(word[word.length - 1]); // the classic way
console.log(word[50]);             // out of range
10
J
S
t
t
undefined

Joining strings

+ concatenates strings, and += appends to a string variable:

js · try it yourself
const first = "Ada";
const last = "Lovelace";
let full = first + " " + last;
full += " (1815–1852)";
console.log(full);
console.log("Score: " + 10 + 5);   // left to right: text first
console.log(10 + 5 + " points");   // numbers first
Ada Lovelace (1815–1852)
Score: 105
15 points

Strings are immutable

You can't change a character inside an existing string — assigning to an index is silently ignored (or throws in strict mode). String methods always return a new string:

js · try it yourself
const greeting = "hello";
try {
  greeting[0] = "J";
} catch (err) {
  console.log(err.message);
}
const shout = greeting.toUpperCase();
console.log(greeting, shout);
Cannot assign to read only property '0' of string 'hello'
hello HELLO

Comparing strings

=== compares strings character by character. < and > compare by Unicode code point — capital letters come before lowercase ones. For human-friendly alphabetical order use localeCompare:

js · try it yourself
console.log("apple" === "apple");
console.log("apple" < "banana");
console.log("Zebra" < "apple");          // "Z" (90) < "a" (97)
console.log("a".localeCompare("b"));      // negative: a comes first
true
true
true
-1

String objects

new String("text") creates a String object rather than a primitive. Don't use it: it's slower and === comparisons break.

js
const a = "text";
const b = new String("text");
typeof b;   // "object"
a === b;    // false

Tip: Primitive strings can still call methods like toUpperCase() — JavaScript temporarily wraps them in an object behind the scenes.

Test yourself

What is "code".length?

  1. 3
  2. 4
  3. 5
  4. undefined

What does "cat"[1] return?

  1. "c"
  2. "a"
  3. "t"
  4. undefined

Which escape sequence produces a new line?

  1. \t
  2. \n
  3. \r
  4. \b