Three Types of Loop statements for Javascript As Follows:
- For
- For…in
- For…off
For Loop statements for Javascript:
The syntax for For loop Javascript:
for ([initialization]; [condition]; [final-expression]) statement
initialization
An expression (including assignment expressions) or variable declaration is evaluated once before the loop begins. Typically used to initialize a counter variable. This expression may optionally declare new variables with var or let keywords. Variables declared with var are not local to the loop, i.e. they are in the same scope the for loop is in. Variables declared with let are local to the statement.
The result of this expression is discarded.
condition
An expression to be evaluated before each loop iteration. If this expression evaluates to true, the statement is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the for construct.
final-expression
An expression is to be evaluated at the end of each loop iteration. This occurs before the next evaluation of the condition. Generally used to update or increment the counter variable.
statement
A statement that is executed as long as the condition evaluates to true. To execute multiple statements within the loop, use a block statement ({ … }) to group those statements. To execute no statement within the loop, use an empty statement (;).
More Examples on For Loop statements for Javascript
For in Loop statements for Javascript:
The syntax for For..in loop Javascript:
for (variable in object) {
statement
}
variable
A different property name is assigned to the variable on each iteration.
object
Object whose non-Symbol enumerable properties are iterated over.
Description
A for…in loop only iterates over enumerable, non-Symbol properties. Objects created from built–in constructors like Array and Object have inherited non–enumerable properties from Object.prototype and String.prototype, such as String’s indexOf() method or Object’s toString() method. The loop will iterate over all enumerable properties of the object itself and those the object inherits from its prototype chain (properties of nearer prototypes take precedence over those of prototypes further away from the object in its prototype chain).
Deleted, added, or modified properties
A for…in loop iterates over the properties of an object in an arbitrary order (see the delete operator for more on why one cannot depend on the seeming orderliness of iteration, at least in a cross-browser setting).
If a property is modified in one iteration and then visited at a later time, its value in the loop is its value at that later time. A property that is deleted before it has been visited will not be visited later. Properties added to the object over which iteration is occurring may either be visited or omitted from iteration.
In general, it is best not to add, modify, or remove properties from the object during iteration, other than the property currently being visited. There is no guarantee whether an added property will be visited, whether a modified property (other than the current one) will be visited before or after it is modified, or whether a deleted property will be visited before it is deleted.
Array iteration and for…in
Note: for…in should not be used to iterate over an Array where the index order is important.
Array indexes are just enumerable properties with integer names and are otherwise identical to general object properties. There is no guarantee that for…in will return the indexes in any particular order. The for…in loop statement will return all enumerable properties, including those with non–integer names and those that are inherited.
Because the order of iteration is implementation-dependent, iterating over an array may not visit elements in a consistent order. Therefore, it is better to use a for loop with a numeric index (or Array.prototype.forEach() or the for…of loop) when iterating over arrays where the order of access is important.
Iterating over own properties only
If you only want to consider properties attached to the object itself, and not its prototypes, use getOwnPropertyNames() or perform a hasOwnProperty() check (property Is Enumerable() can also be used). Alternatively, if you know there won’t be any outside code interference, you can extend built-in prototypes with a check method.
Why Use for…in?
Given that for…in is built for iterating object properties, not recommended for use with arrays, and options like Array.prototype.forEach() and for…of exist, what might be the use of for…in at all?
It may be most practically used for debugging purposes, being an easy way to check the properties of an object (by outputting to the console or otherwise). Although arrays are often more practical for storing data, in situations where a key-value pair is preferred for working with data (with properties acting as the “key”), there may be instances where you want to check if any of those keys hold a particular value.
More Examples on Loop For…in statements for Javascript
For..of Loop statements for Javascript:
The syntax for For..off loop Javascript:
for (variable of iterable) {
statement
}
variable
On each iteration, a value of a different property is assigned to a variable. variable may be declared with const, let, or var.
iterable
An object whose iterable properties are iterated.