Use case conversion methods and regex for different formats
const text = 'hello world';
const upper = text.toUpperCase(); // "HELLO WORLD"
const lower = text.toLowerCase(); // "hello world"const camelCase = str =>
str.replace(/[-_\s]+(\w)/g, (_, c) => c.toUpperCase())
.replace(/^\w/, c => c.toLowerCase());
const snakeCase = str =>
str.replace(/([A-Z])/g, '_$1')
.toLowerCase()
.replace(/^_/, '');
const kebabCase = str =>
str.replace(/([a-z])([A-Z])/g, '$1-$2')
.toLowerCase();
const titleCase = str =>
str.toLowerCase()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
console.log(camelCase('hello-world-example'));
console.log(snakeCase('helloWorldExample'));
console.log(kebabCase('HelloWorldExample'));
console.log(titleCase('hello world example'));const pascalCase = str =>
str.replace(/[-_\s]+(\w)/g, (_, c) => c.toUpperCase())
.replace(/^\w/, c => c.toUpperCase());const capitalize = str =>
str.charAt(0).toUpperCase() + str.slice(1);Avoid manual character replacement
// DON'T DO THIS
var result = '';
for (var i = 0; i < str.length; i++) {
result += str[i].toUpperCase();
}✓ Works in all modern browsers (ES5+)