Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

max value from array in javascript

// For regular arrays:
var max = Math.max(...arrayOfNumbers);

// For arrays with tens of thousands of items: 
let max = testArray[0];  //here we have considered max to the first element because we don't know which is max yet.
for (let i = 1; i < testArrayLength; ++i) { 
  if (testArray[i] > max) {  //in each iteration it will compare if the value is greater than the current considered value (we just considered first element)
    max = testArray[i]; //in the above iteration if the testArray find value/element greater than max then this new max value will be considered as Max (this will happen until the max value found).
  }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #max #array #javascript
ADD COMMENT
Topic
Name
5+6 =