All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Capitalize First Letter In JavaScript

Last Updated : Mar 11, 2024

How To Capitalize First Letter In JavaScript

In this article we will show you the solution of how to capitalize first letter in JavaScript, in JavaScript, to capitalize the letter we used toUpperCase() function. toUpperCase(): We can convert the string into uppercase letters by using this function.

This function used the duplicate of the original string. Now to capitalize the first letter, we will set the index position to 0.

Then add the rest of the letter to form the final string we are going to use the slice() function. We are going to see two different examples in this tutorial.

Step By Step Guide On How To Capitalize First Letter In JavaScript :-

Example 1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> how to capitalize first letter in javascript </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
    <style>
        h1 {
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
    </style>
</head>
<body>
    <center>
        <h1> TALKERSCODE </h1>
        <h3> how to capitalize first letter in javascript </h3>
    </center>
    <script>
        let string = "this is a string" ;
        let capital = string.charAt(0).toUpperCase() + string.slice(1) ;
        console.log(capital) ;
    </script>
</body>
</html>
  1. To write HTML code <! DOCTYPE html> to mention which version of the HTML file is written in.
  2. Then we have to write the <html> tag used to define the root of an HTML document. And also write the ending tag </html>.
  3. <head> tag used here to use to contain the metadata about the HTML file and end it with </head> tag
  4. Then <title> tag is used to set the title of the HTML document and end it with </title> tag.
  5. Attach an external CSS file using <link> tag to give style to the page
  6. <h1> tag used to add heading here and also adding the inline CSS here.
  7. Create a <Script> tag to write JavaScript within it.
  8. First use let to create a string.
  9. Then create another variable capital with the chatAt() method that returns the first character of the above string with the toUpperCase() function to convert the first character of the string to upper case.
  10. Now use slice() method with index position 1 to join the rest of the characters on the string
  11. Using console.log() to display the string by capitalizing the first letter.

Example 2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> how to capitalize first letter in javascript </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
    <style>
        h1 {
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
    </style>
</head>
<body>
    <center>
        <h1> TALKERSCODE </h1>
        <h3> how to capitalize first letter in javascript </h3>
    </center>
    <script>
        const capitalized = function(sentence) {
            let str = sentence.split(" ") ;
            for (let i=0 ; i<str.length ; i++) {
                str[i] = str[i][0].toUpperCase() + str[i].substring(1) ;
            }
            return str.join(" ") ;
        }
        console.log(capitalized("this is a string")) ;
    </script>
</body>
</html>
  1. To write HTML code <! DOCTYPE html> to mention which version of the HTML file is written in.
  2. Then we have to write the <html> tag used to define the root of an HTML document. And also write the ending tag </html>.
  3. <head> tag used here to use to contain the metadata about the HTML file and end it with </head> tag
  4. Then <title> tag is used to set the title of the HTML document and end it with </title> tag.
  5. Attach an external CSS file using <link> tag to give style to the page
  6. <h1> tag used to add heading here and also adding the inline CSS here.
  7. Create a <Script> tag to write JavaScript within
  8. Now use const to create a constant that is equal to a function with a formal parameter.
  9. Using the split() function to convert string to an array first
  10. Then using a for loop to capitalize the first letter of every word using the toUpperCase() function and join the rest of the letters in the array
  11. Using console.log() with the function with the string we want to capitalize the first letter of each word.

Conclusion :-

At last, here in conclusion, we can say that with this article’s help, we know how to capitalize the first letter in JavaScript.

I hope this article on how to capitalize first letter in JavaScript helps you and the steps and method mentioned above are easy to follow and implement

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪