Underscore.js _.each() Function

25 Nov, 2021
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 _.each() function is an inbuilt function in Underscore.js library of JavaScript which is used to return the each element of the given list. Syntax: 
_.each(list, function)
Parameters: It accepts two parameters which are specified below: 
list: It is the list which contains some elements.
function: It is the function which is executed by taking each element of the list.
Return values: It returns each element of the list. JavaScript code to show the working of this function: 
Passing a list of numbers with alert() function: When the list of elements is passed to the alert inbuilt function then it takes the element from the list one by one and then shows it on the page (which is the function of the alert function). After alerting all the elements, the alert function ends and the _.each function also ends. 
javascript
<html>  <head>    <script>        <src = "https://cdnjs.cloudflare.com/ajax/libs/                underscore.js/1.9.1/underscore-min.js" >    </script>    <script type="text/javascript"     src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js         /1.9.1/underscore-min.js.map"></script>      <script type="text/javascript"      src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js          /1.9.1/underscore.js"></script></head>  <body>    <script type="text/javascript">        _.each([1, 2, 3], alert);    </script></body>  </html>
Output:  
Passing a list of words with prompt() function: When the list of words is passed to the prompt inbuilt function then it takes the words from the list one by one and then shows it on the page along with its index from the list (which is the function of the prompt function). After alerting all the elements, the prompt function ends and the _.each function also ends. 
javascript
<html>  <head>    <script>        <src = "https://cdnjs.cloudflare.com/ajax/libs/                underscore.js/1.9.1/underscore-min.js" >    </script>    <script type="text/javascript"     src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js         /1.9.1/underscore-min.js.map"></script>      <script type="text/javascript"      src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js          /1.9.1/underscore.js"></script></head>  <body>    <script type="text/javascript">        _.each(["Geeks", "for", "Geeks"], prompt);    </script></body>  </html>
Output:  
Passing a list of numbers with a user defined function: First we need to create a function by using the ‘function’ keyword. Then, when we pass the list of elements to the user defined function then it takes the element from the list one by one and print it. After all the elements are passed, the function ends. And the _.each function also ends. 
javascript
<html>  <head>    <script>        <src = "https://cdnjs.cloudflare.com/ajax/libs/                underscore.js/1.9.1/underscore-min.js" >    </script>    <script type="text/javascript"     src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js         /1.9.1/underscore-min.js.map"></script>      <script type="text/javascript"      src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js          /1.9.1/underscore.js"></script></head>  <body>    <script type="text/javascript">        function func() { document.write("Hi Geeks" +"<br>");}        _.each({one: 1, two: 2, three: 3}, func);    </script></body>  </html>
Output: 
Hi Geeks
Hi Geeks
Hi Geeks
surindertarika1234
javascript-functions
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Roadmap to Learn JavaScript For Beginners
Hide or show elements in HTML using display property
Difference Between PUT and PATCH Request
How to append HTML code to a div using JavaScript ?
File uploading in React.js
How to calculate the number of days between two dates in javascript?
How to Open URL in New Tab using JavaScript ?