How to Convert String to UpperCase and LowerCase in JavaScript

How to Convert String to UpperCase and LowerCase in JavaScript


In this tutorial we will create a How to Convert String to UpperCase and LowerCase. This tutorial purpose is to teach how to change the string cases. This will tackle the overall process for changing of string cases. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is very easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to your website if you want to change your string cases. I will try my best to give you the easiest way of creating this program Converting cases. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface for our application. This code will display only textbox and buttons. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a href="" class="navbar-brand">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Convert String to UpperCase and LowerCase</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-6">
  17. <form>
  18. <div class="form-group">
  19. <label>Enter a String</label>
  20. <input type="text" class="form-control" id="str"/>
  21. </div>
  22. </form>
  23. </div>
  24. <div class="col-md-6">
  25. <div>
  26. <input type="radio" name="status" value="first"/>
  27. <label>Single</label>
  28. <input type="radio" name="status" value="all" style="margin-left:50px;"/>
  29. <label>All</label>
  30. </div>
  31. <br />
  32. <button type="button" class="btn btn-default" onclick="UpperCase()">Uppercase</button> <button type="button" class="btn btn-default" onclick="LowerCase()">Lowercase</button>
  33. </div>
  34. </div>
  35. </div>
  36. </body>
  37. <script src="script.js"></script>
  38. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically change the string cases from lower or upper. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. let string = document.getElementById("str");
  3. function GetRadio(){
  4. for (let i = 0; i < document.getElementsByName('status').length; i++){
  5. if (document.getElementsByName('status')[i].checked)
  6. {
  7. return document.getElementsByName('status')[i].value;
  8. }
  9. }
  10. }
  12. function UpperCaseFirstLetter(string) {
  13. return string[0].toUpperCase() + string.slice(1);
  14. }
  17. function LowerCaseFirstLetter(string) {
  18. return string[0].toLowerCase() + string.slice(1);
  19. }
  21. function UpperCase(){
  22. if(GetRadio() == "all"){
  23. string.value = string.value.toUpperCase();
  24. }else if(GetRadio() == "first"){
  25. string.value = UpperCaseFirstLetter(string.value);
  26. }
  27. }
  29. function LowerCase(){
  30. if(GetRadio() == "all"){
  31. string.value = string.value.toLowerCase();
  32. }else if(GetRadio() == "first"){
  33. string.value = LowerCaseFirstLetter(string.value);
  34. }
  35. }

In this code we first set the string variable with name string. We then create a method that will change the string cases called UpperCaseFirstLetter() and LowerCaseFirstLetter(). In the created method we set the string with a special function that will change the string to an uppercase or lower case accordingly.


The How to Convert String to UpperCase and LowerCase in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Convert String to UpperCase and LowerCase in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment