If every integer between two and the integer nearest the square root of our given number doesn’t divide evenly into that given number (if the modulus of every integer is not zero), then that given number is prime.
A great place to start gaining an understanding of finding primes might be taking a look at Wikipedia, in the article about “sieve of Eratosthenes”. Good article, and a good introduction to algorithms in general.
This way of making an array adds one element at a time, slowly making the array bigger and bigger. That dynamic reallocation of space for the array is what is very slow.
In this case, since we need all numbers from 2 to num, there is actually no reason whatsoever to make the array anyways, as we can just for (let i = 2; i < num; i++) {....}.
If JavaScript had a proper range iterator, then this would be different, but unfortunately it does not.