The _.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 so as to return that number of random elements from the array.
Parameters:It takes two arguments:
- The List
- The number n
It returns an of the element from the passed array.
- 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.
Id 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.
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.
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.
These commands will not work in Google console or in firefox as for these additional files need to be added which they didn’t have added.
So, add the given links to your HTML file and then run them.
The links are as follows:
An example is shown below:
- How to check an element has certain CSS style using jQuery ?
- How to get the coordinates of a mouse click on a canvas element ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.