Counting number from 0 to 9 with Javascript

In this tutorial we will write a simple javascript program to count from 0 to 9 but before we start to write the program, we will take a look at a few ingredients that we need in our program.

Variable :

Variable can be anything ranging from number to string, and in this tutorial we are going to use number.

Number can be either float or integer. Float has the decimal places which is something similar to this, 12.336 or 1.2 while a whole number is something like 5 or 6. In our program we are using the whole number.

When we assign a value to a number we will use the statement like this

var salary = 3000;

Notice that we need a “;” to signal to the javascript engine that it is the end of the entire statement. In the latest version of javascript we do not even need “;” anymore if there is only one statement on that row, we only need to insert “;” if we have more than one statement in a row, for example, var x = 0; x++;

When we declare a variable outside a function, loop or {} then that variable will become a global variable and thus we will be able to access it within a function or a loop. For example,

Loop can be either a for loop or a while loop, we will use the while loop in this program, the while loop will loop through as many times before the condition has been met which the loop will then terminate. If there is only one statement within the while loop then we do not need to have an open { and close } but if there are more than one statement within the while loop then we will use the {} brackets and in the below program we do need the brackets.

Comparison Operator:

These are the operators we will use to compare two variables and a boolean variable will be returned depends on whether the statement is true or false. For example if x < 10 (x is less than 10) then the True boolean object will be returned or else that statement will return False.

Here are all the comparison operators in Javascript.

== (Equal), for example if var a = 1 and var b = 3 then they are not equal and thus a == b will return false.

!= (Not Equal), take the above variable, a !=b will return true.

> (Greater Than) a > b will return false.

< (Smaller Than) a < b will return true.

>= (Greater or Equal) a >= b will return false.

<= (Smaller or Equal) a <= b will return true.

Arithmetic Operators:

These is just like the normal arithmetic operators that we use to add up, divide two number in everyday life, They are as follow…

Addition (+)

Subtraction (-)

Multiplication (*)

Division (/)

Modulus (%)

++ (Increment)

— (Decrement)

We will use the increment operator in our program,

var a = 0;
a++; // a is now 1

console object:

When we working with browser or in this case the eclipse IDE we can use the console.log method to print out a variable or string on the console panel.

We will further explore the javascript method in the coming chapter.

The {} :

This is the method javascript uses to group up a group of statement, mostly use by function or loop, for example

while (i < 9) {
//statements go here
}

All right, that is about it, lets look at the simple program which will print the increment of a number on the console for 10 times (includes the original number).

/**
* Print number a few times on the console
*/
var i = 0;
while(i < 10) {
console.log(i);
i++;
}

From what we have learned we have written the above simple program which will print the increment of a number (include the original number) 10 times on the console.

0
1
2
3
4
5
6
7
8
9

In this tutorial we have learned loop, operator and variable, hope you do enjoy this tutorial and it is time to move to the next chapter.