Skip to content
the poor coder

string uppercase

Poor Man
Published
Reading time
2 min read
On this page

Understanding String Uppercase#

String uppercase is a method used to convert all characters in a given string to uppercase letters. This can be useful in various scenarios where you need to manipulate strings or compare them in a case-insensitive manner.

Using the toUpperCase() Method#

The most common way to perform string uppercase in JavaScript is by using the toUpperCase() method. This method belongs to the String object and converts all lowercase characters in a string to uppercase.

Text
let str = "hello world";
let upperStr = str.toUpperCase();
console.log(upperStr);
// Output: "HELLO WORLD"

Using CSS Text Transform Property#

You can also convert strings to uppercase using CSS text-transform property. This can be useful when you want to style text on a webpage.

Text
.uppercase {
  text-transform: uppercase;
}

Using Regular Expressions#

Another way to perform string uppercase is by using regular expressions. This method involves replacing lowercase characters with their uppercase counterparts.

Text
let str = "hello world";
let upperStr = str.replace(/[a-z]/g, function(char) {
  return char.toUpperCase();
});
console.log(upperStr);
// Output: "HELLO WORLD"

Using ASCII Codes#

You can also perform string uppercase by converting lowercase characters to their ASCII codes and subtracting 32 from them. This will give you the corresponding uppercase ASCII code, which can then be converted back to a character using the String.fromCharCode() method.

Text
let str = "hello world";
let upperStr = "";
for(let i=0; i<str.length; i++) {
  let charCode = str.charCodeAt(i);
  if(charCode>=97 && charCode<=122) { // Check if lowercase letter
    charCode -= 32; // Convert to uppercase ASCII code
  }
  upperStr += String.fromCharCode(charCode); // Convert back to character
}
console.log(upperStr);
// Output: "HELLO WORLD"

These are some of the ways to perform string uppercase in JavaScript. Choose the method that suits your needs and use it to manipulate strings in your code.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC