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

JavascriptObject Oriented ProgrammingFront End Technology

_.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.

Live Demo

<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.

Live Demo

<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
raja
Published on 09-Jul-2019 18:58:07
Advertisements