if you can make a code using some programming languages, you definitely use a loop statement. However, How much do you know about loop statements?
Today we are going to talk about a loop statement!
A loop statement is a control structure that allows you to repeatedly execute a block of code as long as a specified condition evaluates to true.
In plain javascript, there are three loop statements.
for, while, do ... while
Of course there are a few functions that can be replaced a loop statement in javascript.
for ... in, for ... of
There are higher-order functions that can iterate over array.
forEach,
This is commonly used form of a for loop.
for (initialization; condition; increment) {
// code to be executed
}
The while loop executes a block of code as long as the specified condition evaluates to true. it is usually used when the repetition count is unclear.
The do..while loop guarantees the code block before the condition is tested. Therefore the code block executes at least once.
These are control statements used within loops.
// Using break
for (let i = 0; i < 5; i++) {
if (i === 3) {
break;
}
console.log(i); // Outputs 0, 1, 2
}
// Using continue
for (let i = 0; i < 5; i++) {
if (i === 3) {
continue;
}
console.log(i); // Outputs 0, 1, 2, 4
}
Now we are going to know about loop replacement statements.
The for...in and for...of loops in JavaScript are used for iterating over different types of collections, but they serve different purposes and behave.
The for...in loop enumerates properties in the prototype chain that have their [[Enumerable]] attribute set to true.
The for...in loop iterates over the enumerable properties of an object.
const person = { fname: "John", lname: "Doe", age: 25 };
console.log('toString' in person) // true
for (let key in person) {
console.log(key + ": " + person[key]); // Outputs "fname: John", "lname: Doe", "age: 25"
}
Summary for Iteration Protocol!
- the iterable protocol : An object is considered iterable if it implements a method whose key is Symbol.iterator.
Objects that conform to the iterable protocol can be used in for...of loops, spread syntax.
ex) Array, String, Map, Set. not Object!- the iterator protocol : An iterator is an object that provides a next() method.
The for...of loop iterates over the values of iterable objects such as arrays, strings, maps, sets.
const numbers = [1, 2, 3, 4, 5];
for (let number of numbers) {
console.log(number); // Outputs 1, 2, 3, 4, 5
}
Actually, it is highly recommeneded not to use the for ... in loop for arrays. Instead, use the for ... of or the forEach loop.
That's beacuse the array is also object type. so the array can have its propertys.
but commonly the array is for in order data. therefore, it can approch its elements by index.
const arr = [1, 2, 3]
array.customProperty = "I am a custom property";
for (let key in array) {
console.log(key); // "0", "1", "2", "customProperty"
}
console.log(arr.length) // 3
for (let value of array) {
console.log(value); // "1", "2", "3"
}
array.forEach(value => {
console.log(value); // "1", "2", "3"
});