for loop is used to get the array value.
for(var i=0; i<5; i++){
console.log(i);
}
Output:- 0 1 2 3 4
get the Array value
Suppose you have emp array
var emp=["John","Rom","Mathew","Sachin"];
Now, get the Array value
for(var i=0; i < emp.length; i++){
console.log(emp[i]);
}
Output:- John Rom Mathew Sachin
Note:- emp.length is used to count the number of array element.
get the array element value from last element to first element.
var emp=["John","Rom","Mathew","Sachin"];
for(var i=emp.length-1; i >= 0; i--){
console.log(emp[i]);
}
Output:- Sachin Mathew Rom John
Changes the increment
for(var i=0; i<=10; i=i+2)
{
console.log(i);
}
Output:- 0 2 4 6 8 10