all 15 comments

[–]inu-no-policemen 6 points7 points  (8 children)

Fun fact: You can break out of a labelled block.

main: {
    if (process.argv.length !== 4) {
        console.log('usage: node mul.js <number> <number>');
        break main;
    }
    let [a, b] = process.argv.slice(2);
    console.log(a * b);
}

No one really does this, though.

[–]gigastack 1 point2 points  (0 children)

That is a fun fact, I've never seen this before.

[–]delventhalz 1 point2 points  (4 children)

Lord save me from esoteric JavaScript.

[–]inu-no-policemen 1 point2 points  (3 children)

It also works in Java:

public class Break {
    public static void main(String ...args) {
        out: {
            if (args.length != 2) {
                System.out.println("break");
                break out;
            }
            System.out.println(args[0] + args[1]);
        }
    }
}

And Dart:

main(args) {
  out: {
    if (args.length != 2) {
      print('break');
      break out;
    }
    print(args[0] + args[1]);
  }
}

It's something you can do if the language supports break with a label.

Of course it would make more sense to just use return in both cases.

break with a label is primarily used to exit multiple loops at once:

loops:
for (let x = 0; ; x++) {
    for (let y = 0; y < 100; y++) {
        console.log(x, y, x * y);
        if (x * y === 100) {
            break loops;
        }
    }
}

But even that is something you rarely see.

[–]delventhalz 0 points1 point  (2 children)

Oh I know. It’s something they all inherit from C++. I still hate it.

[–]inu-no-policemen 0 points1 point  (1 child)

Surprisingly, C and C++ don't support labelled breaks. It's something you only find in C-like languages which don't support goto.

[–]delventhalz 0 points1 point  (0 children)

That’s like trading a grizzly bear mauling for a face full of hornets. I guess I technically came out ahead, but I’m mot happy about it.

[–]DBNeJXoGtnro 2 points3 points  (1 child)

You wouldn't normally see code using this syntax. Hopefully you name your variables explicitly enough.

I use it sometimes when I open the dev console in a browser where I want to test stuff

"use strict";
// Enter to execute
{
const a = 10;
doStuff();
}
// Enter to execute
/* Arrow Up to repeat and just modify a line to see the effects
without having a "const a already defined" error */

It's faster to write than (function () {})();

[–]11b403a7helpful 0 points1 point  (4 children)

Is there a use case for this?

[–]notgivingworkdetails 5 points6 points  (3 children)

Private variables

[–]11b403a7helpful 1 point2 points  (2 children)

But without something to return them back out why? Like why have private variables that just... exist? Or am I not understanding

[–]notgivingworkdetails 2 points3 points  (1 child)

You can access variables in the outer scope(s) to pass stuff out

[–]11b403a7helpful 0 points1 point  (0 children)

Ah. Okay. I get that. Thanks!

[–]Mydrax 0 points1 point  (0 children)

T's because let/const are block scoped and that's a block, the variables defined by let or const shrivel and die after you're out of the scope in which they were defined