get the max value of array javascript code example

Example 1: get first 10 items of array javascript

const list = ['apple', 'banana', 'orange', 'strawberry']
const size = 3
const items = list.slice(0, size) // res: ['apple', 'banana', 'orange']

Example 2: max value in array javascript

// For large data, it's better to use reduce. Supose arr has a large data in this case:
const arr = [1, 5, 3, 5, 2];
const max = arr.reduce((a, b) => { return Math.max(a, b) });

// For arrays with relatively few elements you can use apply: 
const max = Math.max.apply(null, arr);

// or spread operator:
const max = Math.max(...arr);

Example 3: get index of element in array js

const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];

console.log(beasts.indexOf('bison'));
// expected output: 1

// start from index 2
console.log(beasts.indexOf('bison', 2));
// expected output: 4

console.log(beasts.indexOf('giraffe'));
// expected output: -1

//*** Thanks to MDN Web Docs ***//
//https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array/indexOf

Example 4: javascript max array

var values = [3, 5, 6, 1, 4];

var max_value = Math.max(...values); //6
var min_value = Math.min(...values); //1

Example 5: how to find max in array

int[] a = new int[] { 20, 30, 50, 4, 71, 100};
		int max = a[0];
		for(int i = 1; i < a.length;i++)
		{
			if(a[i] > max)
			{
				max = a[i];
			}
		}
		
		System.out.println("The Given Array Element is:");
		for(int i = 0; i < a.length;i++)
		{
			System.out.println(a[i]);
		}
		
		System.out.println("From The Array Element Largest Number is:" + max);

Example 6: find highest number in array javascript

function findHigestNumber(nums) {
	
	let inputs = nums.filter((val, i) => nums.indexOf(val) === i)
	let max = inputs.length - 1;
	let min = 0;

	for(let i = 0; i < inputs.length; i++) {
      
      if(inputs[i] > max) max = inputs[i];
      if(inputs[i] < min) min = inputs[i];
	}
  
   return max + (-min);
}

console.log(difference([1, 7, 18, -1, -2, 9]));