Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value:
4.
Examples 
4.1.
Capitalizing a Regular String:
4.2.
JavaScript
4.3.
Capitalizing Names:
4.4.
JavaScript
4.5.
Handling Strings with Leading Spaces:
4.6.
JavaScript
4.7.
Standardizing User Input:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.capitalize() handle non-alphabetic characters?
5.2.
Can _.capitalize() be used for each word in a string?
5.3.
Does _.capitalize() affect the case of letters other than the first one?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.capitalize() Method

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In text processing and user interface development, formatting strings correctly is crucial for readability and aesthetics. Lodash's _.capitalize() method provides a simple and effective way to capitalize the first character of a string and lower case the rest. 

Lodash _.capitalize() Method

This function is particularly useful in scenarios where string data needs to be presented in a standardized format, such as in user interfaces or when processing user inputs.

Why This Function is Used

The _.capitalize() function is used to ensure that a string conforms to a common textual format where only the first letter is capitalized. This is important in creating uniformity in text presentation, especially in user interfaces where text consistency can impact user experience and perception.

Syntax, Parameter and Return Value

Syntax: 

_.capitalize([string=''])

Parameters:

[string=''] (string): The string to capitalize.

Return Value:

 (string) - Returns the capitalized string.

Examples 

Capitalizing a Regular String:

  • JavaScript

JavaScript

var _ = require('lodash');

var text = 'hello world';

console.log(_.capitalize(text));
You can also try this code with Online Javascript Compiler
Run Code

 Output:

 'Hello world'


Demonstrates basic usage of capitalizing the first letter of a string.

Capitalizing Names:

  • JavaScript

JavaScript

var name = 'john doe';

console.log(_.capitalize(name));
You can also try this code with Online Javascript Compiler
Run Code

Output:

 'John doe'


Shows capitalizing the first letter of a name.

Handling Strings with Leading Spaces:

  • JavaScript

JavaScript

var title = '  lorem ipsum';

console.log(_.capitalize(title));
You can also try this code with Online Javascript Compiler
Run Code

Output: 

' lorem ipsum'


An example of how _.capitalize() handles strings with leading spaces.

Standardizing User Input:

  • JavaScript

JavaScript

var userInput = 'tODAY Is a great DAY!';

console.log(_.capitalize(userInput));
You can also try this code with Online Javascript Compiler
Run Code

Output:

 'Today is a great day!'


Demonstrates standardizing user input to start with a capital letter and the rest in lowercase.

Frequently Asked Questions

How does _.capitalize() handle non-alphabetic characters?

_.capitalize() will capitalize the first alphabetic character encountered and lowercase the rest. Non-alphabetic characters at the beginning of the string are left unchanged.

Can _.capitalize() be used for each word in a string?

_.capitalize() only capitalizes the first letter of the entire string. To capitalize each word, you would need to split the string into words and apply _.capitalize() to each word individually.

Does _.capitalize() affect the case of letters other than the first one?

Yes, all characters other than the first one are converted to lowercase by _.capitalize().

Conclusion

Lodash's _.capitalize() method is a useful tool for formatting strings such that only the first character is capitalized, and the rest are in lowercase. It's particularly beneficial for text presentation in user interfaces and standardizing user input.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass