How to count capital letters and small letters in JavaScript

In this JavaScript tutorial, I will show you the best way to count capital letters in JavaScript and also count small letters in JavaScript. You can find different methods and ideas to count capital letters and small letters in JavaScript. But as this is CodeSpeedy, we always provide the simplest and easiest method to get the solution to any problem.
How to Create A Countdown Timer with Progress Bar in JavaScript

So in this post, we gonna learn the following things

  1. How to count capital letters in JavaScript ( Uppercase letters count )
  2. How to count small letters in JavaScript ( Lowercase letters count )

Count Capital Letters in JavaScript

The below Code will count Uppercase letters in JavaScript

<!DOCTYPE html>
<html>
<head>
  <title>Count capital letters in JavaScript</title>
</head>
<body>
    Enter some text: <br>
    <input type="text" id="textbox">
    <button onclick="count()">
  <br>
  
  <script type="text/javascript">
    function count(){
      var mytext= document.getElementById("textbox").value;
      var numUpper = (mytext.match(/[A-Z]/g) || []).length;
      alert(numUpper);
    }
  </script>

</body>
</html>

Here match() and regular expression is used to count capital letters.

How To Change Background Color Every Seconds in JavaScript

We have created a button and set an onclick function count().

In script tag, there is a count() function to take the value entered in the above input box. We have stored the value in a JavaScript variable.

var mytext= document.getElementById("textbox").value;

And this is the main line to count the uppercase letters with the help of regular expression.

var numUpper = (mytext.match(/[A-Z]/g) || []).length;

Live word counter from HTML textarea in JavaScript

How To Count The Number Of Characters Without Spaces in jQuery

Count Lowercase Letters in JavaScript ( Small letters )

The below Code will count small letters in JavaScript

<!DOCTYPE html>
<html>
<head>
  <title>Count small letters in JavaScript</title>
</head>
<body>
    Enter some text: <br>
    <input type="text" id="textbox">
    <button onclick="count()">
  <br>
  
  <script type="text/javascript">
    function count(){
      var mytext= document.getElementById("textbox").value;
      var numUpper = (mytext.match(/[a-z]/g) || []).length;
      alert(numUpper);
    }
  </script>

</body>
</html>

JavaScript function:

function count(){
	var mytext= "anY tEXT";
	var numUpper = (mytext.match(/[a-z]/g) || []).length;
	alert(numUpper);
	}

You may also read,

How to find the Keycode in JavaScript ( Core JavaScript no jQuery )

Detect if particular word typed in textarea in JavaScript

Leave a Reply

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