Asked 7 years ago
14 Jan 2017
Views 1098
jabber

jabber posted

break nested loop in JavaScript ?

how to break nested loop in JavaScript ?


for (var x = 0; x < data.length; x++)
{
    for (var x = 0; x < data.length; x++)
    {
 
     break();
     }
}
Rasi

Rasi
answered Nov 30 '-1 00:00


for (var  x = 0; x < 5; x++)
{
	for (var y = 0; y < 4; y++)
	{
		console.log(x+'----'+y);
		break  ;
	}
}

i put simply break at inner loop . here two loop which should be run normally for 20 iteration but i put break at inner loop so it will beak inner loop but run outer loop so it will give us five iteration output .

if you want quit both iteration than use label with break



mainLoop : 
for (var  x = 0; x < 5; x++)
{
	for (var y = 0; y < 4; y++)
	{
		console.log(x+'----'+y);
		break mainLoop  ;
	}
}


break label ; means it break iteration and go to label . here we put mainLoop label which outside of outerloop so it beak both loop and go out from iteration

Post Answer