Javascript Prime numbers push to array stops for larger numbers with no error

This question is based on running the following code in the Chrome console

//function for returning true for Prime, false for not Prime
function isPrime(num){
if(num <=1) return ‘not a valid number’;

if (num === 2) return 'prime';

for(var i = 3; i < num; i+=2){
    if(num%i === 0)return false
} return true


//function for creating an array of the prime numbers up to number n and returning the array length
function primeList(n){
var arr = [2];
for (var j = 3; j < n; j+=2){
return arr.length;

When I run:
primeList(150) = 35
primeList(15000) = 1754
primeList(150000) = no result returned, no time out, no error

I have checked how many integers I can push into an empty array and it is far bigger than this. (1000000000)

Can you see any reason for this behaviour. I’m simply curious to why.


Hello @net5816413887, welcome to the forums! Could you please review ->this<- post about posting code on the forums. This means we can run the code, which helps to debug it.

Thanks for the heads up… how do I edit my post to highlight the code block? I can’t find the edit window??


So I copied your code into the console, had to change the quotes for ‘not a valid number’ as it was the wrong sort of quotes and managed to get the answer returned.


However, for the smaller values, the answer would appear without pressing enter, with 150000 though, I had to press enter to get the result.

If the edit icon isn’t present then too much time has probably surpassed to be able to edit it further.