How to Uppercase Strings in JavaScript

Sep 17, 2021

The built-in way of capitalizing strings in JavaScript is using the toUpperCase() function, which capitalizes the whole string.

let string = ''
string.toUpperCase(); // MASTERINGJS.IO

Capitalizing the First Letter

To capitalize the first letter, the code gets much busier. Using a combination of toUpperCase(), charAt(), and slice(), you can capitalize the first letter of a word.

const str = 'captain Picard';

const caps = str.charAt(0).toUpperCase() + str.slice(1);
caps; // 'Captain Picard'

To capitalize the first letter of every word in a string, you must use a combination of join(), map(), split(), as well as the steps used in the previous example.

const str = 'captain picard';

function capitalize(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);

const caps = str.split(' ').map(capitalize).join(' ');
caps; // 'Captain Picard'

Locale Capitalization

JavaScript uses different capitalization rules based on the locale of your system. In most cases, the locale doesn't matter for capitalization. But there are some edge cases, like the famous dotless I in Turkish and some other Turkic languages. If these edge cases are important for your app, you can use toLocaleUpperCase() and specify the locale you want to use.

// In most cases, capitalization works correctly regardless of locale
'cafetería'.toLocaleUpperCase('en-US'); // CAFETERÍA
'cafetería'.toLocaleUpperCase('es-ES'); // CAFETERÍA

// But there are edge cases, like dotted I in some Turkic languages
'i'.toLocaleUpperCase('en-US'); // I
'i'.toLocaleUpperCase('tr-tr'); // İ

In general, we recommend using toUpperCase() and not worrying too much about locale.

Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Fundamentals Tutorials