1. Which method converts a string to uppercase?
The toUpperCase method converts all characters in a string to uppercase letters. It returns a new string and does not modify the original because strings are immutable. The method has no parameters. ToLowerCase works the opposite way, converting to lowercase. Both methods are commonly used for case-insensitive comparisons. They work with international characters, not just English letters.
const str = 'Hello World';
// toUpperCase() - all uppercase
const upper = str.toUpperCase();
console.log(upper); // 'HELLO WORLD'
console.log(str); // 'Hello World' (original unchanged)
// toLowerCase() - all lowercase
const lower = str.toLowerCase();
console.log(lower); // 'hello world'
// Case-insensitive comparison
const input = 'HELLO';
if (input.toLowerCase() === 'hello') {
console.log('Match!'); // This runs
}
// Works with international characters
const german = 'straße';
console.log(german.toUpperCase()); // 'STRASSE'
// Common use case - normalize input
function login(username, password) {
const normalizedUsername = username.toLowerCase();
// Compare with stored username
}
// Chain methods
const text = ' hello world ';
const result = text.trim().toUpperCase();
console.log(result); // 'HELLO WORLD'
// First letter uppercase
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}
console.log(capitalize('hELLO')); // 'Hello'