Thursday, October 9, 2025
HomeLanguagesJavascriptLodash _.pad() Method

Lodash _.pad() Method

The _.pad() method is used to pad the string on left and right sides if it is shorter than the given length. If the length can’t be divided evenly then padding characters are truncated.

Syntax:

_.pad([string=''], [length=0], [chars=' '])

Parameters: This method accepts three parameters as mentioned above and described below:

  • string: This parameter holds the string that need to pad.
  • length: This parameter holds the padding length.
  • chars: This parameter holds the string that is used as padding.

Return Value: This method returns the padded string.

Example 1:

Javascript




const _ = require('lodash'); 
  
var str1 = _.pad("GEEKS", 25, '-*-');
console.log(str1);
  
var str2 = _.pad("GFG--Geeks", 30, 'gfg');
console.log(str2);


Output:

"-*--*--*--GEEKS-*--*--*--"
"gfggfggfggGFG--Geeksgfggfggfgg"

Example 2:

Javascript




const _ = require('lodash'); 
  
var str1 = _.pad("GEEKS__FOR__GEEKS", 20, '_');
console.log(str1);
  
var str2 = _.pad("G4G--neveropen--GEEKS", 25, '*');
console.log(str2);
  
var str3 = _.pad("GEEKS-----FOR_____Geeks", 20);
console.log(str3);


Output:

"_GEEKS__FOR__GEEKS__"
"****G4G--neveropen--GEEKS****"
"gEEKS-----FOR_____Geeks"
RELATED ARTICLES

Most Popular

Dominic
32348 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7095 POSTS0 COMMENTS
Thapelo Manthata
6791 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS