
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Capitalizing first letter of each word JavaScript
We are required to write a JavaScript function that takes in a string of words. The function should construct a new string in which the first letter of each word from the original string is capital.
For example −
If the input string is −
const str = 'this is some random string';
Then the output should be −
const output = 'This Is Some Random String';
Example
const str = 'this is some random string'; const capitaliseFirst = (str = '') => { const strArr = str.split(' '); const newArr = strArr.map(word => { const newWord = word[0].toUpperCase() + word.substr(1, word.length - 1); return newWord; }); return newArr.join(' '); }; console.log(capitaliseFirst(str));
Output
And the output in the console will be −
This Is Some Random String
- Related Articles
- Return String by capitalizing first letter of each word and rest in lower case with JavaScript?
- How to capitalize the first letter of each word in a string using JavaScript?
- Print first letter of each word in a string in C#
- Java Program to Print first letter of each word using regex
- C++ Program to Print first letter of each word using regex
- Golang program to print first letter of each word using regex
- Python program to capitalize each word's first letter
- Print first letter of each word in a string using C# regex
- Getting first letter of each word in a String using regex in Java
- How to get first letter of each word using regular expression in Java?
- Capitalize last letter and Lowercase first letter of a word in Java
- Finding word starting with specific letter in JavaScript
- Make first letter of a string uppercase in JavaScript?
- Grouping names based on first letter in JavaScript
- Delete duplicate elements based on first letter – JavaScript

Advertisements