This week I am going in depth with Javascript. Javascript is known as the defacto browser programming language. If you just know Javascript, you can build fully functioning apps by learning the MEARN stack. Which consist of MongoDB , Express , Angular , React , and Node.

After learning Ruby for the past 6 weeks . I have come to realize , that Javascript is not so friendly with helping you iterate through , arrays and hashes compared to Ruby. In Ruby I was overwhelmed with methods to help iterate over arrays ,hashes , and objects with methods such as as .each , .uniq , .sum , .map , and etc.

I’m almost certain in fully functioning apps or even working with the DOM. We are going to iterate over elements at some point as Programmers. Here are some Iterators that i found to be helpful.

For Loop

for (statement 1; statement 2; statement 3) {
// code block to be executed
}

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.

The While Loop

This is the simplest looping statement provided by JavaScript.

The while loop loops through a block of code as long as the specified condition evaluates to true. As soon as the condition fails, the loop is stopped. The generic syntax of the while loop is:

while(condition) {
// Code to be executed
}

The following example defines a loop that will continue to run as long as the variable i is less than or equal to 5. The variable i will increase by 1 each time the loop runs:

Example

Do / While Loop

The do...while statement creates a loop that executes a specified statement until the test condition evaluates to false. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once.

Example

The For/Of Loop

The JavaScript for/of statement loops through the values of an iterable objects

for/of lets you loop over data structures that are iterable such as Arrays, Strings, Maps, NodeLists, and more.

The for/of loop has the following syntax:

for (variable of iterable) {
// code block to be executed
}

variable — For every iteration the value of the next property is assigned to the variable. Variable can be declared with const, let, or var . (Highly recommend not to use var! )

iterable — An object that has iterable properties

forEach Loop

array.forEach(function(currentValue, index, arr), thisValue)

function(currentValue, index, arr) Required. A function to be run for each element in the array.

Function arguments: ArgumentDescription currentValue (Required).

The value of the current element index (Optional).

The array index of the current element arr (Optional).

thisValue (Optional) . A value to be passed to the function to be used as its “this” value.

If this parameter is empty, the value “undefined” will be passed as its “this” value

The For/In Loop

The JavaScript for/in statement loops through the properties of an object:

Example

Hope those loops will help you make your life easier . Here are some sources for more practice and different ways of looping below.

Sources

Learning new things everyday and sharing them with them world. Software Engineering , Muay Thai , and Soccer are my passions.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store