• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / JavaScript / How to Convert a String to Lowercase In JavaScript

How to Convert a String to Lowercase In JavaScript

February 16, 2022 Leave a Comment

We can convert a String to lowercase in JavaScript easily by using the JavaScript String toLowerCase() method.

"This is Some Text".toLowerCase();

When working with strings, it can be useful to convert a string to lowercase. A lot of the time when comparing strings, you will need to convert them both to lowercase to compare them since most comparison operators are case sensitive.

In JavaScript, we can easily convert a string to lowercase using the toLowerCase() method.

Converting a String to Lowercase In JavaScript with a Click

In this simple example, we will let the user input a string, and we will convert it to lowercase.

Here is the HTML setup:

<div id="div1">
  <label for="string1">Enter a String:</label>
  <input type="text" id="string1" name="string1">
  <div id="click-me" onclick="stringToLowercase()">Convert to Lowercase</div>
  <div id="results"></div>
</div>

We will add an onclick event to our #click-me div that will run a function we will create. Our function will first use the value property along with the getElementById method to get the string the user has entered.

We will then convert the string to lowercase using the String toLowerCase() method.

We will finally post the results using the textContent property.

function stringToLowercase(){
  
  //Get the user string
  var userString = document.getElementById("string1").value;
  
  //Convert the string to lowercase
  var lowercaseString = userString.toLowerCase();

  //Display the results
  document.getElementById("results").textContent = lowercaseString;

}

The final code and output for converting a string to lowercase in JavaScript is below.

Code Output:


Convert to Lowercase

Full Code:

<div id="div1">
  <label for="string1">Enter a String:</label>
  <input type="text" id="string1" name="string1">
  <div id="click-me" onclick="stringToLowercase()">Convert to Lowercase</div>
  <div id="results"></div>
</div>

<script>

function stringToLowercase(){
  var userString = document.getElementById("string1").value;
  var lowercaseString = userString.toLowerCase();
  document.getElementById("results").textContent = lowercaseString;
}

</script>

Hopefully this article has been useful for you to learn how to convert a string to lowercase in JavaScript.

Other Articles You'll Also Like:

  • 1.  Math floor JavaScript – Using Math.floor() to Round Down to Floor
  • 2.  Using JavaScript to Change the Image src
  • 3.  Using JavaScript to Check if a Number is Divisible by 2
  • 4.  Using JavaScript to Wait 5 Seconds Before Executing Code
  • 5.  Changing the Background Image of a div in JavaScript
  • 6.  Using JavaScript to Set Visibility
  • 7.  Get the First Child Element and Change it with JavaScript
  • 8.  Creating a JavaScript Function to Add Two Numbers
  • 9.  Using JavaScript to Check if String Contains Only Letters
  • 10.  Using JavaScript to Rotate an Image

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy