What is tolowercase() Method in JavaScript and its usage?

How were you going to use the tolowercase() method in JavaScript?

Are you ready to unfold the mysterious power of the tolowercase() method?

Keep on reading to learn new ideas concerning to tolowercase() method.

This article will show you different ways how to use this method effectively.

What is toLowerCase() method in JavaScript?

The String.prototype.toLowerCase() or the toLowerCase() method is used to convert a string to lowercase letters in JavaScript. It doesn’t alter the original string.

In other words, JavaScript.tolowercase method transforms all the characters in a string to their lowercase equivalents.

Syntax

string.toLowerCase() ✅  

Parameters

None

The toLowerCase() method doesn’t take any parameters.

Return value

The toLowerCase() method returns a new string where all the uppercase characters in the original string are converted to lowercase.

The original string remains unchanged because strings in JavaScript are immutable. If called on null or undefined, it raises a TypeError.

How to use the toLowerCase method in JavaScript?

Here’s an example of how to use the JavaScript.tolowercase method :

let string = "HI, WELCOME TO ITSOURCECODE.COM!";
let result = string.toLowerCase(); ✅ 
console.log(string); 
console.log(result); 

Output:

HI, WELCOME TO ITSOURCECODE.COM!

hi, welcome to itsourcecode.com!

The following are the different ways you can use the JavaScript.tolowercase method:

Directly on a string literal

let string = "ITSOURCECODE!".toLowerCase(); 
console.log(string); 

As you can see, the toLowerCase() is called directly on the string literal “ITSOURCECODE!”

Output:

itsourcecode!

On a string variable

let string = "HELLO, THIS IS ITSOURCECODE!";
let lowerCasestring = string.toLowerCase(); ✅
console.log(lowerCasestring);

In our second example, the toLowerCase() is called on the string variable string.

Output:

hello, this is itsourcecode!

In combination with other string methods

let string = "   Hi, WELCOME TO ITSOURCECODE.COM!   ";
let trimmedAndLowerCase = string.trim().toLowerCase();  
console.log(trimmedAndLowerCase); 

Here the toLowerCase() is used in combination with the trim() method, which removes whitespace from both ends of a string.

Output:

hi, welcome to itsourcecode.com!

📌Take note: The toLowerCase() method does not change the original string. It returns a new string where all the uppercase characters are converted to lowercase.

How to convert string to LowerCase and UpperCase in JavaScript?

You can convert a string to lowercase or uppercase in JavaScript, using the toLowerCase() and toUpperCase() methods respectively.

Here’s how you can use them:

let samplestring = "Hi, Welcome To Itsourcecode.com!";

// Convert to lowercase
let lowerCaseStr = samplestring .toLowerCase();  ✅
console.log("The result of converting to lowercase: " + lowerCaseStr)

// Convert to uppercase
let upperCaseStr = samplestring .toUpperCase(); 
console.log("The result of converting to uppercase: " + upperCaseStr)

As you can see, the toLowerCase() method is called on the string samplestring to convert it to lowercase, and the result is stored in lowerCaseStr.

Similarly, toUpperCase() method is called on samplestring to convert it to uppercase, and the result is stored in upperCaseStr.

Output:

The result of converting to lowercase: hi, welcome to itsourcecode.com!

The result of converting to uppercase: HI, WELCOME TO ITSOURCECODE.COM!

What is the difference between toLowerCase() and toUpperCase()?

The toLowerCase() and toUpperCase() are both methods in JavaScript used to change the case of the text in a string.

The toLowerCase() method converts all the characters in a string to lowercase.

On the other hand, the toUpperCase() method converts all the characters in a string to uppercase.

Both methods return a new string with the changed case, and do not modify the original string because strings in JavaScript are immutable.

So, the main difference between these two methods is that toLowerCase() converts a string to all lower case letters, while toUpperCase() converts a string to all upper case letters.

Conclusion

In conclusion, we have discuss the what is tolowercase() method in JavaScript and it’s usage.

We have provided examples of using the toLowerCase() method directly on a string literal, on a string variable, and in combination with other string methods like trim().

This article also explains how to convert a string to both lowercase and uppercase using the toLowerCase() and toUpperCase() methods respectively.

We are hoping that this article provides you with enough information that helps you understand the tolowercase in JavaScript.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Leave a Comment