How to usea Custom Function with map() and toLowerCase() in Javascript
This approach defines a custom function toLowerCaseCustom
that splits the string into an array of characters, maps over each character converting it to lowercase using toLowerCase()
, and then joins the characters back into a string.
Example: Here, a custom function toLowerCaseCustom
is defined to convert the string to lowercase. It first splits the string into an array of characters, then uses the map()
function to iterate over each character and convert it to lowercase using toLowerCase()
, and finally joins the characters back into a string. The function is then called with the originalString
variable, and the result is stored in lowercasedString
Javascript
function toLowerCaseCustom(string) { return string.split( '' ).map(char => char.toLowerCase()).join( '' ); } let originalString = "HeLLo WoRLD" ; let lowercasedString = toLowerCaseCustom(originalString); console.log(lowercasedString); // Output: "hello world" |
hello world
How to Lowercase a String in JavaScript ?
In JavaScript, converting a string to lowercase means transforming all the characters in the string to their lowercase equivalents. This operation is often used to ensure consistent comparison or formatting of text.
Below are the approaches used to Lowercase a string in JavaScript:
Table of Content
- Using the toLowerCase() Method
- Using a Custom Function with map() and toLowerCase()
- Using a Regular Expression