Introduction
In JavaScript, you might encounter scenarios where you need to convert a string to lower case. Fortunately, JavaScript provides a built-in method that makes this task simple and straightforward. In this tutorial, we will show you how to convert a string to lower case in JavaScript.
To convert a string to lower case in JavaScript, you can use the toLowerCase()
method. This method returns a new string with all alphabetic characters converted to lowercase while leaving non-alphabetic characters unchanged.
Here’s an example that demonstrates how to use the toLowerCase()
method:
let str = ""Hello World"";
let lowerCaseStr = str.toLowerCase();
console.log(lowerCaseStr);
// Output: ""hello world""
In the code snippet above, we have a string str
containing the text ““Hello World””. By calling the toLowerCase()
method on the str
variable, we are able to convert the string to lowercase and store the result in the lowerCaseStr
variable. Finally, we use console.log()
to display the converted string, which prints ““hello world”” to the console.
It is important to note that the toLowerCase()
method does not modify the original string. Instead, it returns a new string with the converted case. If you need to store the converted string, make sure to assign it to a new variable as shown in the example.
The toLowerCase()
method is not only limited to converting English characters to lowercase but can also be used with strings containing characters from any language.
let str = ""Привет, мир!"";
let lowerCaseStr = str.toLowerCase();
console.log(lowerCaseStr);
// Output: ""привет, мир!""
In this example, the string str
contains Cyrillic characters, which are commonly used in Russian. When we convert the string to lowercase using toLowerCase()
, the result is ““привет, мир!””.
Now that you know how to convert a string to lower case in JavaScript, you can use this knowledge in your own projects. Whether you need to normalize user input, compare strings, or perform any other string manipulation tasks, the toLowerCase()
method will be a handy tool in your JavaScript toolkit.
I hope you found this tutorial helpful!"