
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Function to add up all the natural numbers from 1 to num in JavaScript
We are required to write a JavaScript function that takes in a number, say num.
Then our function should return the sum of all the natural numbers between 1 and num, including 1 and num.
For example, if num is −
const num = 5;
Then the output should be −
const output = 15;
because,
1+2+3+4+5 = 15
We will use the below formula to solve this problem −
Sum of all natural number upto n =
((n*(n+1))/2)
Example
The code for this will be −
const num = 5; const sumUpto = num => { const res = (num * (num + 1)) / 2; return res; }; console.log(sumUpto(num)); console.log(sumUpto(7)); console.log(sumUpto(45)); console.log(sumUpto(2)); console.log(sumUpto(8)); console.log(sumUpto(99));
Output
And the output in the console will be −
15 28 1035 3 36 4950
- Related Articles
- Count of all N digit numbers such that num + Rev(num) = 10^N - 1 in C++
- Sum of even numbers up to using recursive function in JavaScript
- Smallest possible number divisible by all numbers from 1 to n in JavaScript
- Find the sum of all the numbers from 1 to 200.
- Write the cubes of all natural numbers between 1 and 10 and verify the following statements:(i) Cubes of all odd natural numbers are odd.(ii) Cubes of all even natural numbers are even.
- Program to find all missing numbers from 1 to N in Python
- How to Display all Prime Numbers from 1 to N in Golang?
- Any possible combination to add up to target in JavaScript
- Java Program to Display All Prime Numbers from 1 to N
- Swift Program to Display All Prime Numbers from 1 to N
- Haskell program to display all prime numbers from 1 to n
- Kotlin Program to Display All Prime Numbers from 1 to N
- Compute sum of digits in all numbers from 1 to n
- How to print all the Armstrong Numbers from 1 to 1000 using C#?
- Fill in the blanks:1. The whole number which is not a natural number is........2. The natural number whose predecessor does not exist is........3. There are ........... whole numbers up to 75.4. Predecessor of 2,90,099 is...........5. There are.......... natural numbers up to 80.6. The additive identify for whole numbers is...........

Advertisements