Curriculum
Module 08 // Core JavaScript
Strings & Essential Methods
Module Objective
slice, split, includes, indexOf, replace, trim, startsWith, endsWith, padStart, padEnd, repeat, charAt
Mental Model Realtime Simulation
INTERACTIVE_CANVASEditor_Pane
Loading...
Console_Output
Waiting for output...
Practical Code Examples
// Example 1
// 1. Template Literals
const user = "Subhajit";
const points = 100;
// Multiline + Variable Interpolation
const msg = `Hello ${user},
You have ${points} points.`;
console.log(msg);💡 Backticks allow for multiline strings and embedding variables directly with `${}` syntax, replacing old-fashioned concatenation with `+`.
// Example 2
// 2. Searching & Checking
const email = "contact@example.com";
console.log(email.includes("@")); // true
console.log(email.startsWith("contact")); // true
console.log(email.endsWith(".com")); // true💡 Modern string methods make checking content much cleaner than using `indexOf() !== -1`.
// Example 3
// 3. Modification (Pure)
const raw = " hello world ";
const clean = raw.trim().replace("hello", "hi").toUpperCase();
console.log(clean); // "HI WORLD"
console.log(raw); // " hello world " (Original remains untouched)💡 Strings are **immutable**. Methods like `trim()`, `replace()`, and `toUpperCase()` always return a **new string** and do not change the original.
Engine & Memory Architecture
Strings in Memory
1. Primitive Storage:
- Strings are stored in the Stack (if short) or a special String Pool in the Heap.
- Because they are immutable, the engine can optimize by pointing multiple variables to the same memory location if the strings are identical.
2. Immutability:
- When you "change" a string, the engine actually creates a brand new string in memory and updates the variable to point to the new address.
- Old unused strings are eventually cleaned up by the Garbage Collector.
3. UTF-16:
- JS strings are encoded in UTF-16. Most characters take 2 bytes, but some (like emojis) take 4 bytes.
- Performance: Complex string building in loops should use an Array +
join()to avoid creating thousands of intermediate strings in memory.