javascript capitalize first letter of each word
function titleCase(str) { var splitStr = str.toLowerCase().split(' '); for (var i = 0; i < splitStr.length; i++) { // You do not need to check if i is larger than splitStr length, as your for does that for you // Assign it back to the array splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1); } // Directly return the joined string return splitStr.join(' '); } document.write(titleCase("I'm a little tea pot"));
Source: stackoverflow.com
capitalize first letter of string javascript
let val = ' this is test '; val = val.trim(); val = val.charAt(0).toUpperCase() + val.slice(1); console.log("Value => ", val);
Source: stackoverflow.com
capitalize last letter javascript
function uppercaseLastCharacters(title: string, n = 2) { if (title.length < n) { return title; } return title.slice(0, -n).concat(title.slice(-n).toUpperCase()); }