JavaScript Strings

JavaScript Strings

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

let str1 = "Hello, World!"; // Double quotes let str2 = 'Hello, World!'; // Single quotes let str3 = `Hello, ${str1}`; // Template literals (ES6)

Template literals (backticks) allow embedding variables using ${}.

2️⃣ String Properties & Methods

Property / MethodDescriptionExample
lengthReturns 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

let first = "Hello"; let last = "World"; // Using + let result = first + " " + last; // "Hello World" // Using concat() let result2 = first.concat(" ", last); // "Hello World" // Using template literals let result3 = `${first} ${last}`; // "Hello World"

Template literals (${}) are the best way to concatenate strings.

4️⃣ Searching in Strings

let text = "JavaScript is fun!"; console.log(text.indexOf("Script")); // 4 console.log(text.lastIndexOf("i")); // 13 console.log(text.includes("fun")); // true console.log(text.startsWith("Java")); // true console.log(text.endsWith("!")); // true

5️⃣ Extracting Part of a String

let str = "JavaScript"; console.log(str.slice(0, 4)); // "Java" console.log(str.substring(4, 10)); // "Script" console.log(str.substr(4, 6)); // "Script"

6️⃣ Replacing Parts of a String

let text = "Hello JavaScript!"; // Replace first occurrence console.log(text.replace("JavaScript", "World")); // "Hello World!" // Replace all occurrences (ES6) console.log(text.replace(/o/g, "0")); // "Hell0 JavaScript!" // Using replaceAll() (ES2021) console.log(text.replaceAll("o", "0")); // "Hell0 JavaScript!"

✅ Use /g in replace() for replacing all occurrences.

7️⃣ Converting String to Array

let str = "apple,banana,orange"; let arr = str.split(","); console.log(arr); // ["apple", "banana", "orange"]

8️⃣ Repeating Strings

console.log("Ha".repeat(3)); // "HaHaHa"

9️⃣ Escaping Special Characters

Use \ to escape characters like quotes and new lines.

let text = "He said, \"JavaScript is awesome!\""; let newLine = "First line\nSecond line";

🔹 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!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close