why does jsFiddle not like this loop?

111 Views Asked by At

There seems to be some serious problems with the difference between the fiddles and the kind of javascript statements that actually work when ran on servers. Can you guys help me solve this? I've run into this countless times in last few months. Here's what I have:

<script>
int counter;
int retainer = 0;

for (counter = 1; counter < 10; counter++) {
    retainer = retainer + counter;
}
document.write(retainer);
</script>

can someone tell me why this gives me a blank page when run in a browser? does the WRITE() method not working in this way?

2

There are 2 best solutions below

0
lifetimeLearner007 On

There is no int in javascript. All variables are declared using "var"(till ES5) instead irrespective of datatype. Javascript is dynamically typed language. "let" and "const" have been introduced from ES6 onwards

<script>
var counter;
var retainer = 0;

for (counter = 1; counter < 10; counter++) {
    retainer = retainer + counter;
}
document.write(retainer);
</script>
4
Narek Gevorgyan On

are you sure it's a js code, there is no int in javascript as I remember