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.
Padding the End of a String:
4.2.
JavaScript
4.3.
Padding with Custom Characters:
4.4.
JavaScript
4.5.
Aligning Text in Table Columns:
4.6.
JavaScript
4.7.
Formatting Data for Fixed-Width Output:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.padEnd() handle strings longer than the specified length?
5.2.
Can _.padEnd() pad with multiple characters?
5.3.
Is the padding applied evenly when using multiple characters?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.padEnd() Method

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

Introduction

In various programming scenarios, especially in text formatting and output alignment, padding a string to a specific length by adding characters to its end is often required. Lodash's _.padEnd() method provides a straightforward solution for this.

Lodash _.padEnd() Method

It pads a string on the right side until it reaches the specified length, making it particularly useful for aligning text, formatting tables, or ensuring uniform string lengths in data processing.

Why This Function is Used

The _.padEnd() function is used to extend the length of a string to a specified total length by adding padding characters to its end. This is important in scenarios where you need to ensure that strings align correctly or meet specific length requirements, such as in data reporting, creating fixed-width file formats, or aligning console output.

Syntax, Parameter and Return Value

Syntax:

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

Parameters:

  • [string=''] (string): The string to pad.
     
  • [length=0] (number): The padding length.
     
  • [chars=' '] (string): The string used as padding.

Return Value:

 (string) - Returns the right-padded string.

Examples 

Padding the End of a String:

  • JavaScript

JavaScript

var _ = require('lodash');

var text = 'hello';

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

Output: 

'hello     '

Demonstrates extending a string to a total length of 10 characters by adding spaces to the end.

Padding with Custom Characters:

  • JavaScript

JavaScript

var message = 'Note:';

console.log(_.padEnd(message, 10, '*'));
You can also try this code with Online Javascript Compiler
Run Code

 Output:

 'Note:*****'


Shows padding the end of a string with a custom character.

Aligning Text in Table Columns:

  • JavaScript

JavaScript

var items = ['Apple', 'Banana', 'Cherry'];

var paddedItems = items.map(item => _.padEnd(item, 10));

console.log(paddedItems.join('\n'));
You can also try this code with Online Javascript Compiler
Run Code

Output:

 'Apple     '
 'Banana    '
 'Cherry    '


An example of using _.padEnd() to align text in a column format.

Formatting Data for Fixed-Width Output:

  • JavaScript

JavaScript

var ids = ['123', '4567', '89'];

var formattedIds = ids.map(id => _.padEnd(id, 5, '0'));

console.log(formattedIds.join('\n'));
You can also try this code with Online Javascript Compiler
Run Code

Output:

'12300'
'45670'
'89000'


Demonstrates using padding to format data for a fixed-width output.

Frequently Asked Questions

How does _.padEnd() handle strings longer than the specified length?

If the string is already longer than the specified length, _.padEnd() returns the original string without adding any padding.

Can _.padEnd() pad with multiple characters?

Yes, you can specify a string of multiple characters for padding, and _.padEnd() will repeat this string until the total length is reached.

Is the padding applied evenly when using multiple characters?

If the padding string needs to be truncated to achieve the specified length, _.padEnd() will truncate the padding string at the end to fit the required length.

Conclusion

Lodash's _.padEnd() method is a valuable tool for extending the length of a string by adding padding characters to its end. It's useful for aligning text, preparing data for fixed-width formats, and ensuring uniform string lengths in various text formatting and data processing tasks.

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