Sling Academy
Home/JavaScript/JavaScript: Convert a String to Upper or Lower Case

JavaScript: Convert a String to Upper or Lower Case

Last updated: February 27, 2023

This concise article shows you how to convert a string to upper or lower case in JavaScript, from theory to practical examples.

Overview

To convert a given string to uppercase, you can use the toUpperCase() method. To convert a string to lowercase, you can use the toLowerCase() method. Both of these methods return a new string with the case converted as desired (the original string is intact).

Examples

Converting a string to uppercase

The code:

const name = "welcome to sling academy";
const upperCaseName = name.toUpperCase();
console.log(upperCaseName);

Output:

WELCOME TO SLING ACADEMY

Converting a string to lowercase

The code:

const message = "THE DRAGON IS FLYING OVER THE MOUNTAIN";
const lowerCaseMessage = message.toLowerCase();
console.log(lowerCaseMessage); 

Output:

the dragon is flying over the mountain

Using a user input

The code:

const userInput = prompt("Enter a string:");
const upperCaseInput = userInput.toUpperCase();
console.log(upperCaseInput); 

Run this code with a web browser and see how it works.

Using the ternary operator

The code:

const greeting = 'one more time, bad moon rising';
const isUpperCase = true;

const formattedGreeting = isUpperCase
  ? greeting.toUpperCase()
  : greeting.toLowerCase();
console.log(formattedGreeting);

Output:

ONE MORE TIME, BAD MOON RISING

Next Article: 3 Ways to Reverse a String in JavaScript

Previous Article: JavaScript: Regular Expressions Cheat Sheet

Series: JavaScript Strings

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration