JavaScript toLowerCase() method
The toLowerCase()
method in JavaScript is used to convert all the characters in a string to lowercase. This method is particularly useful when you want to standardize string data for comparison or display.
Syntax:
Return Value:
- Returns a new string with all the characters converted to lowercase. The original string remains unchanged.
Example 1: Basic Usage
In this example, the method converts the string "Hello, World!"
to "hello, world!"
, changing all uppercase letters to their lowercase equivalents.
Example 2: Handling Mixed Case
The toLowerCase()
method works on strings with mixed case.
Here, all letters in the string are converted to lowercase.
Example 3: Original String Remains Unchanged
The original string is not modified by the toLowerCase()
method.
Example 4: Non-Alphabetic Characters
Non-alphabetic characters remain unchanged when using toLowerCase()
.
In this example, the numbers and special characters stay the same, while the letters are converted to lowercase.
Example 5: Locale Considerations
The toLowerCase()
method is case-sensitive and follows the default locale of the environment. For instance, certain characters may have different lowercase representations in different locales.
In this case, the uppercase 'İ' is converted to the lowercase 'i̇' in the Turkish locale.
Summary:
- The
toLowerCase()
method converts all characters in a string to lowercase. - It does not change the original string; it returns a new string with the lowercase conversion.
- Non-alphabetic characters are unaffected by the method.
- The method's behavior can depend on the default locale, particularly for special characters.