Javascript toLowerCase Function

Javascript is a popular scripting or programming language. Programmers often use Javascript to manipulate or manage data. For example, say, you are getting some data from a user in the fields of an HTML form. While getting the data from the user, you cannot control what the user is typing. But, you also need to show the data in a good format. Users may input capital letters where they are not needed or vice versa. While saving data to the database or showing data to a web page, as a programmer, it is necessary to take care of this function. For this purpose, we have Javascript’s built-in function toLowerCase() for converting a string to lower-case letters.

Syntax

The syntax for Javascript’s toLowercase() function is as follows:

string.toLowerCase();

The toLowerCase() function converts all the alphabets in a string into lower-case letters. This function does not change the original value of the variable. Instead, the function creates a new string for the instance. In this way, the function does not take any arguments, although the parentheses “()” are used for taking arguments. But, with the toLowercase() function, you follow the same conventions of a function.
Let us now look at some examples.

Examples

Suppose there is a string that includes some uppercase and lowercase letters, such as the following string:

let str = "Welcome to thE LinuxHint."


But, we need to convert the string entirely into lower-case letters. So, we will simply apply the following:

str.toLowerCase();


As you can see, the string has been converted to a formatted string. All the letters are now in lower-case format. It is as simple as that.

This function is for the string. But, this is not only restricted/limited here. We can apply the function to an array of strings and even to an array of objects containing strings, as well.

Let us take a look:
First, we will write down the array of strings, as follows:

let arr = ["Hi", "LinuxHint", "GREAT"]


Now, to change the term “GREAT” to all lowercase letters, we will apply the following:

arr[2].toLowerCase();

where ‘2’ is the index of “GREAT”.

As you can see, now, the term “GREAT” has been converted into lower-case letters. We did something similar for two other strings: [0] for the term “Hi” and [1] for the term “LinuxHint.”
Let us now see how we can apply this function to an array of objects in which the objects contain the string, as follows:

let arr = [{"name":"John"},{"name":"BOB"},{"name":"Ivan"}]


To convert the term “BOB” to all lower-case letters, we will change it as follows:

arr[1].name.toLowerCase();


Awesome. As you can see, we can do a lot with Javascript’s toLowerCase() function.

Conclusion

In this article, we showed you how to convert string characters to lower-case. We also showed you how to apply the same function to an array of strings and an array of objects containing strings. I hope that this article proved beneficial for converting an informal string into a cleaner and more formal-looking lowercase string. You can learn more about Javascript at linuxhint.com.



from Linux Hint https://ift.tt/3570VGt

Post a Comment

0 Comments