A Common Problem

As a Javascript Developer, you will often have to manipulate strings in different ways (removing, adding, testing). However oftentimes we just have to adjust the case of a letter . Often time’s we may get a paragraph. First we’ll get into how uppercase the first letter of a word.

Then once were comfortable we will return to our scenario of the lowercase sentence.

Let’s take a string

Well let’s think we want to just uppercase the “e” in elephant and return the rest of the string onwards

JS already comes with two functions that help us let’s get into the code

A Shorter Way

If we don’t care about supporting older version of IE we can use s[0].toUpperCase() instead. To get the first letter

Also since were just using boolean true/false logic in capitalize we can leverage the ternary operator

We got the same result with shorter and more concise code. Without any major hits to readability.

Further Reading

Now that you’re comfortable with capitalizing the first letter of a single word. Are you ready to tackle whole sentences in our Capitalize First Word of Sentence in JS article coming soon

Take Your Javascript Skills to The Next Level

Here at CodingPhase we offer a comprehensive Future-Proof Javascript and ESNEXT – ES6, ES7, ES8 which will cover all the modern Javascript. Hiring managers are looking for people who are up to date with the latest language specifications

Sign up for a monthly subscription for $20/m and also get access to our  Object Oriented Programming with Javascript  and our Learn React by Building a Craigslist Clone