Loops

1- For Loop (MDN)

Commonly used to iterate over an iterable like arrays, strings when keeping a track of index is needed. 

const myName = 'javascript';

for (let i = 0; i < myName.length; i++) {      //myName.length gives length of string/array
  console.log(myName[i]);
}

 

2- While Loop (MDN)

Commonly used when there is no need to keep track of index.

const myName = 'javascript';

let count = 0;

while (count < myName.length) {
  count++;
}

console.log(myName + ' has ' + count + ' characters.');         //this is known as string concatenation 

 

 

Discussion

Be the first one to comment!