-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
  • 169,008
  • 28
  • 173
  • 236
bafix2203
  • 541
  • 7
  • 25

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
  • 16,558
  • 19
  • 65
  • 93
  • 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