Capitalize the First Letter of a String Using Lodash

Jul 30, 2022

If you want to capitalize the first letter of every word in a string, you can use Lodash's startCase() function.

const _ = require('lodash');

_.startCase('hello world, today is a beautiful day'); // Hello World, Today Is A Beautiful Day

If you want to capitalize only the first letter in the string, you can use Lodash's capitalize() function.

const example = 'hello world, today is a beautiful day';

_.capitalize(example); // Hello world, today is a beautiful day.

Vanilla JavaScript Alternative

You don't need Lodash to capitalize the first letter of a string in JavaScript. For example, below is how you can capitalize the first letter of a string in vanilla JavaScript.

const str = 'captain Picard';

const caps = str.charAt(0).toUpperCase() + str.slice(1);
caps; // 'Captain Picard'

You can also capitalize the first letter of each word as follows.

const str = 'captain picard';

function capitalize(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
}

const caps = str.split(' ').map(capitalize).join(' ');
caps; // 'Captain Picard'

More Lodash Tutorials