Thursday, January 29, 2026
HomeLanguagesJavascriptUnderscore.js _.cat() Method

Underscore.js _.cat() Method

The _.cat() method is used to concatenate zero or more arguments into one array.

Syntax:

_.cat(arg1, arg2, ...)

Parameters: 

  • Args: This method accepts n arguments to concatenate into a single array.

Return Value: This method returns a concatenated array.

Note: This method 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 concatenate 2 arrays.

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Array1
var arr1 = [1,2,3]
  
// Array2
var arr2 = [4,5,6]
  
// Concatenation
var arr = _.cat(arr1, arr2); 
console.log("array 1 : "+ arr1); 
console.log("array 2 : "+ arr2); 
console.log("concatenated array : "+ arr); 


Output:

array 1 : 1,2,3
array 2 : 4,5,6
concatenated array : 1,2,3,4,5,6

Example 2: In this example, we will concatenate 2 numbers to form an array.

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Number 1
var num1 = 1
  
// Number 2
var num2 = 4
  
// Concatenation
var arr = _.cat(num1, num2); 
console.log("num1 : "+ num1); 
console.log("num2 : "+ num2); 
console.log("concatenated array : "+ arr); 


Output:

num1 : 1
num2 : 4
concatenated array : 1,4

Example 3: In this example, we will concatenate 3 arrays.

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
// Array1
var arr1 = [1,2,3]
// Array2
var arr2 = [4,5,6]
// Array3
var arr3 = [7,8,9]
// Concatenation
var arr = _.cat(arr1, arr2, arr3); 
console.log("array 1 : "+ arr1); 
console.log("array 2 : "+ arr2); 
console.log("array 3 : "+ arr3);
console.log("concatenated array : "+ arr); 


Output:

array 1 : 1,2,3
array 2 : 4,5,6
array 3 : 7,8,9
concatenated array : 1,2,3,4,5,6,7,8,9

Example 4: The _.cat() method will also work with the arguments object as if it were an array.

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Function
function f(){ return _.cat(arguments, 4, 5, 6); }
  
console.log("Array is : " + f(1,2,3));


Output:

Array is : 1,2,3,4,5,6
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

1 COMMENT

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
122 POSTS0 COMMENTS
Nango Kala
6848 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12065 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7221 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6916 POSTS0 COMMENTS