Web Development - HTML, CSS & JavaScript
🔹 Escape Characters
2️⃣5️⃣ New Line
You can use \n and \t:
const message1 = "Hello\nWorld";
console.log(message1);
// Hello
// World
const message2 = "Name:\tRahul";
console.log(message2); // Name: Rahul (with tab)
🔹 Comparing Strings
2️⃣6️⃣ String Comparison
Strings can be compared:
console.log("apple" === "apple"); // true
console.log("Apple" === "apple"); // false - case-sensitive
const input = "ADMIN";
if (input.toLowerCase() === "admin") {
console.log("Access granted");
}
⭐ Practical String Processing
Imagine a user enters an email:
const input = " [email protected] ";
const email = input.trim().toLowerCase();
console.log(email); // [email protected]
if (email.includes("@")) {
console.log("Email looks valid");
}
This combines:
• trim() ↓ toLowerCase() ↓ includes()
🧠 Real-World Example
Suppose you receive this data:
const data = "Amit, Priya, Rahul, Neha";
const names = data.split(",");
const cleanedNames = names.map(name => name.trim());
console.log(cleanedNames); // ["Amit", "Priya", "Rahul", "Neha"]
const filteredNames = cleanedNames.filter(
name => name.startsWith("R")
);
console.log(filteredNames); // ["Rahul"]
⚠️ Common Beginner Mistakes
• Mistake 1: Forgetting that strings are zero-indexed
const word = "Hello";
console.log(word[0]); // H not e
• Mistake 2: Expecting string methods to modify the original string
const name = "rahul";
name.toUpperCase();
console.log(name); // still "rahul"
const upperName = name.toUpperCase(); // correct
•
Mistake 3: Forgetting trim()
• User input often has accidental spaces: " Rahul "
•
Mistake 4: Confusing split() and join()
• split() → string → array
• join() → array → string
💻 Practice
•
1️⃣ Reverse a String: Given const word = "JavaScript"; Try to produce tpircSavaJ. Hint: split() + reverse() + join()
•
2️⃣ Count Characters: Count characters in "JavaScript"
•
3️⃣ Convert to Uppercase: Convert "javascript is powerful" to uppercase.
•
4️⃣ Extract Text: From "JavaScript" extract Script
•
5️⃣ Replace Text: Convert "I love Python" into "I love JavaScript"
•
6️⃣ Clean User Input: Given const input = " HELLO JAVASCRIPT "; Produce hello javascript
•
7️⃣ Challenge: Given const email = " [email protected] ";
Create a cleaned version that:
- Removes extra spaces
- Converts everything to lowercase
- Checks whether it contains @
- Prints "Valid email format" if it does.
🧠 Double Tap ❤️ For More
6 · 2.2K ·