Convert the First Letter of a String into UpperCase in JavaScript

This is a preview lesson
Register or sign in to take this lesson.
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() and slice().
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.