The _.capitalize() method is a part of the Lodash library and is used to format strings. It takes a string as input and returns a new string where the first character is uppercase and all other characters are lowercase. This method is commonly used when formatting user inputs or ensuring consistent text case.
Syntax
_.capitalize(string);- string: The string that you want to capitalize.
Let's start with a simple example of how the _.capitalize() method works.
To make the lodash library available in your project, the user must download it from the NPM with the help of the following command.
npm install lodashconst a = require('lodash');
let text = "hello world";
let capital = a.capitalize(text);
console.log(capital);
Output

- Imports Lodash: The require('lodash') statement loads the Lodash library.
- Defines a string variable: The variable text is assigned the value "hello world".
- Capitalizes the first letter: The capitalize() method is used to transform "hello world" into "Hello world".
- Stores the result: The capitalized string is saved in the variable capitalizedText.
- Prints the output: The console.log() statement displays "Hello world" in the terminal.
How Does _.capitalize() Work
- Takes a string as input: It accepts a single string argument.
- Converts the first character to uppercase: The very first letter of the string is transformed into an uppercase letter.
- Converts all remaining characters to lowercase: Every other character in the string is changed to lowercase, even if they were originally uppercase.
- Returns a new string: The method does not modify the original string but returns a modified version.
- Ensures consistency: It helps maintain a standardized sentence-like format, making text more readable.
Practical Use Cases
1. Formatting Titles or Headings
_.capitalize() enhances readability by capitalizing the first letter, making it ideal for formatting headings or titles.
const _=require('lodash')
let title = "my first blog post";
let format = _.capitalize(title);
console.log(format); // Output: "My first blog post"
Output

- Lodash’s _.capitalize() formats the title by capitalizing the first letter and converting the rest to lowercase.
- console.log(formattedTitle) displays the formatted title, improving its readability to "My first blog post."
2. Standardizing User Input
Use _.capitalize() to format user input, like names, by capitalizing the first letter and converting the rest to lowercase.
const _=require('lodash')
let name = "jAIKANT";
let format = _.capitalize(name);
console.log(format);
Output

- _.capitalize() capitalizes the first letter of a string and converts the rest to lowercase.
- console.log(formattedTitle) outputs "My first blog post," improving readability.
Possible Scenarios to Consider
1. Empty String
If you pass an empty string to _.capitalize(), it will return an empty string.
const _=require('lodash')
let empty = "";
console.log(_.capitalize(empty)); // Output: ""
Output

- The variable emptyString is initialized as an empty string, holding no characters ("").
- Calling _.capitalize() on the empty string returns an empty string, as there are no characters to capitalize.
2. Single Character
If you provide a single character string, it will be capitalized.
const _=require('lodash')
let single = "a";
console.log(_.capitalize(single)); // Output: "A"
Output

- The variable singleChar is assigned the string "a": It contains only a single lowercase letter.
- _.capitalize() is used to capitalize the first letter: The method changes "a" to "A", resulting in the output "A".
3. Already Capitalized String
If the string already starts with a capital letter, _.capitalize() will ensure the rest of the string is in lowercase.
const _=require('lodash')
let Capital= "Hello";
console.log(_.capitalize(Capital));
Output

- The variable Capital is assigned the string "Hello", which already has the first letter capitalized.
- Applying _.capitalize() leaves the string unchanged as "Hello", since the first letter is uppercase and the rest are lowercase.
Features of lodash in JavaScript
- Deep cloning: Lodash provides _.cloneDeep() to create a deep copy of an object or array, preserving nested structures.
- Chaining: Lodash allows method chaining, enabling you to run multiple operations on a collection in a concise and readable manner with _.chain().
- Debouncing: The _.debounce() method helps limit the rate at which a function is executed, perfect for handling events like scrolling or resizing.
- Object manipulation: Lodash offers powerful utilities for working with objects, such as _.merge() for deep merging and _.get() for safely accessing nested properties.
- Array operations: Functions like _.uniq() remove duplicate values, and _.groupBy() groups elements based on a criterion, making array manipulation efficient.
Conclusion
Lodash’s _.capitalize() method capitalizes the first letter of a string and converts the rest to lowercase, making it ideal for text formatting, standardizing user input, and ensuring consistent presentation in your app. It saves time and reduces errors compared to custom case transformation logic.