In this tutorial, we will learn about how to capitalize first letter in JavaScript. If you’re working with strings in your code, there are two JavaScript methods that can come in handy: charAt() and toUpperCase(). With the charAt() method, you can retrieve a specific character from a string by specifying its index. Meanwhile, the toUpperCase() method can convert an entire string to uppercase letters.
If you need to capitalize first letter javascript of a string, you can use these two methods together. First, use charAt() to retrieve the first character of the string. Then, use toUpperCase() to convert it to uppercase. Finally, you can concatenate the uppercase first letter with the rest of the original string using the slice() method.
By utilizing these methods, you can manipulate and transform strings to fit your specific programming needs. Whether you’re working on a small script or a large-scale project, understanding the various methods available to you can help streamline your code and make it more efficient.
To achieve Capitalization i.e. capitalize first letter of each word JavaScript we consider these three function –
(1) charAt()
The charAt() function returns the first character of the String. When it comes to manipulating Strings in JavaScript, there need to access a particular character within the String. In such scenarios, utilizing the charAt method of Strings in JavaScript can prove to be useful. By providing an index value as a parameter, this method returns the character that resides at the specified index.
It is important to note that if the index value provided is out of range. Then the charAt method will return an empty string. Additionally, if no index value is provided, the method will automatically use the default index value of 0. By understanding these nuances of the charAt method, we can effectively leverage it to achieve our desired String manipulations.
Syntax :
string.charAt(index)
Example :
const str = 'developerhelps';
const str2 = str.charAt(0);
console.log(str2);
Output :
d
(2) toUpperCase()
The toUpperCase() return all the characters of the input String in Uppercase. In JavaScript, the toUpperCase() is a built-in string function. We use the toUpperCase() method to convert the string’s lowercase characters to uppercase. Since JavaScript strings are immutable, the toUpperCase() method has no effect on the value of the string. String toUpperCase() method only works with the string data type and it will throw a TypeError. If called by any null or undefined object..
Syntax :
string.toUpperCase()
Example :
const str = 'developerhelps';
const str2 = str.toUpperCase();
console.log(str2);
Output :
DEVELOPERHELPS
The example above demonstrates the utilization of the toUpperCase() function to convert all characters of a string to uppercase. However, this may not always be the desired outcome. If we aim to capitalize only the first letter of a string. A better approach would be to use the charAt() function to isolate the first character. Then convert it to uppercase using toUpperCase(). Then retrieve the remaining characters of the string using the slice() function. We can achieve the desired outcome of capitalize first letter of each word javascript. While preserving the rest of its original format.
(3) slice()
This slice() function slices a given string from a specified “start” position until specifies “end” position. The slice() function in Array class of Javascript returns a shallow copy of a subarray into a new array object which includes the elements from a user-specified start index to a user-specified end index (end not included). The original array is not modified
Syntax :
string.slice(start,end)
Example :
const str = 'developerhelps';
const str2 = str.slice(1);
console.log(str2);
Output :
eveloperhelps
Now Let us combine all these function to capitalize the first word of the input String
const str = 'developerhelps';
const str2 = str.charAt(0).toUpperCase() + str.slice(1);
console.log(str2);
Output :
D
After this we capitalize the first letter of a word but we have to captialize the first letter of each word in a given string. This can be achieved via –
const inString = 'capitalize the first letter of each word in javaScript';
const inArr = inString.split(" ");
for (var count = 0; count < inArr.length; count++) {
inArr[count] = inArr[count].charAt(0).toUpperCase() + inArr[count].slice(1);
}
const outString = inArr.join(" ");
console.log(outString);
Output :
Capitalize The First Letter Of Each Word In Javascript
Follow for More Info βΒ https://instagram.com/developerhelps?igshid=MzRlODBiNWFlZA==