JavaScript:
Converting a string to lower case
How to:
In JavaScript, we convert a string to lowercase with the .toLowerCase()
method. It’s as simple as:
let greeting = "Hello, World!";
let lowerCaseGreeting = greeting.toLowerCase();
console.log(lowerCaseGreeting); // "hello, world!"
When used, each character in the original string is converted, if possible, to lowercase:
let mixedCase = "jAvAScript ROCKs!";
let lowerCased = mixedCase.toLowerCase();
console.log(lowerCased); // "javascript rocks!"
Note that characters which have no lowercase equivalents remain unchanged.
Deep Dive
In the old days, handling text meant being wary of character encodings and manual conversions. But in modern JavaScript, .toLowerCase()
abstracts away those complexities. Underneath, it uses Unicode mappings to convert characters, so it works with more than just A-Z.
Alternative methods do exist, like:
toLocaleLowerCase()
: This respects the user’s locale, making it essential for certain languages where the rules of lowercasing are context-specific.Regular expressions: Before
toLowerCase()
, developers might have used regex to replace uppercase characters manually.
Details-wise, remember .toLowerCase()
doesn’t change the original string (strings in JavaScript are immutable). You always get a new string. It also handles all characters recognized as uppercase by the Unicode standard, which means you’re covered across different languages and scripts.