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.

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.

Read Also:  authentication login with username or email in laravel

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

Read Also:  How to declare a global variable in Laravel 5.8?

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 :

Read Also:  PHP Star Triangle Program printing Source Code

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. Read Also:  PHP Laravel delete files from Public Folder
  2. Read Also:  how to retrieve data from database in html form using PHP
  3. Read Also:  Increase WordPress Memory Limit using PHP
  4. Read Also:  Drag And Drop Multiple File Uploading using DropzoneJS and PHP Example
  5. Read Also:  Laravel 6 Bootstrap Toggle Switch With Ajax Update To Mysql
  6. Read Also:  PHP Technical Interview Questions and Answers
  7. Read Also:  How to allow only numbers in Textbox using Jquery?
  8. Read Also:  How to declare a global variable in Laravel 5.8?
  9. Read Also:  PHP Technical Interview Questions and Answers
  10. Read Also:  Drag And Drop Multiple File Uploading using DropzoneJS and PHP Example