JavaScript Program to Display Prime Numbers Between Two Intervals Using Functions Last Updated : 19 Feb, 2024 Comments Improve Suggest changes 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 JavaScript Program to Display Prime Numbers Between Two Intervals Using Functions 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 Find difference between count of Prime and Composite in given Range Given two integers L and R, the task is to find the value of (number of composites - number of primes) that lie in the range [L, R]. Note: 1 is considered neither a prime nor a composite number. Examples: Input: L = 2, R = 10Output: 1Explanation: The composite numbers in the range are 4, 6, 8, 9, 10 5 min read How to Print Prime Numbers in MS SQL Server? In this article, we are going to print Prime numbers using MS SQL. Here we will be using 2 while loops statement for printing prime numbers. Steps 1: First we will DECLARE a variable I with initial value 2. Query: DECLARE @I INT=2Step 2: Then we will DECLARE a variable PRIME with an initial value of 3 min read How to Teach Prime Numbers to Kids Prime Numbers are those numbers that have only two factors: 1 and itself. A prime number is completely divisible by 1 and itself leaving zero remainder. Real-life applications of prime numbers are spread in various fields such as cryptography, computer science, and number theory. Prime numbers help 7 min read Find Sum of numbers in given Range when numbers are modified as given Find the sum of all the numbers between the range l and r. Here each number is represented by the sum of its distinct prime factors. Examples: Input: l = 1, r = 6Output: 17Explanation: For 1, sum of prime factors = 0For 2, Sum of Prime factors = 2For 3, Sum of Prime factors = 3For 4, Sum of Prime fa 7 min read Like