---
title: "Write a javascript program that takes a string and returns a new string with capitalized words."  
description: "Write a javascript program that takes a string and returns a new string with capitalized words."  
author: "Revati S Misra"  
published: 2023-04-14  
updated: 2023-11-27  
canonical: https://www.mindstick.com/forum/157804/write-a-javascript-program-that-takes-a-string-and-returns-a-new-string-with-capitalized-words  
category: "javascript"  
tags: ["javascript", "string"]  
reading_time: 1 minute  

---

# Write a javascript program that takes a string and returns a new string with capitalized words.

Write a [javascript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) [program that takes](https://www.mindstick.com/forum/157801/write-a-javascript-program-that-takes-an-array-of-numbers-and-returns-the-highest-number) a [string and returns](https://www.mindstick.com/forum/157802/write-a-javascript-program-that-takes-a-string-and-returns-the-longest-word-length) a new string with capitalized words.

## Replies

### Reply by Aryan Kumar

Certainly! You can achieve this by splitting the input [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) into words, capitalizing each word, and then joining them back into a new string. Here's a simple JavaScript [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) for this:

```plaintext
function capitalizeWords(inputString) {
  // Split the input string into an array of words
  const words = inputString.split(' ');

  // Capitalize each word
  const capitalizedWords = words.map(word => {
    // Ensure the word is not an empty string
    if (word.length > 0) {
      return word.charAt(0).toUpperCase() + word.slice(1);
    } else {
      return '';
    }
  });

  // Join the capitalized words into a new string
  const resultString = capitalizedWords.join(' ');

  return resultString;
}

// Example usage:
const inputString = 'hello world! this is a test.';
const result = capitalizeWords(inputString);

console.log(result); // Outputs: 'Hello World! This Is A Test.'
```

This **capitalizeWords** function takes an input string, splits it into an array of words, capitalizes each word, and then joins them back into a new string. The example usage demonstrates how to use the function with a sample input string.


---

Original Source: https://www.mindstick.com/forum/157804/write-a-javascript-program-that-takes-a-string-and-returns-a-new-string-with-capitalized-words

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
