in ,

Javascript Notes – Loops

Javascript logo

DRY code is an acronym for Don’t Repeat Yourself. It describes the philosophy of writing code intelligently with loops instead of typing out repetitive code over and over.


while(someCondition ) { 
//run some code

Adding to the count

For example if we want to run a simple loop that counts to ten we could do the following:

What if we wanted to add more than 1 to the count, what if we wanted to add 2?

We set count+=2

Printing each character in a string



This takes advantage of the .length property and another big concept which is the [indexwhich will select whichever character or object from an array that is defined by the brackets.

Some additional examples:


for(init; condition; step){

// run some code


Here are some samples of basic for loops:


forEach loops are a method of arrays and are therefore covered in the Arrays Post