Uppercase first letter of a string variable - Javascript String Operation

Javascript examples for String Operation:String Case

Description

Uppercase first letter of a string variable

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {/*from   w  w  w.j  a v a2 s  .  c  om*/

String.prototype.ucwords = function() {
    return this.toLowerCase().replace(/\b[a-z]/g, function(letter) {
        return letter.toUpperCase();
    });
}
console.log("hello world".ucwords()); 
console.log("this is a test".ucwords());

    });

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials