Changing Case and Trimming
Case conversion and whitespace removal are the two cleanup steps almost every piece of user input needs.
toUpperCase and toLowerCase return a new string. trim removes whitespace from both ends, with trimStart and trimEnd for one side. Normalising with both is the standard way to compare user input reliably.
EXERCISE
1Changing Case
Two methods flip a string's case, and like every string method they return a new value rather than changing the original.
The two methods:
const name = "Ada Lovelace";
console.log(name.toUpperCase()); // ADA LOVELACE
console.log(name.toLowerCase()); // ada lovelace
console.log(name); // Ada Lovelace <- unchanged
They apply to letters only:
console.log("abc-123!".toUpperCase()); // ABC-123!
// Digits and punctuation pass through untouched.
The main real use: comparing without caring about case:
const typed = "ADA@example.COM";
const stored = "ada@example.com";
console.log(typed === stored); // false
console.log(typed.toLowerCase() === stored.toLowerCase()); // true
function sameEmail(a, b) {
return a.toLowerCase() === b.toLowerCase();
}
console.log(sameEmail("Ada@X.com", "ada@x.com")); // true
Capitalising a first letter, which has no built-in method:
function capitalise(word) {
if (word.length === 0) return word;
return word[0].toUpperCase() + word.slice(1).toLowerCase();
}
console.log(capitalise("ada")); // Ada
console.log(capitalise("LOVELACE")); // Lovelace
console.log(capitalise("")); // ""
slice is covered two lessons from here. It takes everything from a position onwards.
Lowercasing both sides before comparing is the standard way to make text comparison case-insensitive, and it is what you want for emails, usernames and search terms. Store the original as the person typed it, and lowercase only at the moment you compare, so you never lose the capitalisation they chose.
EXERCISE
2Removing Whitespace
Invisible spaces at the edges of user input cause bugs that are genuinely hard to see, and one method removes them.
trim takes whitespace off both ends:
const typed = " ada@example.com ";
console.log(typed.length); // 22
console.log(typed.trim()); // "ada@example.com"
console.log(typed.trim().length); // 15
Whitespace means more than spaces:
console.log("\t tab and newline \n".trim()); // "tab and newline"
// Spaces, tabs and newlines all count.
It only touches the ends, never the middle:
console.log(" a b ".trim()); // "a b"
// The two spaces between a and b survive.
One side at a time, when you need it:
console.log(" hi ".trimStart()); // "hi "
console.log(" hi ".trimEnd()); // " hi"
The bug it prevents, in the shape it always takes:
const input = "ada@example.com "; // one trailing space, invisible
const users = ["ada@example.com", "grace@example.com"];
console.log(users.includes(input)); // false "no such user"
console.log(users.includes(input.trim())); // true
// The person typed the right thing. The space came from a copy-paste.
Pro Tip: Trim every piece of text that comes from a person, at the moment it arrives. Trailing spaces are invisible in a form field, survive copy and paste, and turn a correct login, a correct coupon code or a correct search into a failure with no explanation anyone can see.
EXERCISE
3Normalising Input Before You Use It
Combining trim and case conversion into one small function is the standard way to make text comparisons reliable.
The two steps, together:
function normalise(text) {
return text.trim().toLowerCase();
}
console.log(normalise(" ADA@Example.com ")); // ada@example.com
console.log(normalise("\t Hello \n")); // hello
Methods chain because each one returns a new string, so .trim() hands its result straight to .toLowerCase().
Where you apply it:
const registered = ["ada@example.com", "grace@example.com"];
function isRegistered(input) {
return registered.includes(normalise(input));
}
console.log(isRegistered(" ADA@example.com ")); // true
console.log(isRegistered("nobody@example.com")); // false
Normalise for comparison, store the original:
const submitted = " Ada Lovelace ";
const displayName = submitted.trim(); // "Ada Lovelace"
const searchKey = normalise(submitted); // "ada lovelace"
// Show the person what they typed, minus the accidental spaces.
// Compare and search using the flattened version.
A search box, which is the same idea:
const titles = ["Dune", "The Hobbit", "Brave New World"];
function search(query) {
const q = normalise(query);
return titles.filter((title) => normalise(title).includes(q));
}
console.log(search(" HOBBIT ")); // [ 'The Hobbit' ]
filter and includes arrive in the next lesson and in Unit 7. The shape is what matters: normalise both sides, then compare.
Pro Tip: Keep normalisation in one named function rather than scattering
.trim().toLowerCase()through your code. When the rules change, and they will, perhaps to strip dots from Gmail addresses or collapse double spaces, there is one place to change and every comparison in the program stays consistent with every other.
