JAVASCRIPT

Jumps


 

In JavaScript, the jump command doesn't exist as a standalone command like it might in some other programming languages. However, you can achieve the functionality of "jumping" to another part of your code using control flow statements like Break and Continue.

 

1. Break 

The break statement ‘jumps out’ of a loop.

 

Syntax:

// break
for (initialization; condition; increment/decrement) {
    // code block
    if (condition_to_exit_loop) {
        break;
    }
    // if condition is false
}

 

Example:

// break
for (let i = 0; i < 5; i++) {
    if (i === 3) {
        console.log("Loop interrupted at i = 3");
        break;
    }
    console.log("i:", i);
}

 

2. Continue

The break statement ‘jumps out’ of a loop.

 

Syntax:

// continue
for (initialization; condition; increment/decrement) {
    // code block
    if (condition_to_skip_iteration) {
        continue;
    }
    // if condition is false
}

 

Example:

// continue
for (let i = 0; i < 5; i++) {
    if (i === 2) {
        console.log("Skipping iteration at i = 2");
        continue;
    }
    console.log("i:", i);
}