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

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Capitalize the First Letter of a String

Using JavaScript to Capitalize the First Letter of a String

February 24, 2022 Leave a Comment

We can use JavaScript to capitalize the first letter of a string by combining the JavaScript String toUpperCase() and substring() methods.

var theString = "how are you?"
theString = theString.substring(0,1).toUpperCase() + theString.substring(1);

Let’s look at an example below.


We can create a simple function that will use JavaScript to capitalize the first letter of a string.

We can do this by using the JavaScript toUpperCase() and substring() methods.

function capitalizeFirstLetter(theString){
  //This takes the first character of the string and capitalizes it
  theString = theString.substring(0,1).toUpperCase() + theString.substring(1);
}

Using JavaScript to Capitalize the First Letter of a String with a Click

In this simple example, we will let the user input a string, and we will capitalize the first letter of it.

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="capitalizeFirstLetter()">Capitalize First Letter</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 first character to uppercase using the String toUpperCase() method.

We will finally post the results using the textContent property.

function capitalizeFirstLetter(){
  
  //Get the user string
  var userString = document.getElementById("string1").value;
  
  //Capitalize the first letter of the string
  userString = userString.substring(0,1).toUpperCase() + userString.substring(1);

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

};

The final code and output for capitalizing the first letter of a string in JavaScript is below:

Code Output:


Capitalize First Letter

Full Code:

<div id="div1">
  <label for="string1">Enter a String:</label>
  <input type="text" id="string1" name="string1">
  <div id="click-me" onclick="capitalizeFirstLetter()">Capitalize First Letter</div>
  <div id="results"></div>
</div>

<script>

function capitalizeFirstLetter(){
  var userString = document.getElementById("string1").value;  
  userString = userString.substring(0,1).toUpperCase() + userString.substring(1);
  document.getElementById("results").textContent = userString;
};

</script>

Hopefully this article has been useful for you to learn how to use JavaScript to capitalize the first letter of a string.

Other Articles You'll Also Like:

  • 1.  Check That String Does Not Contain Character in JavaScript
  • 2.  How to Create a JavaScript Count Up Timer
  • 3.  Using JavaScript to Replace a Space with an Underscore
  • 4.  JavaScript String endsWith Method
  • 5.  Using JavaScript to Sort an Array of Strings
  • 6.  Using JavaScript to Get the Scroll Position
  • 7.  JavaScript String startsWith Method
  • 8.  Set the Height of a Div Using JavaScript
  • 9.  Using JavaScript to Calculate Average of Array of Numbers
  • 10.  charAt() JavaScript – Getting the Index Position of a Character

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