• 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 / JavaScript Capitalize First Letter of Every Word

JavaScript Capitalize First Letter of Every Word

May 14, 2022 Leave a Comment

In JavaScript, we can capitalize the first letter of every word in a string easily with the help of a bunch of JavaScript String and Array methods. Here is our function to do this:

function capitalizeFirstLetter(str){
  var new_strings = str.split(" ");
  var new_string = [];
  for ( var i=0; i<new_strings.length; i++ ){
    new_strings[i] = new_strings[i].charAt(0).toUpperCase() + new_strings[i].substring(1);
  };
  new_string = new_strings.join(" ");
  return new_string;
};

Here would be a sample result using our function:

var someString = "this is a string with some words";

function capitalizeFirstLetter(str){
  var new_strings = str.split(" ");
  var new_string = [];
  for ( var i=0; i<new_strings.length; i++ ){
    new_strings[i] = new_strings[i].charAt(0).toUpperCase() + new_strings[i].substring(1);
  };
  new_string = new_strings.join(" ");
  return new_string;
};

console.log(capitalizeFirstLetter(someString))

#Output:
This Is A String With Some Words

When using string variables in JavaScript, we can easily perform string manipulation to change the value of the string variables.

One such manipulation is to capitalize the first letter of every word in a string.

We can easily capitalize the first letter of every word in JavaScript.

As seen in our function above, we first use the split() method to get an array of the strings from the string passed to the function. Then, we can loop over each word, and use the charAt() method to get the first character of the string and capitalize it with the toUpperCase() method. We then concatenate that back to the rest of the word with the help of the substring() method.

At the end, we can join the array of capitalized words back together with the join() method.

Below again is the function we created to capitalize the first letter of every word in a string.

function capitalizeFirstLetter(str){
  var new_strings = str.split(" ");
  var new_string = [];
  for ( var i=0; i<new_strings.length; i++ ){
    new_strings[i] = new_strings[i].charAt(0).toUpperCase() + new_strings[i].substring(1);
  };
  new_string = new_strings.join(" ");
  return new_string;
};

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

Other Articles You'll Also Like:

  • 1.  Loop Through an Array of Objects in JavaScript
  • 2.  React Axios Interceptor to Prevent Infinite Loops in JWT Authentication
  • 3.  Using JavaScript to Get the Length of Array
  • 4.  Using JavaScript to Run a Function Every 5 seconds
  • 5.  JavaScript asin – Find Arcsine and Inverse Sine of Number
  • 6.  Using JavaScript to Format the Date in mm dd yyyy
  • 7.  Using JavaScript to Get the Last Day of the Month
  • 8.  Reverse a String in JavaScript
  • 9.  How to Swap Values in an Array in JavaScript
  • 10.  JavaScript substring – How to Get a Substring From a String

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