Home  >  Article  >  Web Front-end  >  How to convert strings to uppercase and lowercase using JavaScript (detailed graphic and text explanation)

How to convert strings to uppercase and lowercase using JavaScript (detailed graphic and text explanation)

yulia
yuliaOriginal
2018-10-29 10:57:3147625browse

JavaScript is a very important part of front-end development. It can realize the functions of the page. Friends who are learning JavaScript, do you know how to use JS to convert a lowercase string to uppercase? This article will tell you about the method of JS string case conversion. It has certain reference value. Interested friends can take a look.

1. toLowerCase converts the string to lowercase

The toLowerCase() method in JavaScript can convert the string to lowercase, but it does not work with non-alphabetic characters There is no need to worry about compatibility when using it, as all major browsers support the toLowerCase() method.

Example: Use toLowerCase to convert a string to lowercase

Description: Define a mixed-case string a, use toLowerCase to convert it to lowercase, assign the return value to b, and use document.write displays the values ​​​​of a and b on the page to see what the difference is. The code is as follows:

<script type="text/javascript">
  var a="WATERmelon"
  var b=a.toLowerCase()
  document.write(a+&#39;<br>&#39;+b)
 </script>

Rendering:

How to convert strings to uppercase and lowercase using JavaScript (detailed graphic and text explanation)

From the picture It can be seen that after conversion, the strings become lowercase, and toLowerCase will not change the original string.

2. toUpperCase Converts the string to uppercase

The toUpperCase() method can convert the string to uppercase. Like toLowerCase, it will not has an impact, and all major browsers support the toUpperCase method.

Example: Use toUpperCase to convert a string to uppercase

Description: First define a mixed-case string a, use toUpperCase to convert the string to uppercase, and then assign the value to b , use document.write to display the values ​​​​of a and b on the page, and see what the difference is before and after the conversion. The code is as follows:

<script type="text/javascript">
  var a="absolute"
  var b=a.toUpperCase()
  document.write(a+&#39;<br>&#39;+b)
 </script>

Rendering:

How to convert strings to uppercase and lowercase using JavaScript (detailed graphic and text explanation)

As can be seen from the picture, the strings become uppercase after conversion, and oUpperCase will not change the original string.

The above introduces how to use JavaScript to convert uppercase and lowercase strings. If you want to convert a string to lowercase, use the toLowerCase() method. If you want to convert a string to uppercase, use toUpperCase () method, the explanation is more detailed and easy to understand. Friends who have never been exposed to string case conversion before must practice it by yourself to see if your code can achieve the effect of case conversion. I hope this article will be useful to you. Helped!

【Related Tutorial Recommendations】

1. JavaScript Video Tutorial
2. JavaScript Chinese Reference Manual
3. bootstrap tutorial

The above is the detailed content of How to convert strings to uppercase and lowercase using JavaScript (detailed graphic and text explanation). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn