JavaScript Capitalize first letter uppercase, lowercase and camel case

Here is a quick javascript capitalize first letter code try it for the pure JavaScript version of ucfirst. This code try it will allow you to capitalize the first letter uppercase of a string using JavaScript.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

javascript capitalize first letter

One of the most common operations(javascript capitalize first letter) with strings is to make the string capitalized: uppercase its first letter, and leave the rest of the string as-is.

I have learn to 4 ways to capitalize the first letter of A string in pure JS. Bellow Example Like as The built-in functions for Capitalize, uppercase, lowercase and camel case in JavaScript.

  • toUpperCase():
  • slice():
  • charAt():
  • replace():

How to make first letter of a string uppercase in JavaScript ?

uppercase first letter javascript

<br></br>
function capitalizeFLetter(userStr) 
{
    return userStr.charAt(0).toUpperCase() + userStr.slice(1);
}
<br></br>

This soure code snippet will use the JavaScript function charAt to get the character at a certain index.

var firstLetter = userStr.charAt(0);

After that I use the uppercase function in JavaScript to set this user strings to be in javascript first letter uppercase.

var uppercaseFirstLetter = userStr.charAt(0).toUpperCase();

Then I can included the lots of the user string on to this capital letter by using the js function slice() help for the capitalize first letter js which will allow us to get the easy way of the string as well as delete the first letter.

var stringWithoutFirstLetter = userStr.slice(1)

Putting this altogether I get the JavaScript soure code simple use a ucfirst() Best alternative Way Like below Example.

function capitalizeFLetter(userStr) 
{
    return userStr.charAt(0).toUpperCase() + userStr.slice(1);
}

How To Capitalize The First Letter Of A String In JavaScript?

Imp Note: You Don’t forget that if you just want to your any User string like as a any article or post capitalize for main presentational intention on a Great create a Web Page, CSS design might be a good helpful functions, just included a capitalize user define class to your any webpages HTML paragraph as well as use: js capitalize first letter of string

p.capitalize {
  text-transform: capitalize;
}

Example 1: uses slice() method

<!DOCTYPE html>   
<html>   
    <head>  
        <title>capitalize javascript - www.pakainfo.com</title> 
    </head>  
        
    <body style = "text-align:center;">   
    
        <h2 style = "color:blue;" >   
            Pakainfo   
        </h2>   
          
        <input id = "stringletterdata" type="text" name="stringletterdata"/>  
        <button onclick="jsCapitalizeFirstLetterUcase()">  
            Click to Results Get 
        </button>  
        <h3 id = "jsfletterID" style="color: blue"> 
        </h3> 
            
        <script>  
         
        function jsCapitalizeFirstLetterUcase() { 
          var stringletterdata = document.getElementById("stringletterdata"); 
          var get_data = document.getElementById("jsfletterID"); 
          var userStr = stringletterdata.value; 
          get_data.innerHTML = userStr[0].toUpperCase() +  
            userStr.slice(1); 
        } 
        </script>  
    </body>   
</html> 

Example 2: uses slice() method

<!DOCTYPE html>   
<html>   
    <head>  
        <title>javascript capitalize first letter of string	</title> 
    </head>  
        
    <body style = "text-align:center;">   
    
        <h2 style = "color:blue;" >   
            Pakainfo   
        </h2>   
            
        <input id = "stringletterdata" type="text" name="stringletterdata"/>  
        <button onclick="jsCapitalizeFirstLetterUcase()">  
            Click to Results Get 
        </button>  
        <h3 id = "jsfletterID" style="color: blue"> 
        </h3> 
            
        <script>  
         
        function jsCapitalizeFirstLetterUcase() { 
          var stringletterdata = document.getElementById("stringletterdata"); 
          var get_data = document.getElementById("jsfletterID"); 
          var userStr = stringletterdata.value; 
          get_data.innerHTML = userStr.charAt(0).toUpperCase() + 
           userStr.slice(1); 
        } 
        </script>  
    </body>   
</html> 

Example 3: uses string.replace() method

<!DOCTYPE html>   
<html>   
    <head>  
        <title>js capitalize first letter of string	</title>      
    </head>  
        
    <body style = "text-align:center;">   
    
        <h2 style = "color:blue;" >   
            Pakainfo   
        </h2>        
        <input id = "stringletterdata" type="text" name="stringletterdata"/>  
        <button onclick="jsCapitalizeFirstLetterUcase()">  
            Click to Results Get 
        </button>  
        <h3 id = "jsfletterID" style="color: blue"> 
        </h3> 
            
        <script>  
         
        function jsCapitalizeFirstLetterUcase() { 
          var stringletterdata = document.getElementById("stringletterdata"); 
          var get_data = document.getElementById("jsfletterID"); 
          var userStr = stringletterdata.value; 
          get_data.innerHTML =  
           userStr.replace(/^./, userStr[0].toUpperCase()); 
        } 
        </script>  
    </body>   
</html> 

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about javascript capitalize first letter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   PHP program Star Triangle Nested using Foreach Loop
  2.   Laravel 6 Multiple Where Eloquent Query
  3.   jQuery File upload size Limit validation Example
  4.   PHP Get ID of Last Inserted Record
  5.   Core PHP Creating SEO Friendly URL using htaccess
  6.   Simple Create WordPress Plugin Tutorial
  7.   jQuery Get Dynamic Element Selector Finding ID
  8.   HTML PHP Ajax Progress Bar
  9.   PHP Get country city location details
  10.   Data Tables State save local storage in jQuery