Categories
JavaScript Answers

How to check if first letter of word is a capital letter with JavaScript?

Spread the love

Sometimes, we want to check if first letter of word is a capital letter with JavaScript.

In this article, we’ll look at how to check if first letter of word is a capital letter with JavaScript.

How to check if first letter of word is a capital letter with JavaScript?

To check if first letter of word is a capital letter with JavaScript, we use a regex.

For instance, we write

const word = "Someword";
console.log(/^[A-Z]/.test(word));

to call /^[A-Z]/.test with word to check if the first letter of word starts with a capital letter.

^ is the symbol for the beginning of the string.

[A-Z] is a pattern that matches upper case letters.

Conclusion

To check if first letter of word is a capital letter with JavaScript, we use a regex.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *