Increment a Variable in Javascript
To increment a variable in Javascript is very easy:
ko=0;
ko++;
document.write(ko);
Which Prints:
1
-We create and name our variable
ko and give it a value of 0.
-We increment it, or add one, with plus plus ++
-We print to the browser with
document.write
Another example in a for loop:
ko=1;
for(h=0;h<50;h++) {
if (h == 2 || h==4) {
ko++;
}
}
document.write(ko);
Which Prints:
3
-We gave our variable
ko a value of 1 to start this time.
-We created a
for loop
-We said
if our variable
h equals 2 or 4, the double vertical bars mean
or, increment
ko.
Free Copy and Paste Javascript Code:
<script type="text/javascript">
<!--
ko=1;
for(h=0;h<50;h++) {
if (h == 2 || h==4) {
ko++;
}
}
document.write(ko);
-->
</script>
Resources:
PRINT to Browser with document.write Command in Javascript
Write a For Loop in Javascript
If, Else If, and Else in Javascript