while Loop
- The
while loop is very similar to a for loop.
- The key difference is that the number of loops before reaching the
while loop's terminating condition is not pre-defined.
- In other words, the number of loops will vary (the
for loop has a set number of loops that can be known beforehand).
- The
while loop syntax is:
while (test condition) {
// JavaScript statements to execute in each loop
// JavaScript statement(s) that impact the test condition (to prevent an infinite loop)
}
- As long as the the test condition evaluates to
true, the loop continues.
- As soon as the test condition evaluates to
false, the loop stops.
- Inside the loop you will need to somehow move the test condition closer to being
false, such as through incrementing (increasing) or decrementing (decreasing) the value with each loop.
- Giving the test condition variable a starting value before the loop begins is also recommended, so that the loop starts properly. Just make sure not to pass a value that stops the loop before it starts!
while Loop Example
console.log('HTML Headings from Largest to Smallest');
let size = 1;
while (size < 7) {
console.log('<h' + size + '>');
size++;
}
// Developer Tools Console output:
// HTML Headings from Largest to Smallest
// <h1>
// <h2>
// <h3>
// <h4>
// <h5>
// <h6>
do-while Loop
- This type of loop is a variation on the
while loop and moves the test condition to the end of the code, rather than having it at the start.
- With a
do-while loop you are guaranteed to have the code execute at least once, which is not guaranteed in a while loop.
- The
do-while loop syntax is:
do {
// JavaScript statements to execute in each loop
// JavaScript statement(s) that impact the test condition (to prevent an infinite loop)
}
while (test condition);
- As long as the the test condition evaluates to
true, the loop continues.
- As soon as the test condition evaluates to
false, the loop stops.
do-while Loop Example
console.log('HTML Headings from Largest to Smallest');
let size = 1;
do {
console.log('<h' + size + '>');
size++;
}
while (size < 7);
// Developer Tools Console output:
// HTML Headings from Largest to Smallest
// <h1>
// <h2>
// <h3>
// <h4>
// <h5>
// <h6>
for-in Loop
- These loops are treated differently than the other loops because they are meant for a very specific scenario: looping through the properties of an Object object (such as an object literal).
- Symbols are omitted from the loop results, because they are private.
- Objects in JavaScript will inherit properties from other objects (remember there is an Object object at the very top of the prototypal hierarchy), so there is a
hasOwnProperty() method on all objects which returns a boolean result if the property passed to the method is actually one of its properties and is not inherited. This is very helpful if you're running this type of loop on anything that is below the Object object.
- The example shows the inclusion of
hasOwnProperty(), although in this case it has no impact on the result.
for-in Loop Example
const sampleObject = {
a : 'Yes',
b : 'No',
c : 'Maybe'
};
for (const theProperty in sampleObject) {
if (sampleObject.hasOwnProperty(theProperty)) {
console.log(`sampleObject.${theProperty} = ${sampleObject[theProperty]}`);
}
}
// Developer Tools Console output:
// sampleObject.a = Yes
// sampleObject.b = No
// sampleObject.c = Maybe