🔠Day 8 — Strings & String Methods in JavaScript
🔠Day 8 — Strings & String Methods in JavaScript
Welcome to Day 8 🌟 — Today’s session is all about **Strings**, one of the most common data types in JavaScript. Strings represent text and allow you to store names, messages, or any sequence of characters.
💬 1️⃣ What is a String?
A string is a sequence of characters written inside quotes.
let name = "Rahul";
let greeting = 'Hello World';
let message = `Welcome ${name}!`; // Template literal
🧩 2️⃣ String Properties
Strings behave like arrays — they have a .length property and can be accessed using index numbers.
let text = "JavaScript"; console.log(text.length); // 10 console.log(text[0]); // J console.log(text[text.length - 1]); // t
🎨 3️⃣ Common String Methods
toUpperCase() & toLowerCase()
let city = "Hyderabad"; console.log(city.toUpperCase()); console.log(city.toLowerCase());
slice(start, end)
let word = "JavaScript"; console.log(word.slice(0, 4)); // Java
substring() vs substr()
let str = "Learning JavaScript"; console.log(str.substring(9, 19)); // JavaScript
replace() and replaceAll()
let text2 = "I love JavaScript";
console.log(text2.replace("JavaScript", "React"));
trim()
Removes extra spaces from start and end.
let messy = " Hello World "; console.log(messy.trim());
🧮 4️⃣ Searching Strings
indexOf()— position of first occurrenceincludes()— checks if substring existsstartsWith()/endsWith()
let msg = "Frontend development is fun!";
console.log(msg.includes("Frontend")); // true
console.log(msg.startsWith("Front")); // true
console.log(msg.endsWith("fun!")); // true
💡 5️⃣ Template Literals
Backticks (`) make it easy to include variables inside strings.
let user = "Rahul";
let lang = "JavaScript";
console.log(`Hey ${user}, welcome to ${lang} learning!`);
🧠6️⃣ Practice Task
- Store your full name and extract first and last name using
slice(). - Convert a sentence to uppercase and lowercase.
- Check if your name starts with a vowel using
startsWith(). - Use
template literalsto create a greeting sentence.
🎯 Summary
Today, you learned how to manipulate strings using various built-in methods. Next, we’ll explore **Objects and JSON**, which are essential for structured data and real-world applications.
Comments
Post a Comment