Monday, May 11, 2026
HomeLanguagesJavascriptUnderscore.js _.splat() Method

Underscore.js _.splat() Method

The _.splat() method takes a function that accepts one or more arguments and hence returns a function that takes an array and uses its elements as the arguments to the original function.

Syntax:

_.splat( function );

Parameters: 

  • function: original function containing arguments.

Return Value: This method returns a function.

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 see the use of the _.splat() method.

javascript




// Defining underscore contrib variable
const _ = require('underscore-contrib');
 
function addNum(a, b) {
    return a + b;
}
 
let listTwoNamesFromArray = _.splat(addNum);
 
console.log(listTwoNamesFromArray([1, 2]));


Output:

3

Example 2: In this example, we will see the use of the _.splat() method.

javascript




// Defining underscore contrib variable
const _ = require('underscore-contrib');
 
function addNum(a, b) {
    return a + " : " + b;
}
 
let listTwoNamesFromArray = _.splat(addNum);
 
console.log(listTwoNamesFromArray(
    ["neveropen", "Computer Science Portal for Geeks"]));


Output:

neveropen : Computer Science Portal for Geeks
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
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS