Use the JavaScript toLowerCase() String method to convert a given string into a lowercase letter string. This method does not change the original string.
string.toLowerCase()
JavaScript toLowerCase
Simple example code converts JavaScript String to be all lower case.
<!DOCTYPE html>
<html lang="en">
<body>
<script>
var s1 = "ALPHABET"
console.log(s1)
console.log(s1.toLowerCase())
</script>
</body>
</html>
Output:
Just an example for toLowerCase()
, toUpperCase()
and a prototype for the not-yet-available toTitleCase()
or toProperCase()
:
String.prototype.toTitleCase = function() {
return this.split(' ').map(i => i[0].toUpperCase() + i.substring(1).toLowerCase()).join(' ');
}
String.prototype.toPropperCase = function() {
return this.toTitleCase();
}
var OriginalCase = 'Your Name';
var lowercase = OriginalCase.toLowerCase();
var upperCase = lowercase.toUpperCase();
var titleCase = upperCase.toTitleCase();
console.log('Original: ' + OriginalCase);
console.log('toLowerCase(): ' + lowercase);
console.log('toUpperCase(): ' + upperCase);
console.log('toTitleCase(): ' + titleCase);
Output:
Original: Your Name
toLowerCase(): your name
toUpperCase(): YOUR NAME
toTitleCase(): Your Name
Using in a Function:
function convertToLowerCase(input) {
return input.toLowerCase();
}
let result = convertToLowerCase("JavaScript Is Fun!");
console.log(result); // Output: "javascript is fun!"
Handling User Input:
let userInput = prompt("Enter some text:");
let lowerInput = userInput.toLowerCase();
console.log("You entered:", lowerInput);
Comparing Strings Case-Insensitively:
let str1 = "Apple";
let str2 = "apple";
if (str1.toLowerCase() === str2.toLowerCase()) {
console.log("The strings are equal (case-insensitive).");
} else {
console.log("The strings are not equal.");
}
- The
toLowerCase
method is often used when you need to perform case-insensitive comparisons or to standardize user input. - It does not modify the original string but returns a new string with all characters in lowercase.
- It can be particularly useful in form validation or processing data from various sources to ensure uniformity.
By using the toLowerCase
method, you can effectively manage and compare strings in a case-insensitive manner, ensuring that your JavaScript code handles text data more robustly.
Do comment if you have any doubts or suggestions on this method examples.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version