Basics
Challenges
Classes
Debugging
Events
External Files
Flow Control
Forms
Functions
Html Elements
Installation
Interfaces
Keywords
Modules
Namespaces
Operators
Reference Files
String
Types
Do While - executes at least once - test after the loop
let counter: number = 1;
let output: string = "";
do
{
output += "<div>" + counter + "</div>";
counter++;
}
while (counter < 6);
While - tests first and then executes
while (counter < 6)
{
output +="<div>" + counter + "</div>";
counter++;
}
For In - loops thru items in a collection and returns the INDEX
There is no for each
in javascript - at least not any more.
It was deprecated as of ES4. Use for in
instead.
Both for..of
and for..in
statements iterate over lists. The values iterated on are
different though. for..in
returns a list of keys (indexes) on the object being iterated,
whereas for..of
returns a list of
values of the numeric properties of the object being iterated.
for in
// show the indexes
let colors : object = [ "Red", "Blue", "White", "Green" ];
let output : string = "";
for (let i in colors)
{
output += i + "<br />";
}
for of - loops through the values of an iterable object.
// show the values
let colors = ["Red", "Blue", "White", "Green"];
let output: string = "";
for (let i of colors)
{
output += i + "<br />";
}
For - loops thru block of code n number of times
// ITI = Initialize. Test, Increment
var output : string = "";
for (var i = 0; i < 5; i++)
{
output += "The number is " + i + "<br />";
}