Let's use our new string superpowers to build a little tool that analyzes a text message.
const message = " Mum, I finished my homework. Can I have burger now? ";
const cleaned = message.trim();
const length = cleaned.length;
const shouting = cleaned.toUpperCase();
const mentionsFood = cleaned.includes("burger");
const isQuestion = cleaned.endsWith("?");
const firstLetter = cleaned[0];
const lastLetter = cleaned.at(-1);
console.log(`Original: [${message}]`);
console.log(`Cleaned: [${cleaned}]`);
console.log(`Length: ${length} characters`);
console.log(`Shouting: ${shouting}`);
console.log(`Food? ${mentionsFood}`);
console.log(`Question? ${isQuestion}`);
console.log(`Starts with "${firstLetter}" and ends with "${lastLetter}"`);
console.log(`Type of length: ${typeof length}`);
It prints
Original: [ Mum, I finished my homework. Can I have burger now? ] Cleaned: [Mum, I finished my homework. Can I have burger now?] Length: 51 characters Shouting: MUM, I FINISHED MY HOMEWORK. CAN I HAVE BURGER NOW? Food? true Question? true Starts with "M" and ends with "?" Type of length: number
Try it yourself
Change the message to something of your own and run it again. Then add a new line that checks whether the message contains the word "please" (good manners!), and another that prints the message with "homework" replaced by "chores".
Grade 2, done! You now know what kinds of things live in the boxes. Next up in Grade 3, we'll learn how to do things with them: math, comparisons, and logic, using operators.