I have a very simple For Loop in Google Apps Script to check some conditions in a Google Sheet. What I want is to add another condition, if it is met, then I want to skip current iteration and move on to next. This is pretty easy in VBA, but I am not sure how to do it on JavaScript.
Current code:
for (var i=1 ; i<=LR ; i++)
{
if (Val4 == "Yes")
{
// Skip current iteration... <-- This is the bit I am not sure how to do
}
elseif (Val1 == "Accepted" && !(Val2 == "") && !(Val3 == ""))
{
// Do something..
}
else
{
// Do something else...
}
}
I have a very simple For Loop in Google Apps Script to check some conditions in a Google Sheet. What I want is to add another condition, if it is met, then I want to skip current iteration and move on to next. This is pretty easy in VBA, but I am not sure how to do it on JavaScript.
Current code:
for (var i=1 ; i<=LR ; i++)
{
if (Val4 == "Yes")
{
// Skip current iteration... <-- This is the bit I am not sure how to do
}
elseif (Val1 == "Accepted" && !(Val2 == "") && !(Val3 == ""))
{
// Do something..
}
else
{
// Do something else...
}
}
Share
Improve this question
edited Jul 14, 2020 at 17:14
Wicket
38.3k9 gold badges77 silver badges192 bronze badges
asked Sep 20, 2018 at 12:52
Oday SalimOday Salim
1,1474 gold badges28 silver badges51 bronze badges
4
-
1
Read about loop control statements in JavaScript. Namely,
break
andcontinue
. Note that if your for loop has only a single logical statement (your if-else if chain), then simply adding that check and using an empty conditional body will suffice. – tehhowch Commented Sep 20, 2018 at 12:58 - 1 you probably mean continue statement w3schools./js/js_break.asp. If not, then showing the VBA alternative should make the question clear – Slai Commented Sep 20, 2018 at 12:58
- @Slai - thanks for that. Really useful. Please put that in answer and I will mark as correct. – Oday Salim Commented Sep 20, 2018 at 13:10
- This question was solved with a minimum of research. An internet search with generic keywords should have led you to the answer - part of asking a good question. Did you do any research before asking? – tehhowch Commented Sep 20, 2018 at 13:29
1 Answer
Reset to default 12continue
statement can be used to continue to next itteration :
for (var i=1 ; i<=LR ; i++)
{
if (Val4 == "Yes")
{
continue; // Skip current iteration...
}
// Do something else...
}
In your sample case, leaving the if block empty will achieve the same result:
for (var i=1; i <= LR; i++)
{
if (Val4 == "Yes")
{
}
elseif (Val1 == "Accepted" && !(Val2 == "") && !(Val3 == ""))
{
// Do something..
}
else
{
// Do something else...
}
}