Underscore.js _.sample() Function
Last Updated :
17 Jan, 2024
Underscore.js _.sample() function is used to find out what kind of elements are present in the array. It gives a random element of the array as output. We can even pass a second parameter to return that number of random elements from the array.
Syntax:
_.sample(list, [n])
Parameters:
- list: It is the list or the array which will be tested in this function.
- n: It is the number of elements that will be returned by the function.
Return values:
It returns the specified number of random elements from the passed array or only one random element by default.
Passing a list of numbers to the _.sample() function
The ._sample() function uses a random function and then displays that element from the list as the result. If the second parameter is not mentioned then t's default value will be taken which is 1. Hence, any one of the element will be displayed.
Example: The below code example implements the _.sample() function with a array of numbers without passing second parameter.
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
console.log(_.sample([1, 2, 3, 4, 5, 6]));
</script>
</body>
</html>
Output:

Passing the second parameter to the _.sample() function:
If we pass the second parameter then the _.sample() function will return as many elements from the passed list as mentioned. The result will be an array containing the number of elements which is present in the second parameter.
Example: The below code will explain the use of the _.sample() function by passing the second argument.
html
<!DOCTYPE html>
<html>
<head>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
console.log(_.sample([1, 2, 3, 4, 5, 6], 3));
</script>
</body>
</html>
Output:

Passing a structure to the _.sample() function:
We can even pass a structure to the _.sample() function and it will work in the same manner. It will display any of the element of the structure randomly as the output. Since, no second parameter is mentioned therefore, it will have only one element of the passed list in the result along with it's all properties.
Example: The below code will illustrate the use of the _.sample() function by passing structure as an argument.
html
<!DOCTYPE html>
<html>
<head>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
const people = [
{
"name": "sakshi",
"hasLong": "false"
},
{
"name": "aishwarya",
"hasLong": "true"
},
{
"name": "akansha",
"hasLong": "true"
},
{
"name": "preeti",
"hasLong": "true"
}
]
console.log(_.sample(people));
</script>
</body>
</html>
Output:

Passing a structure with only one property to the _.sample() function together:
If we pass a structure with only one property then it will work in the same way and display any one of the element randomly from the structure passed. Here also, since the second parameter is not mentioned therefore, the resultant array will contain only one element.
Example: The below code will use the _.sample() function with the structure with only one property.
html
<!DOCTYPE html>
<html>
<head>
<script src =
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
const users = [
{"num":"10"},
{"num":"9"},
{"num":"8"},
{"num":"7"},
{"num":"6"}
];
console.log(_.sample(users));
</script>
</body>
</html>
Output:

Similar Reads
Underscore.js _.some Function
Underscore.js _.some() function is used to find whether any value in the given list matches the given condition or not. If at least one value satisfies this condition then the output will be true. When none of the values matches then the output will be false. Syntax: _.some(list, [predicate], [cont
4 min read
Underscore.js _.tap() Function
Underscore.js is a JavaScript library that provides a lot of useful functions that help in the programming in a big way like the map, filter, invoke, etc even without using any built-in objects. The _.tap() function is an inbuilt function in Underscore.js library of JavaScript which is used to call
2 min read
Underscore.js _.map() Function
The Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects. The _.map() function is an inbuilt function in Underscore.js library of the JavaScript which is used
4 min read
Underscore.js _.template() Function
Underscore.js _.template() function is used to compile JavaScript templates into functions that can be evaluated for rendering. Useful for rendering complicated bits of HTML from JSON data sources. Template functions to create a template function that is compiled and can interpolate properties of da
2 min read
Underscore.js _.size Function
The Underscore.js is a JavaScript library that provides a lot of useful functions that help in programming in a big way like the map, filter, invoke, etc even without using any built-in objects. The _.size() function is used to find the size of an array, i.e. the number of elements in the array. It
3 min read
Underscore.js _.shuffle Function
The Underscore.js _.shuffle() function is used to arrange a list of arrays in a random manner. This _.shuffle() underscore function uses Fisher Yates Shuffle which is discussed in the below-mentioned article. So, every time we use this the output of this function will be different according to the F
4 min read
Underscore.js _.times() Function
Underscore.js _.times() function is used to call the function a particular number of times i.e. execution of a function(f) "n" times. NOTE: It is very necessary to link the underscore CDN before going and using the underscore functions in the browser. When linking the underscore.js CDN link, the "_"
2 min read
Underscore.js _.range() Function
_.range() function: It is used to print the list of elements from the start given as a parameter to the end also a parameter. The start and step parameters are optional. The default value of start is 0 and that of step is 1. In the list formed the start is inclusive and the stop is exclusive. The st
3 min read
Underscore.js _.mod() Function
The _.mod() function returns the remainder of dividing the given dividend by the given divisor. Syntax: _.mod( dividend, divisor ); Parameters: dividend: given dividend from which mod is calculated.divisor: given divisor which is used to calculate the modulus. Return Value: This method returns the c
1 min read
Underscore.js _.min Function
The Underscore.js is a JavaScript library that provides a lot of useful functions that help in programming in a big way like the map, filter, invoke, etc even without using any built-in objects. The _.min() function is used to find the minimum element from the list passed. If an iterate is given, th
3 min read