Ads by ProfitSence
Close

Capitalize First letter of a String in JavaScript

Last Updated on Wednesday 5th Oct 2022

JavaScript Capitalize First Letter

How to Capitalize the First Letter in a string, in other words, make an upperCase letter with Javascript

JS Capitalize

  • Let's create a string
  • Create another variable. So that takes you the original variable
  • Use charAt(0) - that's going to select the first letter
			
					let stringWord = 'generalword'
let firstLetter = stringWord.charAt(0);
console.log(firstLetter); // g

			
	

Use the slice method and set it to slice(1) will skip the first letter and select the rest.

Capitalize JavaScript

			
					let slice = stringWord.slice(1);
console.log(slice); // eneralword

			
	

You need to capitalize the first letter. We are going to use toUpperCase() method to get an upperCase letter

			
					let capitalize = stringWord.charAt(0).toUpperCase();
console.log(capitalize); // G

			
	

Let's Combine all & Create a Function for more convenience to use in the near future

JavaScript UpperCase First Letter

			
					function capitalizeFirst(str){
  return str.charAt(0).toUpperCase() + str.slice(1);
}
console.log(capitalizeFirst("hey")) // Hey

console.log(capitalizeFirst("aBcDEFGHIJK")) // ABcDEFGHIJK

			
	

Capitalize First Letter JavaScript

			
					function capitalizeFirstLetter(str) {
  return str[0].toUpperCase() + str.slice(1);
}

console.log(capitalizeFirstLetter("capitalize Me")) // Capitalize Me