How to convert a string to title case in Node.js

2 Answers

0 votes
function toTitleCase(s) {
  return s.toLowerCase().split(' ').map(function (word) {
        return (word.charAt(0).toUpperCase() + word.slice(1));
    }).join(' ');
}
 
console.log(toTitleCase("nodejs is a back-end javascript runtime environment"));
  
  
    
      
      
/*
run:
      
Nodejs Is A Back-end Javascript Runtime Environment
      
*/

 



answered May 24, 2022 by avibootz
0 votes
function toTitleCase(s) {
    return s.replace(/\w\S*/g, function (word) {
      return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase();
    }
  );
}
 
console.log(toTitleCase("nodejs is a back-end javascript runtime environment"));
  
  
    
      
      
/*
run:
      
Nodejs Is A Back-end Javascript Runtime Environment
      
*/

 



answered May 24, 2022 by avibootz
...