TypeScript to Lowercase: Convert a String with toLowerCase()

One customer types Jordan.Miller@Gmail.COM into a TypeScript signup form, and the next types it all lowercase. To convert a string to lowercase in TypeScript, call toLowerCase(): it returns a new lowercase string and leaves the original alone.

const normalized = signupEmail.toLowerCase();   // "jordan.miller@gmail.com"

In this tutorial, I’ll cover how to convert a string to lowercase in TypeScript, then compare and search without case, handle undefined, fix locale quirks and use the Lowercase<T> type.

Every output below is real: TypeScript 7.0.2 and Node.js v22.22.2, strict mode, run in VS Code.

TypeScript toLowerCase() Syntax and Return Value

The syntax is str.toLowerCase(). It takes no arguments and returns a string. Here it is on a signup email for a Portland bike shop, plus a coupon code with stray spaces:

const signupEmail = "Jordan.Miller@Gmail.COM";

const normalized = signupEmail.toLowerCase();
console.log(normalized);
console.log(signupEmail);           // original is unchanged

const coupon = "  SPRING-Sale26 ";
console.log(`[${coupon.trim().toLowerCase()}]`);
console.log(typeof normalized, normalized.length);

Output:

jordan.miller@gmail.com
Jordan.Miller@Gmail.COM
[spring-sale26]
string 23
VS Code running TypeScript toLowerCase() on a signup email and a trimmed coupon code
The email prints in lowercase while the original variable keeps its capitals.

The second line proves strings are immutable: signupEmail still has its capitals, so always store the result. Chain trim() first when input comes from a form.

Python does the same job with lower(); see how to convert a string to lowercase in Python if you work in both languages.

Compare Two Strings Without Case Sensitivity

Same customer email, saved in lowercase and typed with mixed case. Lowercase both sides before ===, or let localeCompare() ignore case:

const saved: string = "jordan.miller@gmail.com";
const typed: string = "Jordan.Miller@GMAIL.com";

console.log("===:", saved === typed);
console.log("toLowerCase:", saved.toLowerCase() === typed.toLowerCase());

const diff = saved.localeCompare(typed, "en-US", { sensitivity: "base" });
console.log("localeCompare:", diff === 0);

const cities = ["austin", "Boston", "denver", "Chicago"];
console.log([...cities].sort());
console.log([...cities].sort((a, b) =>
  a.toLowerCase().localeCompare(b.toLowerCase())));

Output:

===: false
toLowerCase: true
localeCompare: true
[ 'Boston', 'Chicago', 'austin', 'denver' ]
[ 'austin', 'Boston', 'Chicago', 'denver' ]
VS Code running a TypeScript case-insensitive string comparison with toLowerCase and localeCompare, plus a sorted city list
Plain === is false, both case-insensitive checks are true, and the default sort puts capitals first.

I typed both as string on purpose. With two const literals, TypeScript rejects the === line with TS2367, because the literal types can never match.

The default sort() orders by UTF-16 code units, so Boston and Chicago land before austin. Lowercasing inside the comparer fixes the order.

For every other way to match text, including sensitivity options, read my article on how to compare strings in TypeScript.

Case-Insensitive Search in a TypeScript Array

The bike shop’s product search should find HELMET Light Kit when someone types helmet. Lowercase the query once and each name inside filter():

type Product = { name: string; price: number };

const products: Product[] = [
  { name: "Trail Helmet", price: 89.99 },
  { name: "HELMET Light Kit", price: 24.5 },
  { name: "Road Bike Tube", price: 8.75 },
];

function search(items: Product[], query: string): Product[] {
  const q = query.trim().toLowerCase();
  return items.filter((p) => p.name.toLowerCase().includes(q));
}

for (const p of search(products, " Helmet")) {
  console.log(`${p.name} - $${p.price.toFixed(2)}`);
}
const exact = products.filter((p) => p.name.includes("helmet"));
console.log("case-sensitive hits:", exact.length);

Output:

Trail Helmet - $89.99
HELMET Light Kit - $24.50
case-sensitive hits: 0
VS Code running a TypeScript case-insensitive product search with toLowerCase and includes
Both helmet products match, while the case-sensitive filter finds none.

Two products come back, with prices in USD. The case-sensitive version found 0, which is the bug users report as “search is broken.” More patterns like this are in filtering arrays in TypeScript.

The same function works behind a React search box; call it on every keystroke with the input’s value.

Why Does toLowerCase() Fail on undefined in TypeScript?

Optional fields are typed string | undefined, and strict TypeScript won’t let you call a string method on that. A guest checkout with no email:

type Signup = { name: string; email?: string };

const guest: Signup = { name: "Ava Thompson" };

const email = guest.email.toLowerCase();

tsc output:

lower_undefined.ts(5,15): error TS18048: 'guest.email' is possibly 'undefined'.
VS Code showing TypeScript error TS18048 guest.email is possibly undefined when calling toLowerCase
The compiler flags guest.email before the code can run.

Use optional chaining with a fallback. Same signup type, now with one guest:

type Signup = { name: string; email?: string };

const signups: Signup[] = [
  { name: "Jordan Miller", email: "Jordan.Miller@Gmail.COM" },
  { name: "Ava Thompson" },
];

for (const s of signups) {
  const email = s.email?.toLowerCase() ?? "(no email)";
  console.log(s.name.padEnd(14), email);
}

Output:

Jordan Miller  jordan.miller@gmail.com
Ava Thompson   (no email)
VS Code running TypeScript optional chaining with toLowerCase on signups with and without an email
The signup with an email prints it in lowercase, and the guest gets the fallback text.

s.email?.toLowerCase() returns undefined instead of crashing, and ?? swaps in the fallback. Need to treat blank strings as missing too? See checking if a string is empty in TypeScript.

toLocaleLowerCase() for Turkish and Other Languages

toLowerCase() uses Unicode’s default rules, which are wrong for a few languages. Turkish has a dotted and a dotless I:

const city = "İSTANBUL";
console.log(city.toLowerCase(), city.toLowerCase().length);
console.log(city.toLocaleLowerCase("tr-TR"), city.length);

const word = "TITLE";
console.log(word.toLowerCase(), word.toLocaleLowerCase("tr-TR"));

console.log("STRASSE".toLowerCase(), "ÄÖÜ".toLowerCase());

Output:

i̇stanbul 9
istanbul 8
title tıtle
strasse äöü
VS Code comparing toLowerCase and toLocaleLowerCase with the tr-TR locale on Turkish city names
The default rules add an extra character; the Turkish locale gives the expected result.

Default lowercasing turns İ into two characters, so the length grows from 8 to 9. With "tr-TR", you get a clean istanbul, and TITLE becomes tıtle.

STRASSE lowercases to strasse, not straße, because lowercasing can’t restore a letter that uppercasing split. For English-only data, plain toLowerCase() is right.

Going the other way? Capitalizing the first letter in TypeScript covers toUpperCase().

The TypeScript Lowercase Type for String Literals

Lowercase<T> is a built-in TypeScript type that lowercases a string literal type at compile time. It never runs, so pair it with toLowerCase():

type HttpMethod = "GET" | "POST" | "DELETE";
type LowerMethod = Lowercase<HttpMethod>;   // "get" | "post" | "delete"

function toLower<T extends string>(value: T): Lowercase<T> {
  return value.toLowerCase() as Lowercase<T>;
}

const method = toLower("POST");           // type: "post"
const route: `/${LowerMethod}` = `/${method}`;
console.log(method, route);

const both: LowerMethod[] = (["GET", "DELETE"] as const).map(toLower);
console.log(both);

Output:

post /post
[ 'get', 'delete' ]
VS Code running a TypeScript generic toLower function that returns the Lowercase type of an HTTP method
The method and route print in lowercase, and the mapped array keeps the narrow type.

The as Lowercase<T> cast is needed because toLowerCase() returns plain string. Now assign the wrong values:

type LowerMethod = Lowercase<"GET" | "POST">;

const ok: LowerMethod = "get";
const bad: LowerMethod = "Get";

const input: string = "POST";
const fromUser: LowerMethod = input.toLowerCase();

tsc output:

lower_type_error.ts(4,7): error TS2820: Type '"Get"' is not assignable to type '"get" | "post"'. Did you mean '"get"'?
lower_type_error.ts(7,7): error TS2322: Type 'string' is not assignable to type '"get" | "post"'.
VS Code showing TypeScript errors TS2820 and TS2322 when assigning Get or a plain string to a Lowercase type
"Get" fails with a did-you-mean hint (TS2820), and a plain string fails even after toLowerCase() (TS2322).

User input stays a plain string, so check it against the allowed values first, the same way as converting a string to an enum in TypeScript.

Working with more TypeScript string code? Pick up from here:

Questions About Lowercase Strings in TypeScript

Does toLowerCase() change the original string in TypeScript?

No. Strings are immutable, so it returns a new string. Assign the result to a variable.

Is there a tolower() function in TypeScript?

No. The method is toLowerCase(), called on the string: name.toLowerCase().

What’s the difference between toLowerCase() and toLocaleLowerCase()?

toLocaleLowerCase() follows a locale’s rules, such as Turkish tr-TR. For English text, both return the same result.

How do I lowercase a string that might be undefined?

Use value?.toLowerCase() ?? "". It returns the fallback instead of throwing.

What is the Lowercase type in TypeScript?

A built-in type that lowercases a string literal type, so Lowercase<"GET"> is "get".

Where is String.prototype.toLowerCase() documented?