Why JS lables does'nt work when not used with loops
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?
javascript html ecmascript-6 ecmascript-5
add a comment |
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?
javascript html ecmascript-6 ecmascript-5
add a comment |
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?
javascript html ecmascript-6 ecmascript-5
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
javascript html ecmascript-6 ecmascript-5
asked Nov 12 '18 at 13:49
HemanbabuHemanbabu
13412
13412
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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');
add a comment |
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."
I just asked Why the program throws that error? Are labels only made for loops?
– Hemanbabu
Nov 12 '18 at 14:13
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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');
add a comment |
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');
add a comment |
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');
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');
edited Nov 12 '18 at 13:58
Mamun
25.7k71429
25.7k71429
answered Nov 12 '18 at 13:52
Nina ScholzNina Scholz
178k1491159
178k1491159
add a comment |
add a comment |
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."
I just asked Why the program throws that error? Are labels only made for loops?
– Hemanbabu
Nov 12 '18 at 14:13
add a comment |
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."
I just asked Why the program throws that error? Are labels only made for loops?
– Hemanbabu
Nov 12 '18 at 14:13
add a comment |
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."
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."
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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