Loops in JavaScript work the same way as they do in most other languages, especially those in the C family of languages.
You have traditional condition loops, – while
and do-while
, and a for
counting loop.
while( value != 'C' ) {
// do things
// update value so you don't have an infinite loop
}
do {
// do things
// update value so you don't have an infinite loop
} while( value != 'C' );
for(let i = 0; i < 10; i++) {
// do things for a certain amount of time.
}
These are the most common forms of a loop.
JavaScript also supports several additional loops that are specific to if an object can be iterated over, such as with an array.
These methods, such as .forEach()
, .map()
, etc, will work on the elements of the array, and you pass in a function to that method, in which the data is passed to the function to run.
This tends to be very slow, and not recommended by many people because of the general performance hit. However, if you want to know how, look at the page on JavaScript Functions, which will show you how to create an anonymous function, and how to pass functions as a call back function, by using it as a parameter you pass into a function or method.
Repetition Structures in JavaScript was originally found on Access 2 Learn
One Comment
Comments are closed.