Open In App

How to Lowercase a String in JavaScript ?

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Approach 1: Using the toLowerCase() Method

This approach uses the built-in toLowerCase() method of the string object to convert all characters in the string to lowercase.

Example: Here, the toLowerCase() method is called on the originalString variable, which contains the string “Hello World”. The result is stored in the lowercasedString variable, and when logged to the console, it prints “hello world”.

Javascript




let originalString = "Hello World";
let lowercasedString = originalString.toLowerCase();
 
console.log(lowercasedString); // Output: "hello world"


Output

hello world

Approach 2: Using a Custom Function with map() and toLowerCase()

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"


Output

hello world

Approach 3: Using a Regular Expression

This approach utilizes a regular expression to match all uppercase letters in the string and replaces them with their lowercase equivalents using the replace() function.

Example: Here, a custom function toLowerCaseRegex is defined to convert the string to lowercase using a regular expression. The regular expression /[A-Z]/g matches all uppercase letters in the string. The replace() function is then used to replace each uppercase letter with its lowercase equivalent using a callback function (match => match.toLowerCase()). This function is called with the originalString variable, and the result is stored in lowercasedString, which is then logged to the console.

Javascript




function toLowerCaseRegex(string) {
  return string.replace(/[A-Z]/g, match => match.toLowerCase());
}
 
let originalString = "HeLLo WoRLD";
let lowercasedString = toLowerCaseRegex(originalString);
 
console.log(lowercasedString); // Output: "hello world"


Output

hello world


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads