Saturday, January 31, 2026
HomeLanguagesJavascriptLodash _.tap() Method

Lodash _.tap() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.

The _.tap() method of Sequence in lodash is used to call interceptor. Moreover, the main task of the method is to “tap into” a method chain sequence so that the intermediate results can be modified.

Syntax:

_.tap(value, interceptor)

Parameters: This method accepts two parameters as mentioned above and described below:

  • value: It is the value to be given to the interceptor.
  • interceptor: It is the function to be called.

Return Value: This method returns the value.

Example 1:

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Calling tap() method
let result = _([5, 6, 7]).tap(function(arr) {
  
   // Modifying input array using push
   // operation
   arr.push(8);
 })
.value();
  
 // Displays output
 console.log(result);


Output:

[ 5, 6, 7, 8 ]

Example 2:

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Calling tap() method
let result = _(['Geeks', 'for']).tap(function(arr) {
  
   // Modifying input array using push
   // operation
   arr.push('Geeks');
   })
   .value();
  
 // Displays output
 console.log(result);


Output:

[ 'Geeks', 'for', 'Geeks' ]

Example 3: Using pop operation and tail method.

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Calling tap() method
let result = _(['f', 'g', 'h']).tap(function(arr) {
  
   // Modifying input array using pop
   // operation
   arr.pop();
 })
   .tail()     // Using tail() method
   .value();
  
 // Displays output
 console.log(result);


Output:

[ 'g' ]

Reference: https://lodash.com/docs/4.17.15#tap

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
32478 POSTS0 COMMENTS
Milvus
122 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12066 POSTS0 COMMENTS
Shaida Kate Naidoo
6987 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6917 POSTS0 COMMENTS