Sunday, December 14, 2025
HomeLanguagesJavascriptUnderscore.js _.times() Function

Underscore.js _.times() Function

Underscore.js is a JavaScript library that makes operations on arrays, string, objects much easier and handy. 
The _.times() function in underscore.js is used to call the function a particular number of times i.e. execution of a function(f) “n” number of times.

Note: It is very necessary to link the underscore CDN before going and using underscore functions in the browser. When linking the underscore.js CDN link, the “_” is attached to the browser as a global variable.

Syntax:

_.times(n, iteratee)

Parameters:It takes following parameters:

  • n: It tells how many times a function is needed to be executed.
  • iteratee : It is a function that is to be invoked n times.

Return Value: It produces an array of returned values and this array is returned by the function.

Example 1:

HTML




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script>
        let n = 5
        let func = () => {
            console.log(`This function
                is called ${n} times \n`)
        }
 
        // The _.times function executes
        // the above func function n times
        _.times(n, func);
    </script>
</body>
 
</html>


Output:

Example 2:

Javascript




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script>
        let n = 5;
        let i = 1;
        let func = () => {
            for (i; i <= n; i++) {
                console.log(
        `It is the function call ${i}`)
            }
        }
 
        // Calling the function func n times.
        let c = _.times(n, func);
        console.log(
"array returned by times function: ", c)
    </script>
</body>
 
</html>


Output:

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
32447 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6816 POSTS0 COMMENTS
Nicole Veronica
11953 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12031 POSTS0 COMMENTS
Shaida Kate Naidoo
6951 POSTS0 COMMENTS
Ted Musemwa
7202 POSTS0 COMMENTS
Thapelo Manthata
6898 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS