Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Sum of n Natural Numbers in JavaScript

Find the sum of the first n natural numbers in JavaScript with a for loop, while loop, reduce and the n(n+1)/2 formula. Clear explanation and output.

BeginnerJavaScript Loop ProgramsExample 2 of 6
sum-of-n-natural-numbers.js
1// Method 1: Using for loop
2let n = 10;
3let sum = 0;
4
5for (let i = 1; i <= n; i++) {
6 sum += i;
7}
8
9console.log("Sum of first", n, "natural numbers:", sum);
10
11// Method 2: Using formula (n × (n + 1)) / 2
12let num = 10;
13let sumFormula = (num * (num + 1)) / 2;
14console.log("Using formula:", sumFormula);
15
16// Method 3: Using while loop
17let number = 10;
18let total = 0;
19let counter = 1;
20
21while (counter <= number) {
22 total += counter;
23 counter++;
24}
25
26console.log("Using while loop:", total);
27
28// Method 4: Using function
29function sumNaturalNumbers(n) {
30 let sum = 0;
31 for (let i = 1; i <= n; i++) {
32 sum += i;
33 }
34 return sum;
35}
36
37console.log("Using function:", sumNaturalNumbers(10));
38
39// Method 5: Using reduce (functional approach)
40function sumNaturalNumbersReduce(n) {
41 return Array.from({length: n}, (_, i) => i + 1)
42 .reduce((sum, num) => sum + num, 0);
43}
44
45console.log("Using reduce:", sumNaturalNumbersReduce(10));

Sample Output

Sum of first 10 natural numbers: 55
Using formula: 55
Using while loop: 55
Using function: 55
Using reduce: 55

In this one

  • How to build a running total with +=
  • The n(n+1)/2 formula and why it works
  • How Array.from and reduce create and add a list
  • How to compare two methods to test your code

How it works

  1. 1

    sum starts at 0 and i goes from 1 to 10.

  2. 2

    Each round adds i, so sum ends at 55.

  3. 3

    The formula (10 * 11) / 2 also gives 55.

  4. 4

    The while loop, function and reduce all print 55.

Edge cases

n is 0 or negative

The loop returns 0. The formula gives 0 for n = 0 and odd values for negatives.

Check that n is a positive whole number before calculating.

Very large n

A loop to one billion is slow. Huge results lose precision above 2^53.

Use the formula for speed and BigInt for very large exact results.

What's going on

Natural numbers are the counting numbers 1, 2, 3 and so on. This program adds the first n of them. For n = 10 the answer is 1 + 2 + ... + 10 = 55.

Method 1 uses a for loop. sum starts at 0. Each round adds i to sum with sum += i, which is short for sum = sum + i. When the loop ends, sum holds 55.

Method 2 uses the formula n × (n + 1) / 2. The story goes that a young student, Gauss, found it by pairing numbers: 1 + 10, 2 + 9, and so on. Each pair is 11 and there are 5 pairs, so 55. The formula does one calculation no matter how big n is. In JavaScript / already gives a decimal when needed, so you do not need special division.

Method 3 does the loop with while. Method 4 puts the loop in a function. Method 5 uses Array.from({length: n}, (_, i) => i + 1) to build [1, 2, ..., 10] and reduce to add them. reduce walks through an array and keeps a running total.

The loop is clearer to read. The formula is faster for huge n. Both should agree, and checking one against the other is a good test.

Think about bad input. If n is 0 or negative, the loop returns 0. The formula still gives an answer too, but a negative count of numbers makes no sense. Validate n first.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is the sum of the first 5 natural numbers?

Pick an answer to check it.

Question 2

What does sum += i mean?

Pick an answer to check it.

Questions people ask

Which method is best?

The formula is fastest. The loop is easiest to understand at first.

What does _ mean in (_, i) => i + 1?

It names a parameter you do not use. Here only the index i matters.

Is 0 a natural number?

In this program, natural numbers start at 1.