Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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.
Repeating a String Multiple Times:
4.2.
JavaScript
4.3.
Creating a Divider Line:
4.4.
JavaScript
4.5.
Formatting Text Output:
4.6.
JavaScript
4.7.
Generating Test Data:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
What happens if the repeat count is set to 0 or a negative number?
5.2.
Can _.repeat() be used with non-string values?
5.3.
Is there a limit to the number of repetitions?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.repeat() Method

Introduction

In programming, repeating a string a certain number of times is a common requirement, especially in text processing, formatting, or creating simple patterns. Lodash's _.repeat() method provides a straightforward way to replicate a string the specified number of times.

Lodash _.repeat() Method

 This function is particularly useful in scenarios where repetitive string patterns are needed, such as in generating UI components, creating test data, or formatting output.

Why This Function is Used

The _.repeat() function is used for generating a new string that consists of a given string repeated a specific number of times. This is essential in cases where a pattern or sequence needs to be created without manually concatenating strings, thereby simplifying code and enhancing readability.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax, Parameter and Return Value

Syntax: 

_.repeat([string=''], [n=1])

Parameters:

  • [string=''] (string): The string to repeat.
     
  • [n=1] (number): The number of times to repeat the string.

Return Value:

 (string) - Returns the repeated string.

Examples 

Repeating a String Multiple Times:

  • JavaScript

JavaScript

var _ = require('lodash');

var text = 'abc';

console.log(_.repeat(text, 3));

Output: 

'abcabcabc'


Demonstrates repeating a string three times.

Creating a Divider Line:

  • JavaScript

JavaScript

var divider = _.repeat('-', 20);

console.log(divider);

Output:

 '--------------------'


Shows how to create a simple divider line for text formatting.

Formatting Text Output:

  • JavaScript

JavaScript

var spaces = _.repeat(' ', 10);

var message = 'Start' + spaces + 'End';

console.log(message);

Output:

 'Start          End'


An example of using repeated spaces for text alignment.

Generating Test Data:

  • JavaScript

JavaScript

var testData = _.repeat('Sample Data | ', 5);

console.log(testData);

 Output: 

'Sample Data | Sample Data | Sample Data | Sample Data | Sample Data | '


Demonstrates generating repeated patterns for test data.

Frequently Asked Questions

What happens if the repeat count is set to 0 or a negative number?

If the repeat count n is set to 0 or a negative number, _.repeat() returns an empty string.

Can _.repeat() be used with non-string values?

_.repeat() is designed for strings. Non-string values should be converted to strings before being passed to _.repeat().

Is there a limit to the number of repetitions?

The number of repetitions is limited by memory and performance constraints. Excessively large repeat counts can lead to performance issues or memory overflow.

Conclusion

Lodash's _.repeat() method is a useful tool for generating a new string that repeats a given string a specified number of times. It's handy for creating patterns, formatting output, and generating test data in an efficient and readable manner.

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.

Previous article
Lodash _.isArray() Method
Next article
Lodash _.eq() Method
Live masterclass