JavaScript Program to Print Prime Numbers from 1 to N Last Updated : 19 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 from 2 to N (since 1 is not a prime number).For each number, check if it is divisible by any number other than 1 and itself.If a number is only divisible by 1 and itself, it is a prime number.Print Prime Numbers from 1 to N using Basic ApproachHere's a simple implementation to find and print prime numbers from 1 to N. The below code consists of two functions: isPrime and printPrimeNumbers. isPrime(num) function: This function checks if a given number num is a prime number or not. It iterates from 2 to the square root of num (inclusive) and checks if num is divisible by any of these numbers. If it is, the function returns false, indicating that num is not a prime number. If num is not divisible by any number in the range, the function returns true, but only if num is greater than 1 (since 1 is not considered a prime number). This is an efficient way to check for primality, as it reduces the number of iterations needed.printPrimeNumbers(n) function: This function prints all prime numbers up to a given number n. It iterates from 2 to n and uses the isPrime function to check if each number is prime. If a number is prime, it is printed to the console. JavaScript function isPrime(num) { for (let i = 2; i <= Math.sqrt(num); i++) { if (num % i === 0) { return false; } } return num > 1; } function printPrimeNumbers(n) { for (let i = 2; i <= n; i++) { if (isPrime(i)) { console.log(i); } } } printPrimeNumbers(100); Output2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97Print Prime Numbers from 1 to N using Array MethodsYou can also use JavaScript array methods to make the code more concise. This code defines two functions, printPrimeNumbers and isPrime, to print all prime numbers up to a given number n. The isPrime function checks if a number is prime, while printPrimeNumbers generates an array of numbers from 1 to n, filters out non-prime numbers, and prints the prime numbers. JavaScript function printPrimeNumbers(n) { Array.from({length: n}, (_, i) => i + 1) .filter(isPrime) .forEach(prime => console.log(prime)); } function isPrime(num) { return num > 1 && Array.from( {length: Math.sqrt(num)}, (_, i) => i + 2) .every(divisor => num % divisor !== 0); } printPrimeNumbers(100); Output3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97The Array.from() method is used to generate an array of numbers from 1 to N, filter() is used to keep only the prime numbers, and forEach() is used to print each prime number. Comment More infoAdvertise with us Next Article C Program to Implement Sieve of Eratosthenes V vkash8574 Follow Improve Article Tags : JavaScript Web Technologies 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 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 JavaScript Program to Display Prime Numbers Between Two Intervals Using Functions 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 straightforwar 2 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 C Program to Implement Sieve of Eratosthenes The Sieve of Eratosthenes is a simple and efficient algorithm for finding all prime numbers up to a specified integer. It was created by the ancient Greek mathematician Eratosthenes and is still used today due to its efficiency when dealing with large numbers. In this article, we will learn how to i 4 min read How to Find Prime Number in Golang? Prime numbers are a fascinating concept in the field of mathematics and computer science. They are numbers that have only two distinct positive divisors: 1 and the number itself. This article will guide you through the process of finding prime numbers in Golang, a statically typed, compiled language 6 min read Like