How to Convert a String to Uppercase and Lowercase in JavaScript
When you work with strings in any programming language, you'll often need to change the case of the string.
That can be either turning the entire string to uppercase or lowercase.
Thankfully, JavaScript makes it easy to transform your string between these two cases.
In this post, we'll learn how to convert a string to uppercase or lowercase in JavaScript.
Converting a string to uppercase
Converting a string to uppercase is very simple.
Just use the built-in toUpperCase()
method on the string that you want to transform.
The method will return a new string with all of the characters in the original string in uppercase.
Let's first start with a sample string:
JAVASCRIPTconst string = "This is a string.";
const uppercasedString = string.toUpperCase();
console.log(uppercasedString);
BASHTHIS IS A STRING.
Converting a string to lowercase
You can similarly do the reverse and lowercase strings by using the toLowerCase()
method.
This method will take a string and convert all uppercase characters to lowercase and return the new string.
JAVASCRIPTconst string = "THIS IS A STRING.";
const lowercasedString = string.toLowerCase();
console.log(lowercasedString);
BASHthis is a string.
Conclusion
In this post, we learned how to convert a string to uppercase or lowercase in JavaScript.
Simply make use of the built-in toUpperCase()
and toLowerCase()
methods on strings.
Thanks for reading and happy coding!
- How to Install Node on Windows, macOS and Linux
- Getting Started with Svelte
- Create an RSS Reader in Node
- Best Visual Studio Code Extensions for 2022
- How to deploy a PHP app using Docker
- How to deploy a Deno app using Docker
- Getting Started with Deno
- Getting Started with Sass
- How to Scrape the Web using Node.js and Puppeteer
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Learn how to build a Slack Bot using Node.js
- Using Push.js to Display Web Browser Notifications