🧵 Everything You Ever Wanted to Know About JavaScript Strings

AnjaliAnjali
2 min read

A language is what we use to talk to machines; strings are what we use to talk to people.”

1 Primitive vs Object Strings

jsCopyEditlet s  = "Hello World";
console.log(typeof s);  // "string"

let s1 = new String("Hello");
console.log(typeof s1); // "object"

Why care? Primitive strings are lightweight and immutable; new String() builds a heavy wrapper. Use primitives 99 % of the time.


2 Immutability in Action

jsCopyEdits[0] = "p";      // đźš« does nothing
console.log(s);  // "Hello World"

Strings can’t be mutated in place—you always get a new string.


3 Bread‑and‑Butter Methods

GoalExampleNote
Lengths.lengthUTF‑16 code units
First/last findindexOf("l"), lastIndexOf("l")-1 if missing
Extractslice(1,4), substring(1,4), substr(1,4)never mutates
Replacereplace("l","p")only first match unless /g
Upper/LowertoUpperCase() / toLowerCase()
Splitsplit(" ") → ["Hello","World"]
Checkincludes, startsWith, endsWith
Repeatrepeat(3)
Trimtrim() / trimStart() / trimEnd()

4 Strict vs Loose Comparison

jsCopyEditlet str1 = "Hello";
let str2 = "hello";

str1 === str2; // false
str1 ==  str2; // false

Use ===—avoid coercion surprises.


5 Escaping Quotes

jsCopyEditlet quote = "He said, \"Hello, World!\"";
console.log(quote);

6 A Dash of Regex

jsCopyEdit/World/i.test("Hello, World!"); // true

Regex + strings = super‑powers: validation, search‑and‑replace, parsing.


7 Full Code Playground



let s = "Hello World";
console.log(typeof s);               // "string"

let s1 = new String("Hello");
console.log(typeof s1);              // "object"

// Immutability
s[0] = "p";
console.log(s);                      // "Hello World"

// Core 
console.log(s.length);               // 11
console.log(s[0]);                   // "H"

console.log(s.indexOf("l"));         // 2
console.log(s.lastIndexOf("l"));     // 9
console.log(s.slice(1, 4));          // "ell"
console.log(s.substring(1, 4));      // "ell"
console.log(s.substr(1, 4));         // "ello"
console.log(s.replace("l", "p"));    // "Heplo World"
console.log(s.toUpperCase());        // "HELLO WORLD"
console.log(s.toLowerCase());        // "hello world"
console.log(s.charAt(0));            // "H"
console.log(s.charCodeAt(0));        // 72
console.log(s.split(" "));           // ["Hello", "World"]
console.log(s.includes("l"));        // true
console.log(s.startsWith("Hello"));  // true
console.log(s.endsWith("World"));    // true
console.log(s.repeat(3));            // "Hello WorldHello WorldHello World"
console.log(s.trim());               // "Hello World"

// Comparisons
let str1 = "Hello";
let str2 = "hello";
console.log(str1 === str2); // false
console.log(str1 ==  str2); // false

// Escaping
let quote = "He said, \"Hello, World!\"";
console.log(quote); // "He said, \"Hello, World!\""


// Regex
let regex = /world/i;
console.log(regex.test("Hello, World!")); // true

8 Takeaways

  1. Prefer primitives; wrappers add weight.

  2. Strings are immutable: every method returns a brand‑new string.

  3. Master search, extract, replace, and regex for real‑world tasks.

10
Subscribe to my newsletter

Read articles from Anjali directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anjali
Anjali