-1

What is the difference between 2 code (loop for javascript) ?

<script>
for(x=0;x<5;x++)
{
alert(x);
}
</script>

AND

<script>
for(x=0;x<5;++x)
{
alert(x);
}
</script>

i tested the result will be same.

But in w3school advice like this

for(x=0;x<5;x++)

Could you please tell me. i can use for(x=0;x<5;x++) and for(x=0;x<5;++x) too ?

mongmong seesee
  • 987
  • 1
  • 13
  • 24

2 Answers2

1

In for loop it's same. Difference is in example like this:

var x = 5;
alert(x++); // alerts 5 and then increments

.

var x = 5;
alert(++x); // increments and then alerts 6
Bozidar Sikanjic
  • 717
  • 5
  • 12
0

The i++ and ++i differ only in the value they return, which is ignored when written in the 3rd field of the for loop. In this case, they're exactly the same thing.

The difference between them can be found here.

Community
  • 1
  • 1
SlySherZ
  • 1,631
  • 1
  • 16
  • 25