This tutorial shows how to convert the first letter of a string into uppercase using JavaScript. i.e. "apple" to "Apple". For this, we use in-built JavaScript functions:charAt()
,toUpperCase()
andslice()
. syntax
str.charAt(0).toUpperCase() + str.slice(1)
Example: Converting First Letter of a String into UpperCase
//program to convert the first letter of a string into uppercase
let str= "apple";
let conversion = str.charAt(0).toUpperCase() + str.slice(1)
console.log(conversion)
Output
Apple
The charAt(0)
function selects the first letter of a sting, toUppercase()
function changes that letter to uppercase and the slice(1)
function returns the letters starting from index 1 i.e. the remaining letters.