• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Easy Case Converter Logo 60

EasyCaseConverter

Online Case Converter Tools

  • Home
  • Blog
  • About Us
  • Contact Us
  • Privacy Policy

Change Case to Lowercase and Uppercase in Javascript

August 10, 2020 by adminecc Leave a Comment

This article explains how to change case to lowercase and uppercase in Javascript.

Javascript has two methods to change a string to lowercase and uppercase, called toLowerCase() and toUpperCase(), respectively.

The toLowerCase()

The toLowerCase() is a built-in method that returns the calling string value converted to lowercase.

The following Javascript code is an example of the use of toLowerCase() method:

<!DOCTYPE html>
<html>
<head>
   <title>toLowerCase Javascript</title>
   <script>
      function convertStr(){
         var str = "UPPERCASE";
         var res = str.toLowerCase();
         document.getElementById("result").innerHTML = res
      }
   </script>
</head>
<body>
   <button onclick="convertStr()"> Convert to Lower Case </button>
   <p id="result"></p>
</body>
</html>

Output:

lowercase in Javascript

The toUpperCase()

The toUpperCase() is a built-in method that returns the calling string value converted to uppercase.

The following Javascript code is an example of the use of toUpperCase() method:

<!DOCTYPE html>
<html>
<head>
   <title>toLowerCase Javascript</title>
   <script>
      function convertStr(){
         var str = "lowercase";
         var res = str.toUpperCase();
         document.getElementById("result").innerHTML = res
      }
   </script>
</head>
<body>
   <button onclick="convertStr()"> Convert to Upper Case </button>
   <p id="result"></p>
</body>
</html>

Output:

uppercase in Javascript

How to Uppercase the First Letter of a String in Javascript

Method toUpperCase() will convert the whole characters in a string to uppercase.

Sometimes we only want to uppercase only the first letter of a string.

Uppercase Only The First Letter of a String

To uppercase only the first letter of a string in Javascript, we could use toUpperCase() and slice() method as follows:

<!DOCTYPE html>
<html>
<head>
   <title>toLowerCase Javascript</title>
   <script>
      function CapsFirstLetter(){
         var str = "how to capitalize only the first letter";
         var res = str[0].toUpperCase() + str.slice(1); 
         document.getElementById("result").innerHTML = res
      }
   </script>
</head>
<body>
   <button onclick="CapsFirstLetter()"> Capitalize First Letter </button>
   <p id="result"></p>
</body>
</html>

Output:

Capitalize Only The First Letter

The str[0].toUpperCase() will uppercase the first letter of the word “how”, which is “h” letter in the “how to capitalize only the first letter” string.

And str.slice(1) is a method to slice a string according to the passed parameters.

slice(start, end) has two parameters. The first parameter (start) is required. It specifies the position where to begin the slicing.

The second parameter (end) is an optional parameter. It specifies the position to end the slicing. If this parameter is omitted, it selects all characters from the start.

The string index starts at position 0.

So str.slice(1) means slicing the string starting from index 1 to last index since the second parameter is omitted.

Related posts:

Change Case to Lowercase and Uppercase in Python Easy Case Converter - Lowercase UppercaseUppercase to Lowercase Converter

Powered by YARPP.

Filed Under: article

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

Easy Case Converter Tool
Easy Case Converter - Free Case Converter Tools

Easy Case Converter, Free Online Case Converter Tools

Easy Case Converter is a free online converter case tools to convert your text into any formatted case.

Change Case to Lowercase and Uppercase in Javascript

This article explains how to change case to lowercase and uppercase in Javascript. Javascript has two methods to change a string to lowercase and uppercase, called toLowerCase() and toUpperCase(), respectively. The toLowerCase() The toLowerCase() is a built-in method that returns the calling string value converted to lowercase. The following Javascript code is an example of […]

Change Case to Lowercase and Uppercase in Python

This article explains how to change case to lowercase and uppercase in python. Python has a built-in method related to string handling. You are not only can check whether the string is lowercase or uppercase, but you also can change a string to one of these case characteristics. In this article, we will explain four […]

How to Change Case in Excel?

In the previous article, we discuss how to change Case in Word. But this article, we will explain how to Change Case in Excel. For your information, in this article, we use Microsoft Excel from Office 356. Different from Microsoft Word, to change sentence case, we can do easily by using the Font Options or […]

Copyright © 2019 EasyCaseConverter.com