How to Change String Case in JavaScript

What you’ll build or solve

You’ll change the case of a string using native JavaScript methods.

When this approach works best

Changing string case works best when you:

  • Normalize user input before comparing values.
  • Format text for display, such as headings or labels.
  • Clean inconsistent data from external sources.
  • Prepare values for case-insensitive matching.

Avoid relying only on basic case conversion for complex language rules. Some languages require locale-aware handling.

Prerequisites

  • A browser or Node.js installed
  • Basic understanding of strings and variables

No additional setup is required.


Step-by-step instructions

Step 1: Convert to uppercase or lowercase

Use the built-in methods toUpperCase() and toLowerCase().

consttext="JavaScript";

constupper=text.toUpperCase();
constlower=text.toLowerCase();

console.log(upper);
console.log(lower);

These methods return a new string. The original value remains unchanged.

What to look for:

  • Strings are immutable. Assign the result to a variable if you want to keep it.
  • Convert both values before comparison:
input.toLowerCase()===stored.toLowerCase();

Common capitalization pattern for a single word:

text.charAt(0).toUpperCase()+text.slice(1).toLowerCase();

Title case pattern for multiple words:

sentence
.split(" ")
.map(word =>
word.charAt(0).toUpperCase()+
word.slice(1).toLowerCase()
  )
.join(" ");

Step 2: Use locale-aware methods for language-specific rules

For language-sensitive transformations, use:

  • toLocaleUpperCase()
  • toLocaleLowerCase()
consttext="istanbul";

constupper=text.toLocaleUpperCase("tr-TR");
console.log(upper);

Locale-aware methods apply language rules correctly. This matters for certain alphabets and character sets.

What to look for:

  • Pass a locale string when working with language-specific data.
  • If no locale is provided, the environment default is used.

Examples you can copy

Example 1: Case-insensitive login check

constinput="Admin";
conststored="admin";

constmatch=
input.toLowerCase()===stored.toLowerCase();

console.log(match);

Normalize both sides before comparing.


Example 2: Capitalize a single word

consttext="javascript";

constcapitalized=
text.charAt(0).toUpperCase()+
text.slice(1).toLowerCase();

console.log(capitalized);

Example 3: Convert a sentence to title case

constsentence="learn javascript the right way";

consttitleCase=sentence
.split(" ")
.map(word =>
word.charAt(0).toUpperCase()+
word.slice(1).toLowerCase()
  )
.join(" ");

console.log(titleCase);

Example 4: Clean inconsistent data

constnames= ["ALICE","bob","ChArLiE"];

constnormalized=names.map(name =>
name.toLowerCase()
);

console.log(normalized);

Common mistakes and how to fix them

Mistake 1: Expecting the original string to change

What you might do:

lettext="hello";
text.toUpperCase();
console.log(text);

Why it breaks:

String methods return a new string. They do not modify the original value.

Correct approach:

text=text.toUpperCase();
console.log(text);

Mistake 2: Comparing without normalizing

What you might do:

constinput="Admin";
conststored="admin";

console.log(input===stored);

Why it breaks:

String comparison is case-sensitive.

Correct approach:

console.log(
input.toLowerCase()===stored.toLowerCase()
);

Mistake 3: Accessing the first character of an empty string

What you might do:

consttext="";
constresult=
text.charAt(0).toUpperCase()+
text.slice(1);

Why it breaks:

Empty strings do not have a character at index 0.

Correct approach:

constresult=
text.length>0
?text.charAt(0).toUpperCase()+
text.slice(1)
:"";

Troubleshooting

  • If comparisons fail, convert both strings to the same case first.
  • If characters behave unexpectedly, try locale-aware methods.
  • If capitalization logic fails, check string length before accessing index 0.
  • If multi-word formatting looks wrong, confirm you split on the correct separator.

Quick recap

  • Use toUpperCase() and toLowerCase() for basic case changes.
  • Use toLocaleUpperCase() and toLocaleLowerCase() for language-specific rules.
  • Assign results because strings are immutable.
  • Normalize case before comparing values.
  • Combine string methods when you need capitalization patterns.