To move to a specific label in JavaScript, you can use the label
statement and the goto
statement.
Syntax:
label:
// code block
goto label;
Example:
gotocheck:
console.log('Hello');
goto end;
gotocheck2:
console.log('World');
goto end;
end:
console.log('End of the program');
In the above example, you can use the goto
statement to jump to a specific label. Here, if you run the code, it will output 'Hello' and 'End of the program' because it jumps to the gotocheck
label, and skips the gotocheck2
label.
However, it is important to note that the use of goto
statement is discouraged in JavaScript as it can lead to spaghetti code and make the code harder to read and maintain. It is generally considered a bad programming practice.
Asked: 2022-08-05 11:00:00 +0000
Seen: 8 times
Last updated: Mar 07 '22