How do I convert a string to lowercase or uppercase in JavaScript?
Gable E
In JavaScript, you can convert a string to lowercase or uppercase using built-in string methods. Here's a step-by-step guide on how to perform these conversions:
1. Convert to lowercase:
To convert a string to lowercase, you can use thetoLowerCase() method. This method returns a new string with all alphabetic characters converted to lowercase. Here's an example:
1
2
3
4
5
var myString = 'Hello World';
var lowercaseString = myString.toLowerCase();
console.log(lowercaseString);
In this code snippet,toLowerCase() is called on themyString variable, which converts the string to lowercase. The resulting lowercase string is then logged to the console.
Output: "hello world"
2. Convert to uppercase:
To convert a string to uppercase, you can use thetoUpperCase() method. This method returns a new string with all alphabetic characters converted to uppercase. Here's an example:
1
2
3
4
5
var myString = 'Hello World';
var uppercaseString = myString.toUpperCase();
console.log(uppercaseString);
In this code snippet,toUpperCase() is called on themyString variable, which converts the string to uppercase. The resulting uppercase string is then logged to the console.
Output: "HELLO WORLD"
By following these steps and using thetoLowerCase() ortoUpperCase() methods, you can convert a string to lowercase or uppercase in JavaScript. Choose the appropriate method based on your specific requirement.