print prime numbers from 1 to 50 in javascript code example

Example 1: javascript write all the prime numbers from 1 to 100

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

function display(n) {
    var arr = [2];
    for ( var i = 3; i < n; i+=2 ) {
        if ( isPrime(i) ) {
            arr.push(i);
        }
    }
    console.log(arr); // use arr result on your own
}

display(100);


//Added Functionality

function prime(n,flag) {
    ( typeof flag === "undefined" || flag === false ) ? flag = false : flag = true;

    function isPrime(num) {
        if ( num === 0 || num === 1 ) {
            return false;
        }
        for ( var i = 2; i < num; i++ ) {
            if ( num % i === 0 ) {
                return false;
            }
        }
        return true;
    }

    if ( flag ) {
        var arr = [2];
        for ( var i = 3; i <= n; i+=2 ) {
            if ( isPrime(i) ) {
                arr.push(i);
            }
        }
        return arr;
    } else {
        return isPrime(n);
    }
}

Example 2: javascript prime numbers up till a certain number

// prime numbers from zero up till a certain number
const sumPrimes = (num) => {  
  let primes=[];
        // computing prime numbers from 0 uptill a number num
    for (let i = 0; i <= num; i++) {
      let flag = 0;
      // looping through 2 to user input number
      for (let j = 2; j < i; j++) {
        if (i % j == 0) {
          flag = 1;
          break;
        }
      }
      // if number greater than 1 and not divisible by other numbers
      if (i > 1 && flag == 0) {
        primes.push(i); // pushing all prime numbers in this array
      }
    }
    return primes;
}
console.log(sumPrimes(10)) // Expected output: 17