My Javascript Journey


Allan Sendagi
Mar 29 · 4 min read

Part 9

source: technical interview notes

So far we have learned about conditionals, where we can add an if statement and have it go one way, or else go the other. We have also seen if else and else if.

But what if we wanted to do something like this:

source: viral loop

..Something where we keep doing it over and over. This is known as looping.

If I can demonstrate:

Let’s say that we wanted to add an exclamation mark at the end of these to-do list items. One way is to do it one by one — which is tedious suppose it was hundreds of lines; or we could do a loop in Javascript.

They are many ways to do a Loop and one of the main ways is a for Loop. Heres the syntax:

for, which is a JavaScript key word means we’re going to do a loop. Then we say: i equals 0 — a random variable i is common to be used in a for loop. It stands for index.

So i starts off with 0. We have a semicolon, which means that’s the end of the expression; and then in the next line it says: i which is at this moment 0, if it is less than the todos.length(5), then run this loop.

When you’re done, do i++ — which means i plus 1.

Now if we wanted to add an exclamation mark on the end of the todos, we can access todos by:

So, now I can just do [i] + “!”

But if I do todos— my todos don’t have this exclamation mark we’ve just console logged.

So let’s do it the proper way.

And what if we want to delete some items from the list?

The while loop

The while loop goes something like this:

We are adding; so every time we’re increasing counter by 1. The loop stops as soon it reaches 10 because after 10, it is no longer right.

do while Loop

So what is the difference here?

The difference is that with a while loop we check the condition first, versus the do while loop that says: “Do the stuff first and then do counterTwo”.

forEach Loop

JavaScript is constantly evolving and ECMAScript 5 was a new standard, that added the forEach method.

And also the action that we want to perform on the todos can be represented in a function and it will give the same results.

See you in part 10

Allan Sendagi

Written by

Technology will save Homo Sapiens from extinction. I document my journey learning these technologies