How to capitalize first letter of string JavaScript

In this post, we will learn how to capitalize the first letter of a string in JavaScript with examples, The JavaScript built-in function toUpperCase() is used to convert a string to upper case, We will cover capitalizing first letter of string using Regex, capitalizing the first letter of string elements in array Javascript, How to capitalize first letter of each word of string JavaScript1. capitalize first letter of string JavaScript.

1. How to capitalize first letter of string JavaScript


To capitalize or uppercase the first letter of a string in javascript. We need to use two functions, the first is the charAt(0). which converts the first letter to uppercase and slice() to return the lower case string starting from the second letter. Let us understand with the below example.

let strMsg = 'welcome!';

const firstCharUpper = strMsg.charAt(0).toUpperCase() + strMsg.slice(1)

console.log(firstCharUpper);

Output

Welcome!

2. Capitalize first letter of string JavaScript Regex


In this example, We are using the regular expression to convert the first letter of the string with the help of a string. replace() method. It returns a new string after replacing some or all matching strings with a Regular expression to uppercase.The ES6 arrow function is used to call the upper case inside replace() method.

let strMsg = 'welcome!';
const first_Letter_Upp = strMsg.replace(/^\w/, char => char.toUpperCase());
console.log(first_Letter_Upp)

Output

Welcome!

3. capitalize first letter of array JavaScript


To capitalize the first letter of the array in Javascript, We have defined a custom function FirstLetterCaptialize that takes an array as a parameter and inside the function, The array.map() function will iterate over each element of the array and on each iteration apply two functions and return a new array after converting each element first letter to uppercase.

  • First function is the charAt(0). which converts the first letter to an uppercase
  • substring() to return the lower case element of the array string starting from the second letter.
var myArr = ["dev","JavaScript","data","value"];

function FirstLetterCaptialize(arr) {
  return arr.map(element => {
    return element.charAt(0).toUpperCase() + element.substring(1).toLowerCase();
  });
}

console.log(FirstLetterCaptialize(myArr));

Output

[ 'Dev', 'Javascript', 'Data', 'Value' ]

4. How to capitalize first letter of each word of string JavaScript


Sometimes we have to capitalize the first letter of each word of string JavaScript, In this example, we have used string.replace() matching and replacing the letter to uppercase using regular expression and returning a new string after converting each letter of the word in a string to upper case.Let us understand with below example.

const strMsg = "welcome to devenum javascript";
const uppStr = strMsg.replace(/\b\w/g, function(c) {
    return c.toUpperCase();
});

console.log(uppStr)

Output

Welcome To Devenum Javascript

Summary

In this post, we have learned how to capitalize first letter of string JavaScript with examples. We have used javascript built-in function toUpperCase() and cover capitalizing first letter of string using Regex, capitalizing the first letter of string elements in array Javascript, and How to capitalize the first letter of each word of string JavaScript1. capitalize first letter of string JavaScript.