JavaScript: Check if First Letter of a String Is Upper Case - Stack Abuse

JavaScript: Check if First Letter of a String Is Upper Case

Introduction

In English, it's essential to capitalize the first letter of a sentence. JavaScript has built-in methods to help us with this.

In this article we will look at three different ways to check if the first letter of a string is upper case and how to capitalize it.

Check if First Letter Is Upper Case in JavaScript

We can check if the first of a letter a string is upper case in a few ways. Let's take a look at some popular ones.

toUpperCase()

This is a built-in string method that returns the invoked string with only upper case characters:

function startsWithCapital(word){
    return word.charAt(0) === word.charAt(0).toUpperCase()
}

console.log(startsWithCapital("Hello")) // true
console.log(startsWithCapital("hello")) // false

Here, we're making a single character string consisting of only the first letter/character of the provided string and comparing it to its upper case version. If they match, the original letter is uppercase.

Note: string.charAt(index) is preferred over string[index] (bracket notation). This is because "".charAt(0) returns an empty string whereas ""[0] returns undefined.

This removes the need to check if a variable is undefined, and avoids exceptional states in your code.

charCodeAt()

Every character of a string is represented by a unique number using UTF-16 character encoding. For English capital letters: A = 65 and Z = 90. If the first letter's character code falls in this range, we know it's upper case:

function startsWithCapital(word){
    return word.charCodeAt(0) >= 65 && word.charCodeAt(0) <= 90
}

console.log(startsWithCapital("This is working!")) // true
console.log(startsWithCapital("checking!")) // false

test()

Alternatively, we can check for a match between a regular expression and a specified string. This is easily achieved with the test() function.

Let's take a look at an example:

function startsWithCapital(word){
    return /[A-Z]/.test(word.charAt(0))
}

console.log(startsWithCapital("Capital letters")) // true
console.log(startsWithCapital("is this true?")) // false

Here, /[A-Z]/ represents all capital English letters (A to Z). We are checking whether the first letter of the string matches any of them.

Capitalizing the First Letter

If we found out that the first letter of the string is lower case, and if we want to capitalize it, we can do that using following method:

function capitalizeFirstLetter(word){
    return word.charAt(0).toUpperCase() + word.slice(1)
}

console.log(capitalize("hello world!")) // Hello world

Here, we take the first character and convert it to upper case. Then, we concatenate the rest of the string to that capitalized letter. This is done via the slice() function, where we've specified the starting index as 1. Since it's 0-based, we've skipped the first letter.

Conclusion

In this article, we have looked at a few ways to check if the first letter of a string is upper case and how to capitalize the first letter of a string.

Last Updated: December 30th, 2020

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Abhilash KakumanuAuthor

Hey, I am a full-stack web developer located in India. I am a curious person who is always trying to wrap my head around new technologies. In my free time, I read novels and play with my dog!

Want a remote job?

    Prepping for an interview?

    • Improve your skills by solving one coding problem every day
    • Get the solutions the next morning via email
    • Practice on actual problems asked by top companies, like:
     
     
     

    Getting Started with AWS in Node.js

    Build the foundation you'll need to provision, deploy, and run Node.js applications in the AWS cloud. Learn Lambda, EC2, S3, SQS, and more!

    © 2013-2021 Stack Abuse. All rights reserved.