Why JS lables does'nt work when not used with loops










1















I wrote a small JS Code here. Which runs without any errors






repeat:
while(true)
console.log('Start');
break repeat;
console.log('End');





But when i don't use the while statement the program throws an error Undefined label repeat






repeat:
console.log('Start');
break repeat;
console.log('End');





Why the program throws that error? Are labels only made for loops?










share|improve this question


























    1















    I wrote a small JS Code here. Which runs without any errors






    repeat:
    while(true)
    console.log('Start');
    break repeat;
    console.log('End');





    But when i don't use the while statement the program throws an error Undefined label repeat






    repeat:
    console.log('Start');
    break repeat;
    console.log('End');





    Why the program throws that error? Are labels only made for loops?










    share|improve this question
























      1












      1








      1








      I wrote a small JS Code here. Which runs without any errors






      repeat:
      while(true)
      console.log('Start');
      break repeat;
      console.log('End');





      But when i don't use the while statement the program throws an error Undefined label repeat






      repeat:
      console.log('Start');
      break repeat;
      console.log('End');





      Why the program throws that error? Are labels only made for loops?










      share|improve this question














      I wrote a small JS Code here. Which runs without any errors






      repeat:
      while(true)
      console.log('Start');
      break repeat;
      console.log('End');





      But when i don't use the while statement the program throws an error Undefined label repeat






      repeat:
      console.log('Start');
      break repeat;
      console.log('End');





      Why the program throws that error? Are labels only made for loops?






      repeat:
      while(true)
      console.log('Start');
      break repeat;
      console.log('End');





      repeat:
      while(true)
      console.log('Start');
      break repeat;
      console.log('End');





      repeat:
      console.log('Start');
      break repeat;
      console.log('End');





      repeat:
      console.log('Start');
      break repeat;
      console.log('End');






      javascript html ecmascript-6 ecmascript-5






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 13:49









      HemanbabuHemanbabu

      13412




      13412






















          2 Answers
          2






          active

          oldest

          votes


















          6














          Basically, it works with code blocks. And while you do not have any block to break, the code throws an error.






          repeat: 
          console.log('Start');
          break repeat;
          console.log('End');








          share|improve this answer
































            -1














            Also, "such a construct is basically a goto in drag." I would prefer to see the logic written with a continue statement, like this:



            while (true) 
            console.log('start');
            if (x == y) continue;
            console.log('foo');
            if (q != z) continue;
            ...



            My point being that "it's still a while loop, just as I said it would be," except that it doesn't always reach the end. When it doesn't reach the end, it always goes back to the top.



            The break statement is most common in the case of while (true) and is, in fact, expected by anyone else who reads your code. The logic runs until some condition is met, at which point it "breaks out of the loop" and goes to the statement following it.



            When you "break like a goto," your logic becomes much more difficult to puzzle-out and to understand. This is a very important consideration. "Write simply and clearly, and in the way that is customarily expected. Don't throw a curve-ball."






            share|improve this answer























            • I just asked Why the program throws that error? Are labels only made for loops?

              – Hemanbabu
              Nov 12 '18 at 14:13










            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263581%2fwhy-js-lables-doesnt-work-when-not-used-with-loops%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            6














            Basically, it works with code blocks. And while you do not have any block to break, the code throws an error.






            repeat: 
            console.log('Start');
            break repeat;
            console.log('End');








            share|improve this answer





























              6














              Basically, it works with code blocks. And while you do not have any block to break, the code throws an error.






              repeat: 
              console.log('Start');
              break repeat;
              console.log('End');








              share|improve this answer



























                6












                6








                6







                Basically, it works with code blocks. And while you do not have any block to break, the code throws an error.






                repeat: 
                console.log('Start');
                break repeat;
                console.log('End');








                share|improve this answer















                Basically, it works with code blocks. And while you do not have any block to break, the code throws an error.






                repeat: 
                console.log('Start');
                break repeat;
                console.log('End');








                repeat: 
                console.log('Start');
                break repeat;
                console.log('End');





                repeat: 
                console.log('Start');
                break repeat;
                console.log('End');






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 12 '18 at 13:58









                Mamun

                25.7k71429




                25.7k71429










                answered Nov 12 '18 at 13:52









                Nina ScholzNina Scholz

                178k1491159




                178k1491159























                    -1














                    Also, "such a construct is basically a goto in drag." I would prefer to see the logic written with a continue statement, like this:



                    while (true) 
                    console.log('start');
                    if (x == y) continue;
                    console.log('foo');
                    if (q != z) continue;
                    ...



                    My point being that "it's still a while loop, just as I said it would be," except that it doesn't always reach the end. When it doesn't reach the end, it always goes back to the top.



                    The break statement is most common in the case of while (true) and is, in fact, expected by anyone else who reads your code. The logic runs until some condition is met, at which point it "breaks out of the loop" and goes to the statement following it.



                    When you "break like a goto," your logic becomes much more difficult to puzzle-out and to understand. This is a very important consideration. "Write simply and clearly, and in the way that is customarily expected. Don't throw a curve-ball."






                    share|improve this answer























                    • I just asked Why the program throws that error? Are labels only made for loops?

                      – Hemanbabu
                      Nov 12 '18 at 14:13















                    -1














                    Also, "such a construct is basically a goto in drag." I would prefer to see the logic written with a continue statement, like this:



                    while (true) 
                    console.log('start');
                    if (x == y) continue;
                    console.log('foo');
                    if (q != z) continue;
                    ...



                    My point being that "it's still a while loop, just as I said it would be," except that it doesn't always reach the end. When it doesn't reach the end, it always goes back to the top.



                    The break statement is most common in the case of while (true) and is, in fact, expected by anyone else who reads your code. The logic runs until some condition is met, at which point it "breaks out of the loop" and goes to the statement following it.



                    When you "break like a goto," your logic becomes much more difficult to puzzle-out and to understand. This is a very important consideration. "Write simply and clearly, and in the way that is customarily expected. Don't throw a curve-ball."






                    share|improve this answer























                    • I just asked Why the program throws that error? Are labels only made for loops?

                      – Hemanbabu
                      Nov 12 '18 at 14:13













                    -1












                    -1








                    -1







                    Also, "such a construct is basically a goto in drag." I would prefer to see the logic written with a continue statement, like this:



                    while (true) 
                    console.log('start');
                    if (x == y) continue;
                    console.log('foo');
                    if (q != z) continue;
                    ...



                    My point being that "it's still a while loop, just as I said it would be," except that it doesn't always reach the end. When it doesn't reach the end, it always goes back to the top.



                    The break statement is most common in the case of while (true) and is, in fact, expected by anyone else who reads your code. The logic runs until some condition is met, at which point it "breaks out of the loop" and goes to the statement following it.



                    When you "break like a goto," your logic becomes much more difficult to puzzle-out and to understand. This is a very important consideration. "Write simply and clearly, and in the way that is customarily expected. Don't throw a curve-ball."






                    share|improve this answer













                    Also, "such a construct is basically a goto in drag." I would prefer to see the logic written with a continue statement, like this:



                    while (true) 
                    console.log('start');
                    if (x == y) continue;
                    console.log('foo');
                    if (q != z) continue;
                    ...



                    My point being that "it's still a while loop, just as I said it would be," except that it doesn't always reach the end. When it doesn't reach the end, it always goes back to the top.



                    The break statement is most common in the case of while (true) and is, in fact, expected by anyone else who reads your code. The logic runs until some condition is met, at which point it "breaks out of the loop" and goes to the statement following it.



                    When you "break like a goto," your logic becomes much more difficult to puzzle-out and to understand. This is a very important consideration. "Write simply and clearly, and in the way that is customarily expected. Don't throw a curve-ball."







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 12 '18 at 14:00









                    Mike RobinsonMike Robinson

                    4,00421021




                    4,00421021












                    • I just asked Why the program throws that error? Are labels only made for loops?

                      – Hemanbabu
                      Nov 12 '18 at 14:13

















                    • I just asked Why the program throws that error? Are labels only made for loops?

                      – Hemanbabu
                      Nov 12 '18 at 14:13
















                    I just asked Why the program throws that error? Are labels only made for loops?

                    – Hemanbabu
                    Nov 12 '18 at 14:13





                    I just asked Why the program throws that error? Are labels only made for loops?

                    – Hemanbabu
                    Nov 12 '18 at 14:13

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263581%2fwhy-js-lables-doesnt-work-when-not-used-with-loops%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo