Home  >  Article  >  Web Front-end  >  How to add from 1 to 50 in javascript

How to add from 1 to 50 in javascript

WBOY
WBOYOriginal
2021-12-07 10:10:103949browse

In JavaScript, you can use the for loop statement to implement the function of adding from 1 to 50. The for loop can loop and execute the specified code block under specified conditions. The syntax is "for(var i=1;i< ;=50;i ){sum =i;}”.

How to add from 1 to 50 in javascript

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

How to add 1 to 50 in javascript

In JavaScript, you can use a for loop to add 1 to 50 and then output the result . The specific syntax is as follows:

for(var i=1;i<=50;i++){sum+=i;}

The example is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <meta http-equiv="X-UA-Compatible" content="ie=edge">
   <title>Document</title>
</head>
<body>
   <script>
var sum = 0;
for(var i = 1; i <= 50; i ++)
{
sum += i;
}
alert(sum);
   </script>
</body>
</html>

Output result:

How to add from 1 to 50 in javascript

[Related recommendations: javascript learning tutorial

The above is the detailed content of How to add from 1 to 50 in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn