Consider this code snippet that changes the case of a string:
const greeting = "Hello World"; const result = greeting.toUpperCase(); console.log(result);
What will be printed to the console?
const greeting = "Hello World"; const result = greeting.toUpperCase(); console.log(result);
Think about what toUpperCase() does to each letter.
The toUpperCase() method converts all letters in the string to uppercase. So "Hello World" becomes "HELLO WORLD".
Look at this code that changes string case:
const text = "TypeScript"; const lower = text.toLowerCase(); console.log(lower);
What will be printed to the console?
const text = "TypeScript"; const lower = text.toLowerCase(); console.log(lower);
Remember what toLowerCase() does.
The toLowerCase() method converts all letters in the string to lowercase. So "TypeScript" becomes "typescript".
Check this code that uses both uppercase and lowercase methods:
const phrase = "JavaScript Rocks!"; const part1 = phrase.substring(0, 10).toLowerCase(); const part2 = phrase.substring(10).toUpperCase(); console.log(part1 + part2);
What will be printed to the console?
const phrase = "JavaScript Rocks!"; const part1 = phrase.substring(0, 10).toLowerCase(); const part2 = phrase.substring(10).toUpperCase(); console.log(part1 + part2);
Look carefully at how substring and case methods combine.
The first 10 characters "JavaScript" become lowercase "javascript". The substring from index 10 is " Rocks!" which becomes uppercase " ROCKS!". Concatenated, it prints "javascript ROCKS!".
Observe this code that chains string methods:
const input = " Mixed CASE String "; const output = input.trim().toLowerCase().replace(/ /g, "_"); console.log(output);
What will be printed to the console?
const input = " Mixed CASE String "; const output = input.trim().toLowerCase().replace(/ /g, "_"); console.log(output);
Think about what each method does step by step.
trim() removes spaces at start and end, toLowerCase() makes all letters lowercase, and replace(/ /g, "_") replaces all spaces with underscores. Result is "mixed_case_string".
Consider these four code snippets. Only one will cause a runtime error when run. Which one is it?
Think about what happens if you call a string method on a non-string value.
Option D assigns a number to s and calls toUpperCase() on it. Numbers do not have this method, so it causes a runtime error. Other options safely handle null, undefined, or valid strings.