uppercase
javascript

How do I make the first letter of a string uppercase in JavaScript?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

The usual JavaScript solution is to uppercase the first character and append the rest of the string unchanged. That sounds trivial, but good code should still decide how to handle empty strings, leading whitespace, and whether the goal is true title-casing or only changing the first code unit.

Use charAt, toUpperCase, and slice

For ordinary strings, the standard approach is concise and readable.

javascript
1function capitalizeFirstLetter(value) {
2  if (!value) {
3    return value;
4  }
5
6  return value.charAt(0).toUpperCase() + value.slice(1);
7}
8
9console.log(capitalizeFirstLetter("hello"));
10console.log(capitalizeFirstLetter("world"));

This keeps everything after the first character exactly as it was.

Handle empty strings explicitly

Without the initial guard, the logic still works for "" in many cases, but the explicit check makes intent clear and avoids unnecessary method calls.

javascript
1function capitalizeFirstLetter(value) {
2  if (value.length === 0) {
3    return value;
4  }
5
6  return value[0].toUpperCase() + value.slice(1);
7}

That version is also fine for common ASCII-oriented code.

Decide what should happen with whitespace

If the input may start with spaces, uppercasing the first character technically changes nothing because the first character is whitespace. If you actually want the first non-space letter, trim or search for it deliberately.

javascript
1function capitalizeFirstNonSpace(value) {
2  const match = value.match(/\S/);
3  if (!match) {
4    return value;
5  }
6
7  const index = match.index;
8  return value.slice(0, index) + value[index].toUpperCase() + value.slice(index + 1);
9}
10
11console.log(capitalizeFirstNonSpace("  hello"));

This is a different requirement from the simpler "uppercase the first character" version, so keep the distinction clear.

Do not confuse capitalization with title case

If the string is "hello world", capitalizing the first letter produces "Hello world", not "Hello World". Those are different transformations.

javascript
1function titleCaseWords(value) {
2  return value.replace(/\b\p{L}/gu, char => char.toUpperCase());
3}
4
5console.log(titleCaseWords("hello world from js"));

Use the simpler function when you only need the first letter changed. Do not over-engineer the solution into a full title-casing routine unless the requirement says so.

Unicode and locale can matter

For many applications, toUpperCase() is enough. But if the string may contain language-specific casing rules, locale-aware transformations can matter.

javascript
console.log("istanbul".charAt(0).toLocaleUpperCase("tr-TR") + "istanbul".slice(1));

If your application has real internationalization requirements, do not assume English casing behavior is universal.

Package the rule into a small reusable helper

If capitalization happens in more than one place, put the rule in one helper so the project uses the same behavior everywhere.

javascript
export function capitalize(value) {
  return value ? value.charAt(0).toUpperCase() + value.slice(1) : value;
}

That makes later changes easier if you decide to switch to locale-aware behavior or first-non-space behavior. It also keeps templates and business logic consistent.

Keep the helper predictable

A good helper should state exactly what it does: first character or first non-space character, ASCII-friendly or locale-aware, leave the remainder unchanged or not. That clarity matters more than shaving one method call.

Common Pitfalls

  • Forgetting to handle empty strings before indexing or slicing.
  • Assuming capitalizing the first character is the same as title-casing every word.
  • Ignoring leading whitespace when the real requirement is the first visible letter.
  • Using locale-insensitive casing in applications with language-specific rules.
  • Lowercasing the rest of the string accidentally when the requirement was only to uppercase the first letter.

Summary

  • The common solution is value.charAt(0).toUpperCase() + value.slice(1).
  • Add an empty-string guard for clarity.
  • Decide whether whitespace should count as the first character.
  • Use a different routine if you actually need title case.
  • Consider locale-aware casing when internationalized text matters.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.