A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/lodash-_-capitalize-method/ below:

Lodash _.capitalize() Method - GeeksforGeeks

Lodash _.capitalize() Method

Last Updated : 15 Jul, 2025

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);

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 lodash
JavaScript
const a = require('lodash');

let text = "hello world";
let capital = a.capitalize(text);
console.log(capital);

Output

Lodash _.capitalize() Method How Does _.capitalize() Work Practical Use Cases 1. Formatting Titles or Headings

_.capitalize() enhances readability by capitalizing the first letter, making it ideal for formatting headings or titles.

JavaScript
const _=require('lodash')
let title = "my first blog post";
let format = _.capitalize(title);
console.log(format);  // Output: "My first blog post"

Output

formatting Titles or Headings 2. Standardizing User Input

Use _.capitalize() to format user input, like names, by capitalizing the first letter and converting the rest to lowercase.

JavaScript
const _=require('lodash')
let name = "jAIKANT";
let format = _.capitalize(name);
console.log(format); 

Output

Standardizing User Input Possible Scenarios to Consider 1. Empty String

If you pass an empty string to _.capitalize(), it will return an empty string.

JavaScript
const _=require('lodash')
let empty = "";
console.log(_.capitalize(empty));  // Output: ""

Output

Empty String 2. Single Character

If you provide a single character string, it will be capitalized.

JavaScript
const _=require('lodash')
let single = "a";
console.log(_.capitalize(single));  // Output: "A"

Output

Single Character 3. Already Capitalized String

If the string already starts with a capital letter, _.capitalize() will ensure the rest of the string is in lowercase.

JavaScript
const _=require('lodash')
let Capital= "Hello";
console.log(_.capitalize(Capital));  

Output

Already Capitalized String Features of lodash in JavaScript 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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4