JS Example for Beginners: JavaScript Program to Convert the First Letter of a String in to Upper Case

Hits: 5

(JavaScript programming Example for Beginners)

JavaScript Program to Convert the First Letter of a String in to Upper Case

In this example, you will learn to write a JavaScript program that converts the first letter of a string into uppercase.


Example 1: Convert First letter to Upper Case

// program to convert first letter of a string to uppercase
function capitalizeFirstLetter(str) {

    // converting first letter to uppercase
    let capitalized = str.charAt(0).toUpperCase() + str.slice(1);

    return capitalized;
}

// take input
let string = prompt('Enter a string: ');

let result = capitalizeFirstLetter(string);

console.log(result);

 

Output

Enter a string: javaScript
JavaScript

In the above program, the user is prompted to enter a string and that string is passed into the capitalizeFirstLetter() function.

  • The string’s first character is extracted using charAt() method. Here, str.charAt(0); gives j
  • The toUpperCase() method converts the string to uppercase. Here, str.charAt(0).toUpperCase(); gives J
  • The slice() method returns the rest of the string.
    Here, str.slice(1); gives avaScript
  • These two values are concatenated using the + operator.

Note: You can also extract the first character of a string using an array accessing property; str[0].

str.str[0]; // j

Example 2: Convert First letter to UpperCase using Regex

// program to convert first letter of a string to uppercase
function capitalizeFirstLetter(str) {

    // converting first letter to uppercase
    let capitalized = str.replace(/^./, str[0].toUpperCase());

    return capitalized;
}

// take input
let string = prompt('Enter a string: ');

let result = capitalizeFirstLetter(string);

console.log(result);

Output

Enter a string: javaScript
JavaScript

In the above program, the regular expression(regex) is used to convert the first letter of a string to uppercase.

  • The regex pattern is /^./ matches the first character of a string.
  • The toUpperCase() method converts the string to uppercase.

 

 

JS Example for Beginners: JavaScript Program to Convert the First Letter of a String in to Upper Case

Sign up to get end-to-end “Learn By Coding” example.



Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Leave a Reply

Your email address will not be published. Required fields are marked *