Preprocess all primes till MAX . Create a boolean array " isPrime [ 0 . . MAX ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true .
let isPrime = new Array ( MAX + 1 ) ; isPrime . fill ( true ) ; sieveOfEratosthenes ( isPrime ) ;