-2

I want to get two smallest numbers from array with three numbers

I can only get one smallest number from array. I can't get two numbers. Can anyone help me?

var array = [2, 3, 5];

var min = Math.min(array[0], array[1], array[2]);
console.log(min)
mplungjan
  • 155,085
  • 27
  • 166
  • 222
bafix2203
  • 521
  • 3
  • 22

1 Answers1

0

Sort your array by value and grab the two first results:

var arr = [5,3,10,4,9,1];
arr.sort((a, b) => a - b);
console.log(arr[0], arr[1]);
Rafael Herscovici
  • 15,734
  • 17
  • 62
  • 92
  • 1
    sort sorts in place, so no need to add arr=arr.sort ... just arr.sort ... of course, one may not want the original array mutated – Jaromanda X Oct 23 '18 at 12:46