Home/Concepts/Strings
🔤
Beginner⏱ 10 min#02 / 08

Strings

Text & String Methods

Strings are sequences of characters representing text. JavaScript provides 20+ built-in methods to search, transform, extract, and format text data.

Your Progress0/8 complete
0 visited0%
💡 Real-World Analogy

A string is like a bead necklace - each bead is one character. You can count the beads (.length), pick a specific bead (.charAt), cut a section (.slice), join two necklaces (+), or repaint the whole thing (.toUpperCase).

🔑 Key Concepts

String Creation

Single quotes, double quotes, or backtick template literals. Backticks allow expressions with ${}.

String Methods

20+ built-in methods: toUpperCase, slice, indexOf, includes, replace, split, trim, repeat and more.

Template Literals

Backtick strings allow multi-line text and embedding variables/expressions using ${ } syntax.

💻 Code Examples

1Creating Strings & Template Literals
strings.js
1let single = 'Hello World'; // single quotes
2let double = "Hello World"; // double quotes
3let template = `Hello World`; // backticks (ES6)
4 
5// Template literals - the modern way
6let name = "Thunder";
7let day = 4;
8let msg = `Day ${day} of ${name} Course!`;
9// result: "Day 4 of Thunder Course!"
10 
11// Multi-line string (backticks only)
12let poem = `Roses are red,
13Violets are blue,
14JavaScript is awesome,
15And so are you!`;
2Essential String Methods
strings.js
1let str = " Hello, JavaScript! ";
2 
3// Case transformation
4str.toUpperCase(); // " HELLO, JAVASCRIPT! "
5str.toLowerCase(); // " hello, javascript! "
6 
7// Whitespace removal
8str.trim(); // "Hello, JavaScript!"
9str.trimStart(); // "Hello, JavaScript! "
10str.trimEnd(); // " Hello, JavaScript!"
11 
12// Searching
13str.includes("Java"); // true
14str.indexOf("Java"); // 9
15str.startsWith(" H");// true
16str.endsWith("! "); // true
17str.length; // 22
3Slicing, Replacing & More
strings.js
1let msg = "The Thunder Course is amazing!";
2 
3// Extract substrings
4msg.slice(4, 11); // "Thunder"
5msg.slice(-8); // "amazing!"
6 
7// Replace content
8msg.replace("amazing", "legendary");
9// "The Thunder Course is legendary!"
10 
11// Split into array
12msg.split(" ");
13// ["The","Thunder","Course","is","amazing!"]
14 
15// Padding & repeating
16"5".padStart(3, "0"); // "005"
17"Ha".repeat(3); // "HaHaHa"
18 
19// String to number
20Number("42"); // 42
21parseInt("42px"); // 42
22parseFloat("3.14abc"); // 3.14

⚠️ Common Mistakes

❌ Mistake

Forgetting string methods return NEW strings

✅ Fix

Strings are immutable! let upper = str.toUpperCase(); - always save the result.

❌ Mistake

Using + for complex string building

✅ Fix

Use template literals (backticks) for cleaner, more readable interpolation.

❌ Mistake

indexOf returns -1 when not found - not falsy!

✅ Fix

-1 is truthy! Use includes() for simple true/false checks instead of indexOf.

📋 Quick Reference

.length.toUpperCase() / .toLowerCase().trim().slice(start, end).indexOf(str).includes(str).replace(old, new).split(sep).repeat(n).padStart(n, char)

Live Code Playground

Edit the code and press ▶ Run to see output instantly.

strings_playground.js
⟳ Loading editor...
💾 Auto-saved
// Press ▶ Run Code to execute and view output console logs...
📦
← Previous
Variables
Challenge Mode
Next →
Math Functions
🔢