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:

string.toLowerCase()

Return Value:

  • Returns a new string with all the characters converted to lowercase. The original string remains unchanged.

Example 1: Basic Usage

let str = "Hello, World!"; let lowerStr = str.toLowerCase(); console.log(lowerStr); // "hello, world!"

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.

let str = "JaVaScRiPt Is AwEsOmE!"; let lowerStr = str.toLowerCase(); console.log(lowerStr); // "javascript is awesome!"

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.

let str = "HELLO, WORLD!"; let lowerStr = str.toLowerCase(); console.log(str); // "HELLO, WORLD!" (original string remains unchanged) console.log(lowerStr); // "hello, world!" (new lowercase string)

Example 4: Non-Alphabetic Characters

Non-alphabetic characters remain unchanged when using toLowerCase().

let str = "123 ABC! @#"; let lowerStr = str.toLowerCase(); console.log(lowerStr); // "123 abc! @#"

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.

let str = "İstanbul"; // Turkish capital 'I' let lowerStr = str.toLowerCase(); console.log(lowerStr); // "i̇stanbul" (correct Turkish lowercase)

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.