How to Make First Letter of a String Uppercase in JavaScript?

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Capitalize the first letter in JavaScript is simple, not complex. It typically involves changing the first letter to uppercase. Contrary to some beliefs, it doesn't require manipulating ASCII values directly, as JavaScript handles characters in UTF-16. This technique is helpful for formatting titles, country names, and headings.

How to capitalize the first letter of a word in JavaScript?

Capitalizing the first letter of a word in JavaScript can be achieved through several methods, each utilizing different aspects of the language to accomplish this task. Below are some effective techniques:

  1. The charAt JavaScript string method
  2. The slice JavaScript string method
  3. The toUpperCase JavaScript string method
  4. JavaScript replace() Function
  5. Using the split(), map(), and join() methods
  6. Using ES6 spread syntax and join() method
  7. ES6 arrow functions and template literals

Now, Let's discuss each method one by one.

1. The charAt JavaScript string method

The charAt() method is used to extract or get a single character from a string. It has the following syntax,

The charAt() method returns the character at the index passed as a parameter to the method. The index must be an integer between 0 and (length of the string)-1. This method doesn't change or alter the original string.

Example

Output

2. The slice JavaScript string method

The slice() method efficiently extracts parts of a string without altering the original. It's ideal for situations where you need a substring, using either positive indices from the start or negative indices to count backward. This method shines in tasks like extracting specific text segments or adjusting string casing.

Syntax

  • slice(start_index): Extracts from start_index to the string's end.
  • slice(start_index, end_index): Extracts a section from start_index to end_index.

Example

3. The toUpperCase JavaScript string method

The toUpperCase() method is used to convert lowercase characters into uppercase characters. The syntax of this method is as follows,

The toUpperCase() converts all the small case characters in the String to uppercase and returns it. This method also will not alter or change the original string.

Example

Output

4. JavaScript replace() Function

The JavaScript replace() function is an efficient tool for modifying strings, enabling you to swap out portions of text with new content. It's particularly useful for tasks like capitalizing the first letter in a string, showcasing its versatility in string manipulation.

Quick Overview:

  • Usage: String.replace(pattern, replacement)
  • Pattern: A string or regex identifying what to replace.
  • Replacement: The new text or a function returning text to insert.

Capitalize First Letter Example:

Output

Key Points:

  • Uses regex ^. to target the first character.
  • Arrow function capitalizes the matched character.
  • Demonstrates replace() for simple, targeted string changes.

5. Using the split(), map(), and join() Methods

Capitalizing the first letter of each word in JavaScript is straightforward with the split(), map(), and join() methods. This method is a key approach to learn how to capitalize the first letter in JavaScript, efficiently handling strings for improved readability.

Quick Guide:

  1. Split the string into words using split(' ').
  2. Capitalize the first letter of each word with map().
  3. Rejoin the words into a sentence using join(' ').

Example

Output

6. Using ES6 spread syntax and join() method

Using ES6 spread syntax alongside the join() method simplifies capitalizing the first letter in JavaScript. This approach, key for those learning how to capitalize the first letter in JavaScript, utilizes modern JavaScript features for concise code.

Steps:

  1. Spread the string into an array of characters.
  2. Capitalize the first character.
  3. Join the characters back into a string.

Example:

Output

7. ES6 arrow functions and template literals

ES6 introduces arrow functions and template literals, revolutionizing how we write concise and readable JavaScript code. Arrow functions streamline function declaration, making code less verbose, especially for one-liners and callbacks. Template literals enhance string creation, allowing embedded expressions and multi-line strings without concatenation.

Key Points:

  • Arrow Functions: Use => for concise function syntax. Ideal for callbacks and functions with single expressions.
  • Template Literals: Utilize backticks (`) for strings that can span multiple lines and embed expressions with ${expression}.

Example:

Conclusion

  • Capitalize the first letter in JavaScript is easily done with methods like charAt() and toUpperCase(), which don't alter the original string.
  • Techniques such as using slice() or regex with replace() allow for efficient text manipulation, demonstrating how to capitalize first letters without complex ASCII adjustments.
  • Modern javascript capitalize first letter features, including ES6 syntax with arrow functions and template literals, further simplify string capitalization tasks.
  • For both single letters and entire words in a sentence, these techniques provide straightforward strategies to improve text formatting in JavaScript projects, perfectly illustrating how to capitalize first letter in JavaScript.