Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
51 views

How to Lowercase a String in JavaScript _ - GeeksforGeeks

How to Lowercase a String in JavaScript _ - GeeksforGeeks
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

How to Lowercase a String in JavaScript _ - GeeksforGeeks

How to Lowercase a String in JavaScript _ - GeeksforGeeks
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

15/04/2024, 12:44 How to Lowercase a String in JavaScript ?

- GeeksforGeeks

DSA with JS - Self Paced JS Tutorial JS Exercise JS Interview Questions JS Array JS String JS Object

How to Lowercase a String in JavaScript ?


Last Updated : 12 Feb, 2024
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

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.

We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It !
Policy

https://www.geeksforgeeks.org/how-to-lowercase-a-string-in-javascript/?ref=ml_lbp 1/7
15/04/2024, 12:44 How to Lowercase a String in JavaScript ? - GeeksforGeeks

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";


We uselet
cookies to ensure you have the=best
lowercasedString browsing experience on our website. By using
toLowerCaseCustom(originalString);
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
console.log(lowercasedString); Policy // Output: "hello world"

https://www.geeksforgeeks.org/how-to-lowercase-a-string-in-javascript/?ref=ml_lbp 2/7
15/04/2024, 12:44 How to Lowercase a String in JavaScript ? - GeeksforGeeks

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

Get 90% Course fee refund in just 90 Days! Also get 1:1 Mock Interview,
Job assistance and more additional benefits on selected courses. Take up
the Three 90 challenge today!
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy

https://www.geeksforgeeks.org/how-to-lowercase-a-string-in-javascript/?ref=ml_lbp 3/7
15/04/2024, 12:44 How to Lowercase a String in JavaScript ? - GeeksforGeeks

“This course was packed with amazing and well-organized content! The
project-based approach of this course made it even better to understand
concepts faster. Also the instructor in the live classes is really good and
knowledgeable.”- Tejas | Deutsche Bank

With our revamped Full Stack Development Program: master Node.js and
React that enables you to create dynamic web applications.

So get ready for salary hike only with our Full Stack Development Course.

Suggest improvement

Previous Next

Default Constructor in JavaScript Bootstrap CDN

Share your thoughts in the comments Add Your Comment

Similar Reads
JavaScript Program to Check if a String How to Convert a String to Lowercase
Contains Uppercase, Lowercase, in JavaScript ?
Special Characters and Numeric Values

How to replace lowercase letters with How to create a new object from the
an other character in JavaScript ? specified object, where all the keys are
in lowercase in JavaScript?

How to convert uppercase string to How to convert lowercase string to


lowercase using PHP ? uppercase using PHP ?

PHP Program to Check if a String How to create half of the string in


Contains Uppercase, Lowercase, uppercase and the other half in
Special Characters and Numeric Values lowercase?

How to convert a String to Lowercase in Uppercase Booleans vs. Lowercase in


PHP? PHP
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy

https://www.geeksforgeeks.org/how-to-lowercase-a-string-in-javascript/?ref=ml_lbp 4/7
15/04/2024, 12:44 How to Lowercase a String in JavaScript ? - GeeksforGeeks

A amanv09

Article Tags : JavaScript , Web Technologies

A-143, 9th Floor, Sovereign Corporate


Tower, Sector-136, Noida, Uttar Pradesh -
201305

Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Geeks Community

We use cookies to ensureLanguages


you have the best browsing experience on our website. By using DSA
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Python Policy Data Structures
https://www.geeksforgeeks.org/how-to-lowercase-a-string-in-javascript/?ref=ml_lbp 5/7
15/04/2024, 12:44 How to Lowercase a String in JavaScript ? - GeeksforGeeks

Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL DSA Interview Questions
R Language Competitive Programming
Android Tutorial

Data Science & ML Web Technologies


Data Science With Python HTML
Data Science For Beginner CSS
Machine Learning Tutorial JavaScript
ML Maths TypeScript
Data Visualisation Tutorial ReactJS
Pandas Tutorial NextJS
NumPy Tutorial NodeJs
NLP Tutorial Bootstrap
Deep Learning Tutorial Tailwind CSS

Python Tutorial Computer Science


Python Programming Examples GATE CS Notes
Django Tutorial Operating Systems
Python Projects Computer Network
Python Tkinter Database Management System
Web Scraping Software Engineering
OpenCV Tutorial Digital Logic Design
Python Interview Question Engineering Maths

DevOps System Design


Git High Level Design
AWS Low Level Design
Docker UML Diagrams
Kubernetes Interview Guide
Azure Design Patterns
GCP OOAD
We use cookies to ensure you have
DevOps the best browsing experience on our website. System
Roadmap By usingDesign Bootcamp
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy Interview Questions

https://www.geeksforgeeks.org/how-to-lowercase-a-string-in-javascript/?ref=ml_lbp 6/7
15/04/2024, 12:44 How to Lowercase a String in JavaScript ? - GeeksforGeeks

School Subjects Commerce


Mathematics Accountancy
Physics Business Studies
Chemistry Economics
Biology Management
Social Science HR Management
English Grammar Finance
Income Tax

UPSC Study Material Preparation Corner


Polity Notes Company-Wise Recruitment Process
Geography Notes Resume Templates
History Notes Aptitude Preparation
Science and Technology Notes Puzzles
Economy Notes Company-Wise Preparation
Ethics Notes Companies
Previous Year Papers Colleges

Competitive Exams More Tutorials


JEE Advanced Software Development
UGC NET Software Testing
SSC CGL Product Management
SBI PO Project Management
SBI Clerk Linux
IBPS PO Excel
IBPS Clerk All Cheat Sheets

Free Online Tools Write & Earn


Typing Test Write an Article
Image Editor Improve an Article
Code Formatters Pick Topics to Write
Code Converters Share your Experiences
Currency Converter Internships
Random Number Generator
Random Password Generator
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
https://www.geeksforgeeks.org/how-to-lowercase-a-string-in-javascript/?ref=ml_lbp 7/7

You might also like