JavaScript Strings 🎭

JavaScript Strings 🎭
Strings in JavaScript are sequences of characters enclosed in single ('), double ("), or template literals (` `).
1️⃣ Creating Strings
✅ 1. Using Quotes (' or " - Preferred)
let singleQuote = 'Hello';
let doubleQuote = "World";
✅ 2. Using Template Literals (` ` - Best for Dynamic Content)
let name = "Alice";
let message = `Hello, ${name}!`;
console.log(message); // Output: Hello, Alice!
✅ 3. Using new String() (Not Recommended)
let strObj = new String("Hello");
console.log(typeof strObj); // Output: object
📌 Avoid new String(), as it creates an object instead of a primitive string.
2️⃣ Common String Methods
Method Description Example
.length Returns the length of the string "Hello".length // 5
.toUpperCase() Converts to uppercase "hello".toUpperCase() // "HELLO"
.toLowerCase() Converts to lowercase "WORLD".toLowerCase() // "world"
.trim() Removes spaces from both ends " hello ".trim() // "hello"
.charAt(index) Returns the character at an index "Java".charAt(1) // "a"
.indexOf("str") Finds the first occurrence "hello".indexOf("l") // 2
.lastIndexOf("str") Finds the last occurrence "hello".lastIndexOf("l") // 3
.includes("str") Checks if a string contains another string "Hello".includes("He") // true
.startsWith("str") Checks if string starts with a value "JavaScript".startsWith("Java") // true
.endsWith("str") Checks if string ends with a value "file.txt".endsWith(".txt") // true
.slice(start, end) Extracts part of a string "Hello".slice(1, 4) // "ell"
.substring(start, end) Similar to slice() but can't use negative indexes "Hello".substring(1, 4) // "ell"
.substr(start, length) Extracts a substring with a length "Hello".substr(1, 3) // "ell"
.replace("old", "new") Replaces first occurrence "Hello World".replace("World", "JS") // "Hello JS"
.replaceAll("old", "new") Replaces all occurrences "abc abc".replaceAll("abc", "xyz") // "xyz xyz"
.split("delimiter") Splits a string into an array "a,b,c".split(",") // ["a", "b", "c"]
3️⃣ Examples of String Manipulation
✅ 1. Changing Case
let text = "JavaScript";
console.log(text.toUpperCase()); // "JAVASCRIPT"
console.log(text.toLowerCase()); // "javascript"
✅ 2. Trimming Spaces
let userInput = " hello world ";
console.log(userInput.trim()); // "hello world"
✅ 3. Extracting Parts of a String
let str = "Hello, World!";
console.log(str.slice(7, 12)); // "World"
console.log(str.substring(7, 12)); // "World"
console.log(str.substr(7, 5)); // "World"
✅ 4. Checking if a String Contains a Word
let sentence = "The quick brown fox";
console.log(sentence.includes("quick")); // true
console.log(sentence.startsWith("The")); // true
console.log(sentence.endsWith("fox")); // true
✅ 5. Replacing Text
let phrase = "I love Python";
let newPhrase = phrase.replace("Python", "JavaScript");
console.log(newPhrase); // "I love JavaScript"
✅ 6. Splitting a String into an Array
let csv = "apple,banana,cherry";
let fruits = csv.split(",");
console.log(fruits); // ["apple", "banana", "cherry"]
4️⃣ Template Literals ( ) - String Interpolation
Use backticks (`) for easier multi-line strings and variables inside strings.
✅ 1. Embedding Variables
let name = "Alice";
let age = 25;
let greeting = `Hello, my name is ${name} and I am ${age} years old.`;
console.log(greeting);
✅ 2. Multi-Line Strings
let message = `This is
a multi-line
string.`;
console.log(message);
5️⃣ Summary
✅ Strings are enclosed in ', ", or `.
✅ Use string methods for manipulation (toUpperCase(), trim(), slice(), etc.).
✅ Use template literals ( ) for easy variable insertion.
Date: 2025-03-29 00:00:00.000000