JavaScript Strings
In JavaScript, a string is a sequence of characters enclosed in single ('), double ("), or **backticks (). Strings are immutable, meaning they cannot be changed after creation.
1️⃣ Creating Strings
✅ Template literals (backticks) allow embedding variables using ${}.
2️⃣ String Properties & Methods
| Property / Method | Description | Example |
|---|---|---|
length | Returns the string length | "hello".length // 5 |
charAt(index) | Returns the character at the index | "hello".charAt(1) // "e" |
slice(start, end) | Extracts part of a string | "hello".slice(0, 3) // "hel" |
substring(start, end) | Similar to slice, but doesn’t accept negative indexes | "hello".substring(1, 4) // "ell" |
substr(start, length) | Extracts length characters | "hello".substr(1, 3) // "ell" |
toUpperCase() | Converts to uppercase | "hello".toUpperCase() // "HELLO" |
toLowerCase() | Converts to lowercase | "HELLO".toLowerCase() // "hello" |
trim() | Removes whitespace | " hello ".trim() // "hello" |
replace(old, new) | Replaces a substring | "hello".replace("l", "x") // "hexlo" |
split(separator) | Converts string to an array | "a,b,c".split(",") // ["a", "b", "c"] |
includes(substring) | Checks if a string contains another string | "hello".includes("ll") // true |
startsWith(substring) | Checks if string starts with | "hello".startsWith("he") // true |
endsWith(substring) | Checks if string ends with | "hello".endsWith("lo") // true |
3️⃣ Concatenating Strings
✅ Template literals (${}) are the best way to concatenate strings.
4️⃣ Searching in Strings
5️⃣ Extracting Part of a String
6️⃣ Replacing Parts of a String
✅ Use /g in replace() for replacing all occurrences.
7️⃣ Converting String to Array
8️⃣ Repeating Strings
9️⃣ Escaping Special Characters
Use \ to escape characters like quotes and new lines.
🔹 Summary
✔ Strings are immutable in JavaScript.
✔ Use template literals (${}) for dynamic content.
✔ slice(), substring(), and substr() extract parts of strings.
✔ replace() replaces only the first occurrence, use /g for all.
✔ split() converts a string into an array.
🚀 Need more advanced examples? Let me know!

