Updated April 7, 2023
Definition of JavaScript Underscore
Java provides the JavaScript library that means unserscore.js to the user. Underscore.js provides different types of functions and that function is helpful to programmers to build any type of application without the help of built-in objects. Basically, Underscore is the identifier in JavaScript that means we can identify or we can perform any kind of operation with the help of Underscore. Normally underscore is used in JavaScript to define any field private or public without using private and public keywords. By using underscore (_) we can easily identify the private class and member of that class, so it is a widely used concept in programming language and all programmers will identify it.
Syntax:
<script sc="underscore-main.js"></script>
<script type="text/javascript">
{function(){
var list=['jenny','pooja','sameer'];
_.each(list, function(name, key))
console.log(name);
console.log(key);
}
};
Explanation
In the above syntax, we use JavaScript; here first we need to import the underscore library as shown in the above syntax. After that we take the script type and create a new function, inside the function we write the value and at the end of syntax, we use .each function to access the list value that we already wrote in the list. Finally, we print the value by using a console statement.
How underscore JavaScript works?
Now let’s see how underscore works in JavaScript as follows. First, understand some important features of underscore as follows.
Collection
Normally underscore.js provides different kinds of functions for collection such as each map and reduce. The functions we can apply to the collection, also provide different methods such as groupBy, countBy, min, and max.
Arrays
Underscore.js also provides the different functions for the array such as initial, lastindexOf, intersection, etc.
Functions
When we need to implement the bind, delay, before and after functions, at that time we can also use underscore.js.
Objects
We can manipulate the object; we can map the object as well as we can compare the object by using the underscore.js library.
Utilities
If we need to implement different utilities such as escape, random, and iterate, etc.
Now let’s see how we can set up the underscore.js as follows.
Basically, there are two ways to implement the underscore.js as follows.
1. We can execute the underscore.js file by using the browser:
This is the first way to implement the underscore.js file; in this method, we can directly execute this in the browser. So we need to follow some steps as follows.
Steps:
1. First we need to go to the official website of understand.js, there is one more option that gives you the latest underscore-min.js file and in this file, we cannot make any changes.
2. After that we need to add the underscore-min.js file into the script and we can start working with the underscore.js file.
Now let’s use the second method as follows.
2. Implement underscore.js by using the Node.js:
This is another method to implement underscore.js but make sure Node.js and npm must be installed in your system. Otherwise, we can use the following command to install the required setup.
npm install underscore
Now let’s see the iterating collection in underscore as follows.
- _.each (): we can write the syntax of this method is _.each (list, iteratee) and it is used to get each parameter. In this method there are two parts such as an array or object to iterate and callback function.
The execution of this method is very easy, in which every object of the array that .each() will callback the function and inside that function again we have different parameters such as the value of array, index or array and iterating of the array that we showed in syntax. - _.map (): This is the inbuilt function of the underscore.js library. Basically _.map () function is used to create a new array and map each and every element from the list by using a transformation function.
Syntax
_.map(array_list, transformation_function)
Explanation
In the above syntax, we use two parameters as follows.
array_list: It is used to store the element into the array list.
Transformation_function: it is a function and it executes all elements from the array list.
Now let’s see how we can pass the element to a user-defined function.
We know that we have an array list that contains the array element and we need to access them one by one by using a user-defined function. In this case, a user-defined function fetches the element one by one and performs the operation that we require, and displays the result on screen by using the console.
Now let’s see some methods of Processing Collections as follows.
- _.contains (): The underscore.js also provides the _.contains () method to check whether the element is present in the list or not.
Syntax:
_.contains (array_list, value_to_search, [array_index])
Explanation
In the above syntax, we used different parameters as follows.
- array_list: It is used to store the element into the array list.
- value_to_search: This is used to store the element that we need to search.
- array_index: This is an optional part of this syntax; basically it is used to hold the starting index to search.
In processing collections, we have a different method and some are listed below.
Invoke, pluck, max, min, sortBy, groupBy, indexBy, countBy, etc.
Now let’s see iterating array methods as follows.
Underscore.js provides the array iterate method listed below.
first, initial, last, rest, indexOf, lastIndexOf, sortedIndex etc.
Now let’s see the different functions in underscore.js listed below.
By using functions we can easily handle the underscore.js functions as listed below.
Bind, partial, delay, once, wrap, negate, etc.
Underscore.js provides 100+ methods to the user.
Examples
Now let’s see the different examples of underscore.js in JavaScript as follows.
<!DOCTYPE html>
<html>
<head>
<title=Underscore.js in JavaScript /title>
<script sc = underscore-min.js></script>
<body>
<script type="text/javascript">
var num = [150, 100, 50, 40, 200];
console.log(_.min(num));
</script>
</head>
</body>
</html>
Explanation
In the above example, we write code in an Html file and we use a built-in library that is underscore-min.js. In this program, we try to find the minimum number from the given array list by using _.min () method of underscore.js. The final output of this program we illustrated by using the following screenshot as follows.
So in this way, we can use different methods to perform the different operations.
Conclusion
We hope from this article you learn Underscore JavaScript. From the above article, we have learned the basic syntax of Underscore JavaScript and we also see different examples of Underscore JavaScript. From this article, we learned how and when we use Underscore JavaScript.
Recommended Articles
This is a guide to JavaScript Underscore. Here we discuss the definition, syntax, How underscore JavaScript works? examples with code implementation. You may also have a look at the following articles to learn more –