Use the array’s length property:
function calculate(array) { var i = 0, sum = 0, len = array.length; while (i < len) { sum = sum + array[i++]; } return sum / len; }
Related Posts:
- Loop through an array in JavaScript
- Why is using “for…in” for array iteration a bad idea?
- What is the difference between ( for… in ) and ( for… of ) statements?
- How to add an object to an array
- Return array from function
- Javascript call() & apply() vs bind()?
- What’s the difference between “{}” and “[]” while declaring a JavaScript array?
- For-each over an array in JavaScript
- How do I check if an array includes a value in JavaScript?
- Loop through an array in JavaScript
- How can I remove a specific item from an array?
- Find object by id in an array of JavaScript objects
- Split string into array
- How can I remove a specific item from an array?
- Array.size() vs Array.length
- Sort array of objects by string property value
- How to create a jQuery function (a new jQuery method or plugin)?
- Copy array by value
- How can I reverse an array in JavaScript without using libraries?
- What is the way of declaring an array in JavaScript?
- Understanding nested for loops in javascript
- How to create an associative array in JavaScript literal notation
- Remove duplicate values from JS array [duplicate]
- How can I create a two dimensional array in JavaScript?
- How to loop through an array containing objects and access their properties
- Get the last item in an array
- How do I empty an array in JavaScript?
- Remove duplicate values from JS array
- How can I create a two dimensional array in JavaScript?
- Get the last item in an array
- How to filter an array/object by checking multiple values
- How do you check if a variable is an array in JavaScript?
- How do I empty an array in JavaScript?
- Short circuit Array.forEach like calling break
- How to check if array is empty or does not exist?
- How do I loop through or enumerate a JavaScript object?
- Deleting array elements in JavaScript – delete vs splice
- Filter array to have unique values
- How to randomize (shuffle) a JavaScript array?
- How to compare arrays in JavaScript?
- How to convert an Object {} to an Array [] of key-value pairs in JavaScript
- Fastest way to duplicate an array in JavaScript – slice vs. ‘for’ loop
- What is the difference between call and apply?
- Merge/flatten an array of arrays
- How to print elements from array with javascript
- Remove Object from Array using JavaScript
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- Short circuit Array.forEach like calling break
- Short circuit Array.forEach like calling break
- Most efficient way to convert an HTMLCollection to an Array
- How would I call a javascript function in html?
- Remove duplicate values from JS array
- How to loop through an array containing objects and access their properties
- Sorting arrays in javascript by object key value
- Convert Array to Object
- Pass a JavaScript function as parameter
- includes() not working in all browsers
- push object into array
- Counting the occurrences / frequency of array elements
- Remove last item from array
- Uncaught TypeError: .indexOf is not a function
- Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
- Anagrams finder in javascript
- Objects are not valid as a React child. If you meant to render a collection of children, use an array instead
- Can a for loop increment/decrement by more than one?
- Finding the average of an array using JS
- How to change value of object which is inside an array using JavaScript or jQuery?
- Checking for duplicate strings in JavaScript array
- What is define([ , function ]) in JavaScript?
- Why can’t we pass arrays to function by value?
- use Lodash to sort array of object by value
- Stopping a JavaScript function when a certain condition is met
- Showing an image from an array of images – Javascript
- How to get the selected radio button’s value?
- How to remove all duplicates from an array of objects?
- Finding the average of an array using JS
- Is there a simple way to make a random selection from an array in JavaScript or any other programming language?
- Uncaught TypeError: Cannot read property ‘toUpperCase’ of undefined
- How to get the difference between two arrays in JavaScript?
- Sort array of objects by single key with date value
- Simplest/cleanest way to implement a singleton in JavaScript
- Most efficient method to groupby on an array of objects
- Array Mapping in AngularJs
- Convert array to JSON
- How to get the first element of an array?
- JavaScript array to CSV
- How to sort an array of integers correctly
- console.log showing contents of array object
- JavaScript button onclick not working
- console.log showing contents of array object
- How to sort 2 dimensional array by column value?
- anchor jumping by using javascript
- Get JavaScript object from array of objects by value of property [duplicate]
- Complex JSON nesting of objects and arrays
- How do I store an array in localStorage?
- JavaScript Array Push key value
- Javascript- Multiplying 2 numbers and return number
- javascript push multidimensional array
- Find a value in an array of objects in Javascript [duplicate]
- $http.get(…).success is not a function