Say i have conditions setup like this:
if (condition1) {
for (var i in objx) {
if (objx[i] == 3) {
//go to next else if
}
}
} else if(condition2) {
...
}
so there are two ways it should reach to condition2 if statement:
- when condition1 is false
- something inside loop isn't as needed.
i could have put whole if else inside loop but there are more conditions and this will just make code harder to read and follow. Secondly, i considered to finally try out goto and label method, but according to someone that according to this .12 goto will be obsolete.
Ideally: I'd have preferred a way to use break, and continue somehow in if else statements.
Say i have conditions setup like this:
if (condition1) {
for (var i in objx) {
if (objx[i] == 3) {
//go to next else if
}
}
} else if(condition2) {
...
}
so there are two ways it should reach to condition2 if statement:
- when condition1 is false
- something inside loop isn't as needed.
i could have put whole if else inside loop but there are more conditions and this will just make code harder to read and follow. Secondly, i considered to finally try out goto and label method, but according to someone that according to this http://es5.github.io/#x12.12 goto will be obsolete.
Ideally: I'd have preferred a way to use break, and continue somehow in if else statements.
Share Improve this question asked Aug 9, 2015 at 13:41 Muhammad UmerMuhammad Umer 18.2k24 gold badges110 silver badges176 bronze badges 7- 2 Put the code in a function and call it when you need it. – Pointy Commented Aug 9, 2015 at 13:44
- 1 The only possible way of doing this would be a GOTO statement I suppose but that is considered as bad programming practice. If you could elaborate your objective a little more then we could suggest you alternatives. – Swastik Padhi Commented Aug 9, 2015 at 13:45
- 1 @Pointy I guess he wants to keep the code as it is and just transfer the control. – Swastik Padhi Commented Aug 9, 2015 at 13:45
- I'm quite interested in a practical use for such a logic block, because if you didn't need to jump from the inner you would've done what @Ori Drori answered and moved the bool outside the ifelse block. – Kevin P. Commented Aug 9, 2015 at 13:48
- 1 this is what i am doing: if there is data in local storage search through it, see if any of it's too outdated, if not then return this, otherwise if it's outdated and or if there is no data in local storage prepare to make ouath request and get data. – Muhammad Umer Commented Aug 9, 2015 at 13:58
3 Answers
Reset to default 2Use a flagging variable, like this
// Initial value is `true` because, if `condition1` is falsy, we still have to
// evaluate `condition2` by default.
var flag = true;
if (condition1) {
// `condition1` is truthy, skip `condition2`
flag = false;
for (var i in objx) {
if (objx[i] == 3) {
// We need to check the second condition
flag = true;
// skip rest of the loop
break;
}
}
}
// Evaluate condition2 only if `flag` is set
if (flag && condition2) {
...
}
A slightly other solution with switch
:
switch (true) {
case condition1:
var doBreak = true;
for (var i in objx) {
if (objx[i] == 3) {
// go to condition2
doBreak = false;
break;
}
}
if (doBreak) {
break;
}
case condition2:
// ...
}
You can't go from one if (else if) block to another, but you can move the internal check to the 1st if. condition1 && conditionFunc(objx) - means that if condition1 is true, run the func, and if it is true as well do this if block, if not it will move to the next else if.
function conditionFunc(objx) {
for (var i in objx) {
if (objx[i] == 3) {
return false;
}
}
return true;
}
if (condition1 && conditionFunc(objx)) {
} else if (condition2) {
...
}