Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Title Case A Sentence JavaScript
Let’s say we are required to write a function that accepts a string and capitalizes the first letter of every word in that string and changes the case of all the remaining letters to lowercase.
For example, if the input string is −
hello world coding is very interesting
The output should be −
Hello World Coding Is Very Interesting
Let’s define a function capitaliseTitle() that takes in a string and capitalises the first letter of each word and returns the string −
Example
let str = 'hello world coding is very interesting';
const capitaliseTitle = (str) => {
const string = str
.toLowerCase()
.split(" ")
.map(word => {
return word[0]
.toUpperCase() + word.substr(1, word.length);
})
.join(" ");
return string;
}
console.log(capitaliseTitle(str));
Output
The console output will be −
Hello World Coding Is Very Interesting
Advertisements