loops in modern javascript

Muhammad Ahasanur Rahman
3 min readFeb 22, 2021

this article is about different kinds of loops in javascript language.

sweet & delicious biscuits
Photo by Diana Polekhina on Unsplash

Who doesn’t like biscuits because they are delicious. Mr javascript also likes to taste biscuits. He likes to eat biscuits one by one to feel the taste of above biscuits. He will share you how he will taste biscuits.

Ok, so his biscuits in a container called plate and he will count how many biscuits is there. Then he will start to eat every biscuit and will enjoy the time. As he likes to eat biscuits very much he will eat every piece of biscuits using his right hand.

let plateContainer = ['bsc1', 'bsc2', 'bsc3', 'bsc4', 'bsc5', 'bsc6', 'bsc7']; // a plate where have many biscuitsfor(let biscuit = 1; biscuit <= plateContainer.length; biscuit = biscuit + 1){let biscuitNum= plateContainer[biscuit -1]; // as javascript start count from 0 so "[biscuit-1]"console.log("currently eating ",biscuitNum);}

Mr javascript changed his mind. He now thought that at first he will taste a biscuit from plate. If he finds the biscuit delicious and sweet then he will eat another biscuits. Let’s see how he does that.

let plateContainer = ['bsc1', 'bsc2', 'bsc3', 'bsc4', 'bsc5', 'bsc6', 'bsc7']; // a plate where have many biscuitslet biscuit = 1;let isSweet = false;do{let biscuitNum= plateContainer[biscuit -1];console.log("Now eating ",biscuitNum);isSweet = true; // if this value is false then loop will break in while conditionbiscuit = biscuit + 1; //for getting next biscuit form plate}while( biscuit <= plateContainer.length && isSweet); // if this condition return true then he will go for another biscuit.

Suddenly one of his friend messaged on facebook . It was urgent and he started to type with his right hand and continued to eat using his left hand. Let’s see how he uses of his left hand

let plateContainer = ['bsc1', 'bsc2', 'bsc3', 'bsc4', 'bsc5', 'bsc6', 'bsc7']; // a plate where have many biscuitslet biscuit = 1;let numberOfBiscuits = plateContainer.length;while(biscuit <= numberOfBiscuits){  //checking if there is any biscuit is not visited in platelet biscuitNum = plateContainer[biscuit-1];biscuit = biscuit + 1; // can be written biscuit += 1console.log("Visited biscuit and eaten ", biscuitNum);}

Mr javascript has a robot name “Valo robot”. When he feels tired he commands his Mr. valo robot to help him eating biscuits. Mr javascript just open his mouth and valo robot brings biscuits towards the mouth of him. Let’s see this procedure ..

let plateContainer = ['bsc1', 'bsc2', 'bsc3', 'bsc4', 'bsc5', 'bsc6', 'bsc7']; // a plate where have many biscuitsfor(let biscuit of plateContainer){  //checking if there is any biscuit left in plate which is not visited yetlet biscuitNum = biscuit;console.log(" Eaten  ", biscuitNum);}

Sometimes Mr javascript doesn’t like to eat every biscuit at a time.Then he does like this

let plateContainer = ['bsc1', 'bsc2', 'bsc3', 'bsc4', 'bsc5', 'bsc6', 'bsc7']; // a plate where have many biscuitslet biscuit = 0;let numberOfBiscuits = plateContainer.length;let count = 1;for( ;biscuit < numberOfBiscuits; ){if(count > 3){console.log("Won't eat any biscuit today!!! ");break; //has another keyword "continue" to continue a process above this statement}console.log("Currently eating ", plateContainer[biscuit], " !!");count += 1;biscuit++;}//syntax of typical for loop//for( initialization; checking condition; variable to run process){//instructions to execute// }

Without these Mr javascript have many specialized methods to capture every biscuit in a plate such as: forEach(), map(), filter() etc. We will discuss those in another article. Please run every code to your code editor for better understanding.

If you have anything to say or suggestions can tell in comments. Feel free to comment your thoughts below this article.

If you still don’t understand about loops then google it. There are a lot of helping resources which may be helpful to you. Thank u .

--

--