Is there a better way to replicate the label system in rpg maker mv?

Hey guys,

It seems that labels arent an actual thing in javascript, so i tried to replicate it with a loop:

Code:
    for (var i = 0; i = 1;) {
        if (G_dirpressed.search(/[1]/)>=0 && $gameMap.regionId($gamePlayer.x-1, $gamePlayer.y+1) !== G_cantpass) {if ($gamePlayer.canPassDiagonally($gamePlayer.x, $gamePlayer.y, 4, 2)) {dire = "4, 2"; break;}};
        if (G_dirpressed.search(/[3]/)>=0 && $gameMap.regionId($gamePlayer.x+1, $gamePlayer.y+1) !== G_cantpass) {if ($gamePlayer.canPassDiagonally($gamePlayer.x, $gamePlayer.y, 6, 2)) {dire = "6, 2"; break;}};
        if (G_dirpressed.search(/[7]/)>=0 && $gameMap.regionId($gamePlayer.x-1, $gamePlayer.y-1) !== G_cantpass) {if ($gamePlayer.canPassDiagonally($gamePlayer.x, $gamePlayer.y, 4, 8)) {dire = "4, 8"; break;}};
        if (G_dirpressed.search(/[9]/)>=0 && $gameMap.regionId($gamePlayer.x+1, $gamePlayer.y-1) !== G_cantpass) {if ($gamePlayer.canPassDiagonally($gamePlayer.x, $gamePlayer.y, 6, 8)) {dire = "6, 8"; break;}};
        if (G_dirpressed.search(/[2]/)>=0 && $gameMap.regionId($gamePlayer.x, $gamePlayer.y+1) !== G_cantpass) {if ($gamePlayer.canPass($gamePlayer.x, $gamePlayer.y, 2)) {dire = "2"; break;}};
        if (G_dirpressed.search(/[4]/)>=0 && $gameMap.regionId($gamePlayer.x-1, $gamePlayer.y) !== G_cantpass) {if ($gamePlayer.canPass($gamePlayer.x, $gamePlayer.y, 4)) {dire = "4"; break;}};
        if (G_dirpressed.search(/[6]/)>=0 && $gameMap.regionId($gamePlayer.x+1, $gamePlayer.y) !== G_cantpass) {if ($gamePlayer.canPass($gamePlayer.x, $gamePlayer.y, 6)) {dire = "6"; break;}};
        if (G_dirpressed.search(/[8]/)>=0 && $gameMap.regionId($gamePlayer.x, $gamePlayer.y-1) !== G_cantpass) {if ($gamePlayer.canPass($gamePlayer.x, $gamePlayer.y, 8)) {dire = "8"; break;}};
        if (G_dirpressed.length === 1) {$gamePlayer.setDirection(Number(G_dirpressed))}
        break;
    };

dont pay attention to the actual contents of the if statement - The bottomline is: the game checks if the first if statement passes, if it does it executes the code inside and it breaks the loop because it doesnt have to check further, if it doesnt it goes to the next if statement in line, and repeats the process, until it reaches the bottom and it breaks the loop.

My question is: is this even a good way to actually deal with this problem? and are there alternative more efficient methods?

Thanks in advance!
Why are you looping at all?

The way it's set up, the "loop" will only ever go 1 time, so there's no point to looping.

For structuring the "if" statements, you can read more about switch + case statements, it is the same functionality as a bunch of "if" statements but it looks much nicer.
Some programming languages have labels, but it's usage is frowned upon by most programmers for making the code harder to read. There's probably a much better way of implementing what you want, but I would need to know what it is to better suggest something.
That reminds me of a letter I read once to be careful of label/go-to statements, written in.... 1968!
in js label are a function scope (Closures)

PHP:
function goToLabel(label,para) {
    const label1 = function(para){
        // label1 stuff
        console.log('label1: ', para);
    }
    const label2 = function(para){
        // label2 stuff
        console.log('label2: ', para);
       
    }
    const executor = (label === "label1") && label1 || (label === "label2") && label2;
    executor(para);

}
goToLabel('label2',1234);
goToLabel('label1',52548);

@Aloe Guvner, @Hudell
I made a loop because of the way the if statements are set up basically i had to choose between:
Code:
if (G_dirpressed.search(/[1]/)>=0 && $gameMap.regionId($gamePlayer.x-1, $gamePlayer.y+1) !== G_cantpass || G_dirpressed.search(/[1]/)>=0 && $gameMap.regionId($gamePlayer.x-1, $gamePlayer.y+1) !== G_cantpass && $gamePlayer.canPassDiagonally($gamePlayer.x, $gamePlayer.y, 4, 2))
or
Code:
if (G_dirpressed.search(/[1]/)>=0 && $gameMap.regionId($gamePlayer.x-1, $gamePlayer.y+1) !== G_cantpass) {
    if ($gamePlayer.canPassDiagonally($gamePlayer.x, $gamePlayer.y, 4, 2)) {
        dire = "4, 2";
    }
};
and because i wanted the game to not keep checking all the other 'if statements' after one was fully passed, but also wanted to not skip them all if only the first part of the "if statement" was passed;(the "G_dirpressed.search(/[1]/)>=0 && $gameMap.regionId($gamePlayer.x-1, $gamePlayer.y+1) !== G_cantpass" part, but not the "if ($gamePlayer.canPassDiagonally($gamePlayer.x, $gamePlayer.y, 4, 2))" part)
Thats why i put it in a loop.

@Jonforum Hmm, that code seems a bit excessive for what im currently trying, but its still cool for other instances!
I believe the switch system is actually what i was looking for. But lets say i want every case to be ran, until it finds a match with the if statements within; would the only way be this:
Code:
var variable = 1;
Switch (variable) {
case 1:
if (conditions){break};
variable = 2;
case 2:
if (conditions){break};
variable = 3;
case 3:
if (conditions){break};
variable = 4;
case 4:
etc, etc

Original thread: https://forums.rpgmakerweb.com/threads/is-there-a-better-way-to-replicate-the-label-system-in-rpg-maker-mv.96726/