Explain Codes LogoExplain Codes Logo

Convert JavaScript String to be all lowercase

javascript
functions
best-practices
string-manipulation
Anton ShumikhinbyAnton Shumikhin·Dec 15, 2024
TLDR

Swiftly transform a string to lowercase using .toLowerCase() method:

let noise = "HELLO StackOverflow!".toLowerCase(); // "hello stackoverflow!" ... // because nobody likes shouting, right?

Why opt for toLowerCase()?

The .toLowerCase() method stands as your reliable tool for swift case conversion. When a convenient and effective solution is a necessity to transform a string to lowercase letters, .toLowerCase() is your solution.

Diving into more details on toLowerCase()

Now that we have touched the basics, let's dissect the use of .toLowerCase() and its best practices:

Exceptional circumstances and Special Characters

The strength of .toLowerCase() lies in its ability to withstand exception cases:

  • It handles alphabetical characters by turning them to lowercase.
  • Unchanged are numbers, spaces, and punctuations.
  • Considers special characters and unicode.

Example to display its prowess with special characters:

let chaoticStr = "Löve & Рëace!".toLowerCase(); // "löve & рëace!" ... // now that's what I call a peaceful transformation

Locale-Specific Scenarios

To deal with strings containing locale-centric characters, .toLocaleLowerCase() has got you covered:

let city = "İstanbul"; let lowerCity = city.toLocaleLowerCase('tr'); // "istanbul" ... // because in Turkish, Istanbul isn't written with an English "i"

Edge-Scenarios and Non-String Values

Ever wondered what happens with non-string values? Using .toLowerCase() would attempt to transform the value to a string first. But a TypeError awaits if it comes across null or undefined. Hence, always ensure validity of the value is string or perform a null check.

Readability and Reliability

Using .toLowerCase() enhances both clarity and dependability of your code. It is self-explanatory, indicating your intentions to anyone who has a look at the code.

Advanced applications with toLowerCase()

String transformation by chaining

One of the strengths of .toLowerCase() lies in its ability to chain with other methods. For instance, trimming white spaces followed by case conversion:

let cleanStr = " Clean Me Please! ".trim().toLowerCase(); // "clean me please!"... // I said "please" ;)

Regular Expressions used with toLowerCase()

If you only plan on lowercasing certain parts of a string, regular expressions might come in handy:

let task = "Lowercase ONLY vowels".replace(/[aeiou]/ig, vowel => vowel.toLowerCase()); // "LowerCase OnLy voweLs"... 'cause why should consonants have all the fun!

toLowerCase() with Arrays

For converting array of strings, .toLowerCase() pairs best with .map() method:

let students = ["Alice", "BOB", "Charlie"]; let lowerStudents = students.map(student => student.toLowerCase());

Be careful with non-string Arguments

Be cautious while dealing with non-string objects. If .toLowerCase() is called blindly, output might surprise you with [object Object] or a TypeError. Hence, always verify for valid string or use String(value).toLowerCase().