Sitemap
The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

How to Make the First Letter of a String Uppercase in JavaScript

A solution to the popular interview question

2 min readApr 26, 2020

--

In this article we are going to check a few techniques how to make the first letter of a string Uppercase. This may be a popular interview question so let’s deep dive into many ways of achieving the same result.

First, let’s look at a few test strings together with expected results:

'here is a string'   ==> 'Here is a string'
'the New York Times' ==> 'The New York Times'
'...continued' ==> '...continued'

Simple yet working

One way to uppercase the first letter is basically to… uppercase the first letter of a string:

const s = 'here is a string';
const result = s[0].toUpperCase() + s.substring(1);

In this code example, we grabbed the first character s[0] of the string and used toUpperCase() method. Next, we added + the rest of the string starting from second character (index: 1) with substring(1) method.

Basically, here we could stop as we got the expected result. It’s worth noting that instead of using s[0] we could use s.charAt(0) method, which will give exactly the same result. Additionally, we can substitute substring with slice method, which again will give…

--

--

The Startup
The Startup

Published in The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

Arek Jaworski
Arek Jaworski

Written by Arek Jaworski

PhD Student at Queen's University Belfast. Former Software Architect — AWS/Node.JS/JavaScript Contractor and Tutor. Combing industry experience with research!

Responses (1)