Computer >> Computer tutorials >  >> Programming >> Javascript

What is the use of _.size() method in JavaScript?


_.size()

_.Size() is from the underscore.js library of javascript. This is used to find the size of an array. One should make sure that before using this method one should use the CDN of the underscore.js to execute the code.

syntax

_.size(array);

Example-1

In the following example, a normal array is passed into _.size() method to get the size of the array.

<html>
<body>
<head>
<script 
   src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
   var arr = [100, 62, 73, 534, 565];
   document.write(_.size(arr));
</script>
</body>
</html>

Output

5


Example-2

In the following example, an array that consists of object elements is sent into _.size() method to find the size.

<html>
<body>
<head>
<script 
   src ="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
   var arr = [{name:"Anu", age: 25},
              {name:"uday", age: 29},
              {name:"Anusha", age: 23}];
   document.write(_.size(arr));
</script>
</body>
</html>

Output

3