Case and includes
Learn string operations with toUpperCase, toLowerCase and includes.
Case and includes
toUpperCase turns every letter in a string into uppercase; toLowerCase turns them all into lowercase. Both return a new string and leave the original alone.
includes tells you whether a substring is present, returning true or false. It is case-sensitive: "Hello".includes("hel") returns false.
A case-insensitive search pattern
A common way to do a case-insensitive search is to first lowercase the string with toLowerCase, then call includes. Now "JavaScript" and "javascript" match.
The same trick helps when storing emails: whether the user types upper or lower, you lowercase before saving. Matches stay consistent.
Exercise Lowercase the email
Convert the email string to lowercase and store it in normalized. Print the result.
Show example solution
const email = "Alex@X.COM";
// convert to lowercase
const normalized = email.toLowerCase();
console.log(normalized);
Exercise Case-insensitive keyword search
Lowercase the title string first, then search for the keyword substring and store the result in found. Print it.
Show example solution
const title = "JavaScript Lessons";
const keyword = "lessons";
// lowercase and search
const found = title.toLowerCase().includes(keyword);
console.log(found);
Test yourself
-
What does "Hello".includes("hel") return?
"Hello".includes("hel")- true
- false
- "Hel"
- undefined
Show answer
false — Correct. includes is case-sensitive, so "hel" does not match "Hel".
-
Do a case-insensitive search
const text = "JavaScript is great"; const found = text.toLowerCase().____("javascript"); console.log(found);Show answer
const text = "JavaScript is great"; const found = text.toLowerCase().includes("javascript"); console.log(found); -
Match call to result
- "Hello".toUpperCase()
- "HELLO"
- "Hello".toLowerCase()
- "hello"
- "Hello".includes("el")
- true
- "Hello".includes("xx")
- false