In this article, we will discuss a straightforward JavaScript program to convert the first letter of a string into uppercase.

Understanding the toUpperCase Method

The toUpperCase method is a built-in JavaScript function that converts all characters of a string to uppercase. To convert just the first letter of a string, we need to manipulate the string first before applying the method.

Metthod 1: Using the Substring

The substring method is a simple and efficient way to extract a portion of a string. By using this method, we can extract the first letter of the string, convert it to uppercase using the toUpperCase method, and then concatenate the rest of the string to form the final result.

				
					let str = "javascript";
let firstLetter = str.substring(0,1).toUpperCase();
let restOfString = str.substring(1);
let result = firstLetter + restOfString;

				
			

The result of this code will be “Javascript”, with the first letter of the string in uppercase.

Method 2: Using the CharAt

The charAt method is another way to extract a specific character from a string. This method takes an index as an argument, and returns the character at that position in the string. By using this method, we can extract the first letter of the string, convert it to uppercase using the toUpperCase method, and then concatenate the rest of the string to form the final result.

				
					let str = "javascript";
let firstLetter = str.charAt(0).toUpperCase();
let restOfString = str.substring(1);
let result = firstLetter + restOfString;

				
			

The result of this code will be “Javascript”, with the first letter of the string in uppercase.

Method 3: Using the Slice

The slice method is similar to the substring method, but it has a slightly different syntax. The slice method takes two arguments: the starting index and the ending index. By using this method, we can extract the first letter of the string, convert it to uppercase using the toUpperCase method, and then concatenate the rest of the string to form the final result.

				
					let str = "javascript";
let firstLetter = str.slice(0,1).toUpperCase();
let restOfString = str.slice(1);
let result = firstLetter + restOfString;

				
			

The result of this code will be “Javascript”, with the first letter of the string in uppercase.

Method 4: Using the Spread Operator

The spread operator is a newer feature in JavaScript, and it provides a concise way to extract elements from an array. By using this operator, we can convert the string into an array of characters, extract the first character, convert it to uppercase using the toUpperCase method, and then use the join method to concatenate the rest of the characters to form the final result.

				
					let str = "javascript";
let chars = [...str];
let firstLetter = chars[0].toUpperCase();
chars[0] = firstLetter;
let result = chars.join('');
console.log(result); 
				
			

The result of this code will be “Javascript”, with the first letter of the string in uppercase.


Thanks for reading. Happy coding!