tolowercase javascript

Converting Text to Lowercase in JavaScript

If you want to convert text to lowercase in JavaScript, you can use the toLowerCase() method. This method returns the calling string value converted to lowercase.

Example:

var text = "HELLO WORLD";
var lowercaseText = text.toLowerCase();

console.log(lowercaseText); // Output: hello world

In the above example, we created a variable text and assigned it a string value "HELLO WORLD". Then we used the toLowerCase() method to convert this string to lowercase and assigned it to a new variable lowercaseText. Finally, we printed out the value of lowercaseText using console.log().

You can also use the toLowerCase() method directly on a string literal:

console.log("HELLO WORLD".toLowerCase()); // Output: hello world

This will directly return the lowercase version of the string.

Another way to convert text to lowercase is by using the toLocaleLowerCase() method. This method returns a string where all alphabetic characters have been converted to lowercase, taking into account the host environment's current locale.

Example:

var text = "ß";
var lowercaseText = text.toLocaleLowerCase();

console.log(lowercaseText); // Output: ss

In the above example, we created a variable text and assigned it a string value "ß". Then we used the toLocaleLowerCase() method to convert this string to lowercase, taking into account the current locale. Finally, we printed out the value of lowercaseText using console.log().

Code:


var text = "HELLO WORLD";
var lowercaseText = text.toLowerCase();

console.log(lowercaseText);

console.log("HELLO WORLD".toLowerCase());

var text = "ß";
var lowercaseText = text.toLocaleLowerCase();

console.log(lowercaseText);

In the above code, we declared a variable text and assigned it a string value "HELLO WORLD". Then we used the toLowerCase() method to convert this string to lowercase and assigned it to a new variable lowercaseText. Finally, we printed out the value of lowercaseText using console.log(). We also used the toLocaleLowerCase() method to convert a string containing a german character "ß" to lowercase and printed it out using console.log().

The code is also highlighted using the highlight.js library for better readability.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe