Lodash _.capitalize() Method

Lodash _.capitalize() method is used to convert the first character of string to upper case and the remaining to lower case.  

Syntax:

_.capitalize([string=''..."])

Parameters:

This method accepts single parameter as mentioned above and described below:

  • string: This parameter holds the string that need to convert first character to upper case character and remaining to lower case characters.

Return Value:

This method returns the capitalize string.

Example 1: In this example, the Lodash _.capitalize() function is used to capitalize the first letter of the given strings.

Javascript




const _ = require('lodash');
 
let str1 = _.capitalize("w3wiki");
console.log(str1);
 
let str2 = _.capitalize("GFG--Beginner");
console.log(str2);


Output:

"w3wiki"
"Gfg--Beginner"

Example 2: This code utilizes the Lodash _.capitalize() function to capitalize the first letter of each string in three different scenarios.

Javascript




const _ = require('lodash');
 
let str1 = _.capitalize("Beginner__FOR__Beginner");
console.log(str1);
 
let str2 = _.capitalize("Beginner FOR Beginner");
console.log(str2);
 
let str3 = _.capitalize("Beginner--FOR--Beginner");
console.log(str3);


Output:

"Beginner__for__Beginner"
"Beginner for Beginner"
"Beginner--for--Beginner"