Saturday, October 18, 2025
HomeLanguagesJavascriptUnderscore.js _.repeat() Method

Underscore.js _.repeat() Method

The _.repeat() method takes an integer and a value and creates an array of that size containing the given the value given integer times.

Syntax:

_.repeat(n, value)

Parameters: 

  • n: The given integer for the times the value will be in the array.
  • value: The value to be in the array n times.

Return Value: This method returns a newly created array.

Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed.  Underscore.js contrib library can be installed using npm install underscore-contrib –save.

Example 1: In this example, we will generate a repeat array using this method.

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Integer
var integer = 10;
  
// Value
var value = "neveropen";
  
// Generating Array using Repeat method
var arr =_.repeat(integer, value);
console.log("Integer : ", integer);
console.log("Value : ", value);
console.log("Generated Array : ", arr);


Output:

Integer :  10
Value :  neveropen
Generated Array :  [
  'neveropen',
  'neveropen',
  'neveropen',
  'neveropen',
  'neveropen',
  'neveropen',
  'neveropen',
  'neveropen',
  'neveropen',
  'neveropen'
]

Example 2: Value can also be an integer.

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Integer
var integer = 10;
  
// Value
var value = 1;
  
// Generating Array using Repeat method
var arr =_.repeat(integer, value);
console.log("Integer : ", integer);
console.log("Value : ", value);
console.log("Generated Array : ", arr);


Output:

Integer :  10
Value :  1
Generated Array :  [
  1, 1, 1, 1, 1,
  1, 1, 1, 1, 1
]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS