java script while loop code example

Example 1: javascript while

var i=0;
while (i < 10) {
	console.log(i);
	i++;
}
//Alternatively, You could  break out of a loop like so:
var i=0;
while(true){
	i++;
	if(i===3){
		break;
	}
}

Example 2: example of while loop in javascript with array length

var soccerTeams = ['Barca','Madrid','Gunners','City','PSG']
var x=0;

while( x <= soccerTeams.length) {
  console.log(soccerTeams[x])
  x++;
}

======== output ===========
Barca
Madrid
Gunners
City
PSG
undefined
5

Example 3: do while javascript

do {
  //whatever
} while (conditional);

Tags:

Php Example