Big to Small List Sorting

I wanted to return the largest number at integer[0]

list1= [4,6,2,1,9,63,-134,566]


var max = function(list)
{
 var bigToSmallList=[] //makin an empty list to return the biggest value at position 0
 list.sort() // sorting it into numerical order small to big
 
 
 for(i=list.length;i<list.length;i--) //want to iterate from the end to the start 
 {
    bigToSmallList.push(list[i])//adding next largest to list
    console.log(bigToSmallList) //just checking what's up 
 }


 return bigToSmallList[0]; //should return the biggest value at position 0
}

max(list1)

Seems to just return undefined?


var min = function(list){
    
    return Math.min.apply(null, list)
}

This works

var min = function(list){
    
    return Math.min(null, list)
}

This doesnt

var min = function(list){
    
    return Math.min(list)
}

Messing about with the Math.min and max methods and not sure why it doesnt work

what does the apply and null do and why is it necessary?

Math.min() takes single values as arguments.
list is an array and not a single value.
You can spread the array.

how do you spread an array or list?

In ES6 you can spread an array using the spread operator: …array

Example:

let arr = [1, 2, 3]
let arr2 = [...arr, 4, 5]
console.log(arr2) // returns [1, 2, 3, 4, 5]

For your use case, you could use return Math.min(...list);