JavaScript toLowerCase Function is used to convert strings to lowercase letters.
The JavaScript toLowerCase method does not modify the actual string. It just converted the complete statement of string to lowercase letters.
Introduction
JavaScript is a very essential part of web development. Inside JavaScript there are many types of methods available, in this article, we also discuss one of the basic JS methods toLowerCase() which is mainly used to convert string uppercase to lowercase letters.
Syntax of JS toLowerCase
1 |
string.toLowerCase() |
Note: No parameters are needed.
Basic Example: How to use toLowerCase in JavaScript
1 2 3 4 5 6 7 |
<script> let sentence = 'This IS a toLowerCase method EXample'; console.log(sentence.toLowerCase()); // expected output: "this is a tolowercase method example" </script> |
When We Can Use JavaScript toLowerCase
- When you create any login or sign-up form with an email field, at the time of taking the email to store it on DB, you can use JavaScript
toLowerCase()
method to lowercase the letters on DB. - Also, JS toLowerCase method is used with textarea data and any editor data to convert all the letters to lowercase then store it on our backend database.
Example to Lowercase The Email
1 2 3 4 |
var email = "PHPCoderTech@Gmail.com"; var lowercase_email = email.toLowerCase(); console.log(lowercase_email); |
Output:
Example to Check Special Characters with JS toLowerCase Method
According to the below example, we can see JS toLowerCase method does not affect the special characters.
1 2 3 |
var str = 'It iS a 5r&e@@t Day.'; var string = str.toLowerCase(); console.log(string); |
Output:
JavaScript toLowerCase Is Not a Function
As we all know JS toLowerCase method only works for strings not for numbers.
When anyone uses the toLowerCase function with the number then it will throw this error “JavaScript toLowerCase Is Not a Function“.
If you want to do that check the below example,
1 2 3 |
var ans = 334; var temp = ans.toString().toLowerCase(); console.log(temp); |
In the above example first, we convert the number to string then we use the JS lowercase method. So it returns an output not an error.
Conclusion
In this complete article, we discussed many types of examples related to the toLowerCase function and also discuss when we can use it and how we can use it.
Please let me know if you are facing any issues at the time of use.
You can test all the examples here, JSFiddle – Code Playground.
Happy Coding..!
[…] Also Read: JavaScript toLowerCase Function With Examples […]