Answers for "captialise first letter of each word js"

3

javascript uppercase first character of each word

const uppercaseWords = str => str.replace(/^(.)|\s+(.)/g, c => c.toUpperCase());

// Example
uppercaseWords('hello world');      // 'Hello World'
Posted by: Guest on July-03-2020
0

how to capitalize first letter of word in javascript

function capitalizeFirstLetter(str, inAllWordsOfString = false) {
  if (!inAllWordsOfString) {
    //convert given string to lowercase
    let lowerStr = str.toLowerCase();
    // Now convert first character to upper case
    let firstCharacter = str.charAt(0).toUpperCase();
    // Now combine firstCharacter and lowerStr and return
    return firstCharacter + lowerStr.slice(1);
  } else {
    let str1 = str.split(" ");
    let returnStr = "";

    for (let i = 0; i < str1.length; i++) {
      let lowerStr = str1[i].toLowerCase();
      returnStr =
        returnStr + str1[i].charAt(0).toUpperCase() + lowerStr.slice(1) + " ";
    }
    return returnStr.trim();
  }
}

capitalizeFirstLetter('hello word');
//output: Hello word

capitalizeFirstLetter('hello word', true);
//output : Hello Word
Posted by: Guest on June-10-2021

Code answers related to "captialise first letter of each word js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language