Codectionary / Developer documentation / JavaScript

toUpperCase() and toLowerCase()

toUpperCase() and toLowerCase() return a new string with all characters converted to the respective case. They are frequently used for case-insensitive comparisons, since converting both sides to the same case avoids inconsistent casing causing a false mismatch.

Syntax

str.toUpperCase()
str.toLowerCase()

Examples

Basic Case Conversion

Converting a string's case.

const text = "Hello World";

console.log(text.toUpperCase()); // "HELLO WORLD"
console.log(text.toLowerCase()); // "hello world"

Case-Insensitive Comparison

A very common real-world use: comparing strings regardless of case.

function isSameWord(a, b) {
  return a.toLowerCase() === b.toLowerCase();
}

console.log(isSameWord("Hello", "hello")); // true
console.log(isSameWord("JavaScript", "javascript")); // true

// Also useful for case-insensitive search
const items = ["Apple", "Banana", "Cherry"];
const search = "banana";
const found = items.find(item => item.toLowerCase() === search.toLowerCase());
console.log(found); // "Banana"

Best practices

  • Convert both sides to the same case before comparing user input against known values, to make comparisons case-insensitive
  • Use toLocaleUpperCase()/toLocaleLowerCase() instead when working with locale-specific characters, like Turkish "İ"/"i"
  • Remember these return new strings - the original is never modified
  • Store data in a consistent case (like all lowercase) if you frequently need case-insensitive lookups, rather than converting on every comparison

At a glance

Purpose
Application logic and interaction
File extension
.js · .mjs
Runs in
Browsers and JavaScript runtimes
Usually used with
HTML, CSS and Web APIs

Specifications & further reading

Related JavaScript documentation