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:
Learn JavaScript on Mimo
- 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().
JavaScript
const text = "JavaScript";
const upper = text.toUpperCase();
const lower = 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:
JavaScript
input.toLowerCase() === stored.toLowerCase();
Common capitalization pattern for a single word:
JavaScript
text.charAt(0).toUpperCase() + text.slice(1).toLowerCase();
Title case pattern for multiple words:
JavaScript
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()
JavaScript
const text = "istanbul";
const upper = 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
JavaScript
const input = "Admin";
const stored = "admin";
const match =
input.toLowerCase() === stored.toLowerCase();
console.log(match);
Normalize both sides before comparing.
Example 2: Capitalize a single word
JavaScript
const text = "javascript";
const capitalized =
text.charAt(0).toUpperCase() +
text.slice(1).toLowerCase();
console.log(capitalized);
Example 3: Convert a sentence to title case
JavaScript
const sentence = "learn javascript the right way";
const titleCase = sentence
.split(" ")
.map(word =>
word.charAt(0).toUpperCase() +
word.slice(1).toLowerCase()
)
.join(" ");
console.log(titleCase);
Example 4: Clean inconsistent data
JavaScript
const names = ["ALICE", "bob", "ChArLiE"];
const normalized = 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:
JavaScript
let text = "hello";
text.toUpperCase();
console.log(text);
Why it breaks:
String methods return a new string. They do not modify the original value.
Correct approach:
JavaScript
text = text.toUpperCase();
console.log(text);
Mistake 2: Comparing without normalizing
What you might do:
JavaScript
const input = "Admin";
const stored = "admin";
console.log(input === stored);
Why it breaks:
String comparison is case-sensitive.
Correct approach:
JavaScript
console.log(
input.toLowerCase() === stored.toLowerCase()
);
Mistake 3: Accessing the first character of an empty string
What you might do:
JavaScript
const text = "";
const result =
text.charAt(0).toUpperCase() +
text.slice(1);
Why it breaks:
Empty strings do not have a character at index 0.
Correct approach:
JavaScript
const result =
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()andtoLowerCase()for basic case changes. - Use
toLocaleUpperCase()andtoLocaleLowerCase()for language-specific rules. - Assign results because strings are immutable.
- Normalize case before comparing values.
- Combine string methods when you need capitalization patterns.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot