Home > Article > Web Front-end > How to add from 1 to 50 in javascript
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;}”.
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:
[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!