JavaScript How to Convert String to Lowercase
Use the
toLowerCase() method on a string to convert all its characters to lowercase, like str.toLowerCase().Examples
Input"HELLO"
Output"hello"
Input"JavaScript Is Fun!"
Output"javascript is fun!"
Input"1234!@#"
Output"1234!@#"
How to Think About It
To convert a string to lowercase, think about changing every uppercase letter to its lowercase version while leaving other characters unchanged. The
toLowerCase() method does this by scanning the string and replacing uppercase letters with their lowercase counterparts.Algorithm
1
Get the input string.2
Call the <code>toLowerCase()</code> method on the string.3
Return the new string with all letters in lowercase.Code
javascript
const original = "Hello World!"; const lower = original.toLowerCase(); console.log(lower);
Output
hello world!
Dry Run
Let's trace "Hello World!" through the code
1
Input string
"Hello World!"
2
Apply toLowerCase()
"hello world!"
3
Output result
"hello world!"
| Original String | After toLowerCase() |
|---|---|
| Hello World! | hello world! |
Why This Works
Step 1: What toLowerCase() does
The toLowerCase() method creates a new string where all uppercase letters are converted to lowercase.
Step 2: No change to non-letters
Characters that are not letters, like numbers or symbols, stay the same.
Step 3: Returns new string
It does not change the original string but returns a new one with lowercase letters.
Alternative Approaches
Using toLocaleLowerCase()
javascript
const original = "İstanbul"; const lower = original.toLocaleLowerCase(); console.log(lower);
This method respects locale-specific rules, useful for languages with special lowercase rules.
Complexity: O(n) time, O(n) space
Time Complexity
The method processes each character once, so time grows linearly with string length.
Space Complexity
A new string is created to hold the lowercase result, so space also grows linearly.
Which Approach is Fastest?
toLowerCase() is the standard and fastest for most cases; toLocaleLowerCase() is slower but needed for locale-specific conversions.
| Approach | Time | Space | Best For |
|---|---|---|---|
| toLowerCase() | O(n) | O(n) | General lowercase conversion |
| toLocaleLowerCase() | O(n) | O(n) | Locale-aware lowercase conversion |
Use
toLowerCase() directly on your string variable to quickly get a lowercase version.Forgetting that
toLowerCase() returns a new string and does not change the original string.