How to uppercase first letter of a string in JavaScript

January 11, 2020 No comments JavaScript Snippets Examples QA Capitalize Uppercase String

1. Introduction

In this short article, we are going to show a way to capitalize the first character of the string in JavaScript.

2. Capitalize the first character of a string

Although we don't have a dedicated method for this operation in JS, we can achieve it easily by rewriting the string with uppercased first character connected with the rest of the string. Take a look at the following example:


const name = "john";
const capitalized = name.charAt(0).toUpperCase() + name.slice(1);

console.log(capitalized); // John


3. Conclusion

In this article, we presented a method to rewrite a string with the capitalized first character. We used JavaScript toUpperCase() function that works on characters to change the first char of the string. The rest characters are simply merged to that uppercased first one.

{{ message }}

{{ 'Comments are closed.' | trans }}