Commonly Utilized JavaScript Methods Part III - Loops
Welcome back to the Commonly Utilized JavaScript Methods series, this segment is regarding the use of Loops to fully grasp methods and technique pertaining to JavaScript are essential and integral to understand for they provide convenience and simplicity to operation of algorithms to run a program optimally and smoothly.
What are Loops?
Loops essentially iterate data structures such as Array elements, under conditions. Iteration is not to be confused with Iteration method albeit it is what a loop consists of. There are a plethora of types of Loops in JavaScript and the most common ones utilized are the: For Loop, For In Loop, For Of Loop, While Loop and Do...While Loop. The remainder of this article shall breakdown what the basis and purpose of each loop is, benefits of them and how they differ from each other as well as Iteration methods.
What is a For Loop?
A For Loop is made up of 4 major components for it to be ran, which is the: Initialization/initializer: a variable that is typically set via let indicating which index of a string or array to iterate from. For example let i = 0 which essentially mean start from index 0. Next component is the Condition: this is responsible for the validation of whether the test being ran returns true, for cases of being true the loop continues, otherwise the loop breaks. Next is the Final expression which determines whether you loop incrementally or at a decrement via i++ or i-- and lastly is Statement: code to be repeated in the loop as well as result to be displayed via console.log().
Imagery of Breakdown:
Example of For Loop iteration of a string:
Example of For Loop iteration of an array:
Example of Nested For Loop:
Pros and Cons of For Loop:
For Loops are optimal for iteration through elements of an array or string, it can also iterate multidimensional arrays via use of nested loops. The perils of utilizing For Loops it is integral to know the length of the array especially appending .length -1 to avoid returning undefined at the end of iteration. For breaking out of a loop after a specific condition it's important to create a conditional statement with a break in it.
What is the For In Loop?
This loop is utilized to iterate over objects built via array or object constructors, and the properties of the objects via execution of inside block of code are checked once. For In Loops are optimal for the debugging of Objects and data structures that utilize key and pair values instead of arrays. For the iteration and debugging of arrays it is far more better to utilize forEach or for loop instead.
Imagery of Breakdown:
Example of For In Loop to return property, key and value:
Example of For In Loop implementing hasOwnProperty():
Pros and Cons of For In Loop Usage:
As mentioned prior the For In Loop is optimal for the iteration of objects for return key and pairs, it can also add, delete and modify property although this is a capability of the method it can also be deemed as a peril. Due to the fact deleted and modified properties are not revisited until after the iteration is completed. Another disadvantage of the For In Loop is only exclusively for Objects and not other data structures such as Arrays.
What is the For Of Loop?
For of Loop is capability of iteration over strings, arrays as well as arguments, nodeLists, TypedArrays, Maps and Sets. Furthermore, displaying more versatility than other loop counterparts.
For of Loop enables programmers to implement custom hooks/statements to obtain and display distinct properties of objects. Components of the For of Loop includes: variable, iterable and lastly the statement. Variable can be set as const let or var (out-dated), the variable being set is integral to identifying which property is being evaluated and executed during the loop. Iterable is the object that contains the elements/properties begin looped through for instance if an array is declared as const newNumbers = then newNumbers is the iterable.
Imagery of Breakdown:
Example of For Of Loop with Arrays:
Example of For Of Loop with Strings:
Examples of For Of Loop with Arguments Object:
Examples of For Of Loop with Map:
Examples of For Of Loop with Set:
Pros and Cons of For Of Loop Usage:
It is quite evident of the versatility of the For Of Loop usage but a detriment of using this method, is due the inability to determine initiate and where to end the iteration.
What is a While Loop?
The while loop is ran via evaluation of the condition, when the condition is true then the statement or statements are ran. For instances of condition being false then the loop ends.
Imagery of Breakdown:
Comments
Post a Comment