Excellent. After this chapter, you'll know almost everything you need for working with text in real applications.
You'll use these methods in:
🔍 Search bars
🔐 Login forms
📧 Email validation
📝 Form inputs
🤖 Chat apps
📄 Text processing
Chapter 8 — Strings
🎯 Goal (1 line)
Learn how to manipulate text using the string methods you'll use most often in real projects.
Think Like a Programmer
A string is simply text.
const name = "Pramod";
JavaScript gives us many built-in tools for working with text.
We'll focus only on the ones you'll use frequently.
1. .length
Finds the number of characters.
const name = "Alice";
console.log(name.length);
Output:
5
2. .toUpperCase()
Converts text to uppercase.
const name = "alice";
console.log(name.toUpperCase());
Output:
ALICE
3. .toLowerCase()
const email = "ADMIN@GMAIL.COM";
console.log(email.toLowerCase());
Output:
admin@gmail.com
4. .includes()
Checks if some text exists.
const message = "Hello JavaScript";
console.log(message.includes("Java"));
Output
true
5. .slice()
Cuts part of a string.
const word = "JavaScript";
console.log(word.slice(0, 4));
Output
Java
Think:
JavaScript
0123456789
Starts at index 0 and stops before index 4.
6. .trim()
Removes spaces from the beginning and end.
const username = " Alice ";
console.log(username.trim());
Output
Alice
Very common in login forms.
7. .replace()
Replace text.
const sentence = "I like cats";
console.log(sentence.replace("cats", "dogs"));
Output
I like dogs
8. .split()
Splits text into an array.
const fruits = "Apple,Banana,Orange";
console.log(fruits.split(","));
Output
["Apple", "Banana", "Orange"]
MCQs
Q1
What is the length?
const name = "Alice";
console.log(name.length);
A. 4
B. 5
C. 6
D. Error
✅ Answer
B
Q2
Output?
const city = "delhi";
console.log(city.toUpperCase());
A.
delhi
B.
DELHI
C.
Delhi
D. Error
✅ Answer
B
Q3
Output?
const city = "DELHI";
console.log(city.toLowerCase());
A.
DELHI
B.
Delhi
C.
delhi
D. Error
✅ Answer
C
Q4
Output?
const text = "JavaScript";
console.log(text.includes("Java"));
A. true
B. false
C. Java
D. Error
✅ Answer
A
Q5
Output?
const text = "JavaScript";
console.log(text.includes("Python"));
A. true
B. false
C. undefined
D. Error
✅ Answer
B
Q6
Output?
const word = "JavaScript";
console.log(word.slice(4));
A.
Java
B.
Script
C.
JavaScript
D. Error
✅ Answer
B
Q7
Output?
const word = "React";
console.log(word.slice(0, 2));
A.
Re
B.
ea
C.
React
D. Error
✅ Answer
A
Q8
Output?
const name = " Bob ";
console.log(name.trim());
A.
" Bob "
B.
Bob
C.
BOB
D. Error
✅ Answer
B
Q9
Output?
const text = "I like tea";
console.log(text.replace("tea", "coffee"));
A.
I like tea
B.
I like coffee
C.
coffee
D. Error
✅ Answer
B
Q10
Output?
const fruits = "Apple,Banana";
console.log(fruits.split(","));
A.
Apple Banana
B.
["Apple", "Banana"]
C.
Apple,Banana
D. Error
✅ Answer
B
Q11
Which method checks whether text exists?
A. replace()
B. includes()
C. split()
D. trim()
✅ Answer
B
Q12
Output?
const email = "ADMIN@gmail.com";
console.log(email.toLowerCase());
A.
ADMIN@gmail.com
B.
admin@gmail.com
C.
Admin@gmail.com
D. Error
✅ Answer
B
Q13
Output?
const text = "JavaScript";
console.log(text.slice(0, 10));
A.
JavaScript
B.
Java
C.
Script
D. Error
✅ Answer
A
Q14
Output?
const text = "Frontend";
console.log(text.includes("end"));
A. true
B. false
C. end
D. Error
✅ Answer
A
Q15 (Hardest)
Predict the output.
const email = " ADMIN@GMAIL.COM ";
const cleaned = email
.trim()
.toLowerCase();
console.log(cleaned);
A.
ADMIN@GMAIL.COM
B.
admin@gmail.com
C.
admin@gmail.com
D. Error
✅ Answer
B
Step by step:
trim()removes spaces.toLowerCase()converts all letters to lowercase.
Mini Coding Challenge
const sentence = "JavaScript is awesome";
console.log(sentence.includes("awesome"));
console.log(sentence.toUpperCase());
✅ Solution
Output:
true
JAVASCRIPT IS AWESOME
Mini Project — Email Cleaner
const email = " USER@GMAIL.COM ";
const cleanedEmail = email
.trim()
.toLowerCase();
console.log(cleanedEmail);
✅ Output
user@gmail.com
🚨 Common Beginner Mistakes
❌ Forgetting that strings are immutable
This doesn't change the original string:
const name = "alice";
name.toUpperCase();
console.log(name);
Output:
alice
Why?
Because toUpperCase() returns a new string.
Correct:
const name = "alice";
const upper = name.toUpperCase();
console.log(upper);
❌ Forgetting trim() before validation
Wrong:
const username = " alice ";
if (username === "alice") {
// won't run
}
Correct:
const username = " alice ";
if (username.trim() === "alice") {
// works
}
🧠 The 80/20 Cheat Sheet
text.length
text.toUpperCase()
text.toLowerCase()
text.includes("abc")
text.slice(0, 5)
text.trim()
text.replace("old", "new")
text.split(",")
These eight methods are the ones you'll use most often in real-world JavaScript.
🏆 You're About 35–40% Through the Core JavaScript
Here's what you've mastered so far:
✅ Variables (
let,const)✅ Conditions (
if,else)✅ Functions
✅ Arrays
✅ Objects
✅ Loops
✅ Array methods (
map,filter,find,some,every)✅ String methods
That's a very solid foundation.
📌 Next Chapter (Very Important)
Chapter 9 — Scope
You'll learn:
Block scope
Function scope
Why
constandletbehave differentlyCommon bugs caused by scope
Shadowing variables
Understanding scope is essential because it's one of the most common sources of bugs in JavaScript and a frequent interview topic.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.