Description
The JavaScript String method toLowerCase()
returns the given string in lowercase, without changing the original string.
- It returns a new string and doesn't change the original string.
- It is commonly used to convert a text to its lowercase.
Syntax
The method toLowerCase()
has the below syntax, where str
is a string.
str.toLowerCase()
Parameters
The method toLowerCase()
doesn't allow any parameters.
Result
Returns the lowercase representation of a string, without changing the original string.
Returns TypeError
for null and undefined values.
Example 1: Using the Method
The below example shows the basic usage of the method.
var str1 = "Hello World!";
var str2 = "HELLO WORLD!";
var str3 = "hello world!";
document.write(str1.toLowerCase()); // Prints: hello world!
document.write(str2.toLowerCase()); // Prints: hello world!
document.write(str3.toLowerCase()); // Prints: hello world!
Output:
hello world!
hello world!
hello world!
Example 2: Method Returns TypeError
The method returns TypeError
for null
and undefined
values.
var str1; // This variable contains undefined
var str2 = null; // This variable contains null
// Returns TypeError
document.write(str1.toLowerCase()); // Returns TypeError
document.write(str2.toLowerCase()); // Returns TypeError
Output:
TypeError: Cannot read properties of undefined (reading 'toLowerCase')
TypeError: Cannot read properties of null (reading 'toLowerCase')
Overall
The JavaScript String method toLowerCase()
returns the given string in lowercase, without changing the original string.