'Jacascript Loop - Skip A Step based on a variable

I have a For loop with steps where I need to be able to skip a step if a value in a variable equals a certain text.

For example - in the below code I would like to skip step 0 if that var airport does not equal JFK:

var airport = 'JFK';

for (let step = 0; step < 2; step++) {
    rec.setCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'item',
        value: 1261,
        forceSyncSourcing: true
    })
)


Solution 1:[1]

You want to use a conditional statement with continue to terminate that particular iteration if the condition is met.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue

Simple working snippet below. Note loop 0 is missed.

var airport = 'JFK';

for (let step = 0; step < 4; step++) {
   if (airport == 'JFK' && step==0) {continue}
    // code;
    // code;
    
    console.log(`step ${step} executed`);
}

console.log("loop finished");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dave Pritlove