Home  >  Article  >  Web Front-end  >  How to implement case conversion in JavaScript

How to implement case conversion in JavaScript

不言
不言Original
2019-01-15 15:36:247703browse

The toUpperCase() method and toLowerCase() method in How to implement case conversion in JavaScript can be used to convert uppercase and lowercase strings, toUpperCase is used to convert lowercase letters to uppercase letters, and toLowerCase is used to convert uppercase letters to Lower case letters. Let’s take a look at the specific content of this article.

How to implement case conversion in JavaScript

First let’s take a look at How to use toUpperCase to convert to uppercase?

To use toUpperCase to convert a string to uppercase letters, you need to call the toUpperCase method from the string to be converted.

Return an uppercase string as the return value.

Let’s look at the following code.

var str = "php";
str = str.toUpperCase();
console.log(str);

Execution result: PHP

Through the above method, we can convert the string to uppercase

How to use toLowerCase conversion for lowercase?

To convert a string to lowercase using toLowerCase, call the toLowerCase method from the string you want to convert.

Return a lowercase string as the return value.

 var str = "PHP";
 str = str.toLowerCase(); 
 console.log(str);

Execution result: php

In this way, we can convert the string to lowercase.

Finally, let’s seeHow to compare uppercase and lowercase letters?

To compare the uppercase and lowercase letters of the two strings, compare them with the lowercase letters in toLowerCase.

var str1 = "Php";
var str2 = "php";
 
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
 
var result = str1 === str2;
 
console.log(result);

Execution result: true

In this way, we are able to compare strings after aligning them with lowercase letters.

The above is the detailed content of How to implement case conversion in JavaScript. 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