JavaScript Program to Display Prime Numbers Between Two Intervals Using Functions Last Updated : 19 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Prime numbers are natural numbers greater than 1 that are only divisible by 1 and themselves. In this article, we will explore how to create a JavaScript program to display all prime numbers between two given intervals using functions. Approach 1: Using Basic Loop and FunctionThe most straightforward approach is to use a loop to iterate through each number in the given interval and check if it is a prime number using a separate function. In this example, we have a function isPrime that checks if a number is prime, and a function displayPrimes that uses isPrime to display all prime numbers in a given range. JavaScript function isPrime(number) { if (number <= 1) return false; for (let i = 2; i <= Math.sqrt(number); i++) { if (number % i === 0) return false; } return true; } function displayPrimes(start, end) { for (let i = start; i <= end; i++) { if (isPrime(i)) { console.log(i); } } } // Driver code let num1 = 500; let num2 = 600; displayPrimes(num1, num2); Output503 509 521 523 541 547 557 563 569 571 577 587 593 599Approach 2: Using Array MethodsWe can also use JavaScript array methods to make the code more concise and functional. In this approach, we use the Array.from method to create an array of numbers in the given range, the filter method to keep only the prime numbers, and the forEach method to log them. JavaScript function isPrime(number) { return number > 1 && Array.from( { length: Math.sqrt(number) }, (_, i) => i + 2) .every(divisor => number % divisor !== 0); } function displayPrimes(start, end) { Array.from({ length: end - start + 1 }, (_, i) => i + start) .filter(isPrime) .forEach(prime => console.log(prime)); } // Driver code let num1 = 500; let num2 = 600; displayPrimes(num1, num2); Output503 509 521 523 541 547 557 563 569 571 577 587 593 599 Comment More infoAdvertise with us Next Article How to Generate All Prime Numbers Between Two Given Numbers in Excel? V vkash8574 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Numbers JavaScript-Questions Similar Reads JavaScript Program to Check Prime Number By Creating a Function A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In this article, we will explore how to check if a given number is a prime number in JavaScript by creating a function. Table of Content Check Prime Number using for LoopCheck Prime Number using 2 min read JavaScript Program to Print Prime Numbers from 1 to N A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In this article, we'll explore how to create a JavaScript program to print all prime numbers from 1 to a given number N. To find prime numbers from 1 to N, we need to: Iterate through all numbers 3 min read JavaScript Application To Check Prime and Non-Prime Number Prime numbers have been a fundamental concept in mathematics and computer science. In programming, checking if a number is prime will help to understand loops, conditions, and optimization techniques.What We Are Going to CreateWe will create a simple web application where users can input a number to 4 min read Javascript Program for Count Primes in Ranges Given a range [L, R], we need to find the count of total numbers of prime numbers in the range [L, R] where 0 <= L <= R < 10000. Consider that there are a large number of queries for different ranges.Examples: Input : Query 1 : L = 1, R = 10 Query 2 : L = 5, R = 10Output : 4 2Explanation :P 3 min read How to Generate All Prime Numbers Between Two Given Numbers in Excel? A number greater than 1 with no positive divisors besides 1 and the number itself is referred to as a prime number. In this article, we will be discussing two approaches for generating all prime numbers between two given numbers in excel. In the first approach, we are creating formulas for generatin 3 min read How to Check if the Number is Prime Number in Excel? A prime number is a number that is greater than 1 and has no positive divisor other than 1 and the number itself. Approach: To check whether the number is prime or not we have to divide the number with all the numbers between 2 and the square root of that number. And if it gives remainder 0 in any o 2 min read Like