how to use the tail function from the underscore library in javascript

The _.tail() function from the Underscore library in JavaScript returns all but the first element of an array. The function does not modify the original array.

Here's an example usage of the _.tail() function:

index.tsx
const _ = require('underscore');

const numbers = [1, 2, 3, 4, 5];
const tailNumbers = _.tail(numbers);

console.log(tailNumbers); // [2, 3, 4, 5]
console.log(numbers); // [1, 2, 3, 4, 5]
188 chars
8 lines

In the code above, we load the Underscore library using the require() function, and create an array of numbers. We then call the _.tail() function on the array to obtain a new array with the first element removed. We store this new array in the tailNumbers variable and log it to the console. We also log the original numbers array to show that it has not been modified.

The output of the code above in the console would be:

index.tsx
[2, 3, 4, 5]
[1, 2, 3, 4, 5]
29 chars
3 lines

Note that there is also a native Array.prototype.slice() method in JavaScript which can achieve the same result as _.tail().

gistlibby LogSnag