undefined elements inside an array which has randomized numbers inside it

so I created this script to generate an array of 5 random numbers where the numbers must be unique, but I get +2 undefined elements. There are 5 numbers which are unique but the length of the array isn’t anymore 5

let arr = [];
while (arr.length <= 5) {
  let x = Math.floor(Math.random() * 11);
  if (arr.includes(x) === false) {
    arr.push(x);
  }
  arr.length += 1;
}
console.log(arr);

Thanks in advance guys.

100 thoughts on “undefined elements inside an array which has randomized numbers inside it”

Leave a Comment