I want to do something like this:
for (var i=1; i<=10; i++) {
document.write(i + ",");
}
It shows a result like:
1,2,3,4,5,6,7,8,9,10,
But I want to remove the last ",", and the result should be like this:
1,2,3,4,5,6,7,8,9,10
I want to do something like this:
for (var i=1; i<=10; i++) {
document.write(i + ",");
}
It shows a result like:
1,2,3,4,5,6,7,8,9,10,
But I want to remove the last ",", and the result should be like this:
1,2,3,4,5,6,7,8,9,10
You should check wether you have hit the end of the loop ( i == 10):
for (var i=1; i<=10; i++) {
document.write(i + (i==10 ? '': ','));
}
Here's a fiddle:
You might simply test when generating :
for (var i=1; i<=10; i++) {
document.write(i);
if (i<9) document.write(',');
}
Note that when you start from an array, which might be your real question behind the one you ask, there is the convenient join function :
var arr = [1, 2, 3];
document.write(arr.join(',')); // writes "1,2,3"