doSomething();
doSomething();
doSomething();
doSomething();
doSomething();
doSomething();
doSomething();
doSomething();
doSomething();
doSomething();
doSomething();
doSomething();for (var i =0; i < 8; i++) {
doSomething()
}var count = 10;
while(count > 0){
count--
doSomething();
}var number = 1; // Initializer
while(number <= 4){ // Conditional Expression
// Loop Body
console.log("Repeat this #" + number + " four times");
number++; // Iteration (or Change)
}for(var i = 0; i < 10; i++){
console.log(i);
}1.
2.
3.
4.
function upToFive(){
for(var i = 1; i<=5; i++){
console.log(i);
}
}
upToFive();i <= 5
The WHILE loop is normally used when you need to repeat something until a given condition is met
var badInput = true;
while(badInput){
//ask user for input
alert('Please Submit Valid Credentials');
badInput = checkInput(); // returns true or false
}The FOR loop is normally used when you need to iterate a given amount of times
for(var i = 0; i < 100; i++){
...//do something for a 100 times.
}
Overall do not worry about which one to use
var firstArray = [0,1,"two"];[55,22,"hello"][10, 40, 13, 20, 8];
var myArray = [0,true, 4+5, "no", undefined, null, {}];
Arrays are sequences of values
Access Indexes in Arrays using bracket notation
myArray[1]; // returns trueAn Array uses square brackets and surrounds values separated by commas
Create a new empty array
var myArray = [];Create an array with values
var myArray = ["Go", 22];.length: returns the length of the array starting at the count of 1. *this is a property*
myItems.length // returns 2var myItems = ["Eggs", "Ham"];.push(item): called directly on your array and accepts an argument with a single value. The value will be added at the last index (back) of the array.
myItems.push("Chips");myItems; // ["Eggs", "Ham", "Chips"]var myItems = ["Eggs", "Ham"];myItems.pop(); // returns "Ham"myItems.indexOf("Ham", 0) // returns 1