String .toUpperCase() / toLowerCase()
| Since: | ES1(ECMAScript 1997) |
|---|
Converts all alphabetic characters in a string to uppercase or lowercase. The original string is not modified.
Syntax
// Converts all alphabetic characters to uppercase. string.toUpperCase() // Converts all alphabetic characters to lowercase. string.toLowerCase()
Methods
| Method | Description |
|---|---|
| toUpperCase() | Returns a new string with all alphabetic characters converted to uppercase. |
| toLowerCase() | Returns a new string with all alphabetic characters converted to lowercase. |
Sample Code
sample_toUpperCase.js
var str = "Hello World";
// Converts to uppercase.
console.log(str.toUpperCase()); // Outputs "HELLO WORLD".
// Converts to lowercase.
console.log(str.toLowerCase()); // Outputs "hello world".
// The original string is not modified.
console.log(str); // Outputs "Hello World" unchanged.
// Useful for case-insensitive comparisons.
var input = "JavaScript";
var target = "javascript";
console.log(input.toLowerCase() === target.toLowerCase()); // Outputs "true".
// Non-alphabetic characters such as digits and spaces are left as-is.
var mixed = "Hello world 123";
console.log(mixed.toUpperCase()); // Outputs "HELLO WORLD 123".
// Case-insensitive search filtering.
var members = ["Okabe Rintaro", "Makise Kurisu", "Shiina Mayuri", "Hashida Itaru"];
var query = "kurisu";
var i;
for (i = 0; i < members.length; i++) {
if (members[i].toLowerCase().indexOf(query.toLowerCase()) !== -1) {
console.log(members[i] + " matched"); // Detects a match regardless of letter case.
}
}
// URL slug generation: Convert to lowercase and replace spaces with hyphens.
var title = "Future Gadget Lab";
var slug = title.toLowerCase().replaceAll(" ", "-");
console.log(slug); // Outputs "future-gadget-lab".
HELLO WORLD hello world Hello World true HELLO WORLD 123 Makise Kurisu matched future-gadget-lab
Overview
string.toUpperCase() and string.toLowerCase() are methods that convert the alphabetic characters in a string to uppercase or lowercase. Non-alphabetic characters such as digits and symbols are unaffected and remain unchanged.
The most common use case is case-insensitive string comparison. When searching or matching user input, convert both strings to lowercase before comparing. Because string.indexOf() and string.startsWith() are case-sensitive, these methods are often used together with toLowerCase().
Both methods leave the original string unchanged and return a new string. To use the result, assign it to a variable or use it directly.
Browser Compatibility
2 or earlier ×
2 or earlier ×
Android Browser
37+ ○
4 or earlier ×
Chrome Android
36+ ○
17 or earlier ×
Firefox Android
79+ ○
3 or earlier ×If you find any errors or copyright issues, please contact us.