Javascript question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Javascript question

myBlock: { var a = 0; break myBlock; a = 1; } console.log(a); // 0 Why is the result here 0? Shouldnt it be a syntax error?

31st Dec 2021, 3:02 PM
David
David - avatar
9 Answers
+ 16
David because u are using break statement after break statement all code are useless which means a = 1 will not work
31st Dec 2021, 4:43 PM
Pariket Thakur
Pariket Thakur - avatar
+ 5
David Using the break statement with a label is legal as long as it is nested within the referenced labeled statement", in this case, myBlock. more on the subject- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break#break_in_labeled_blocks https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break
31st Dec 2021, 6:20 PM
ODLNT
ODLNT - avatar
+ 2
After break statement the next code will be skipped that's why
1st Jan 2022, 9:08 AM
Muhammad Qasim
Muhammad Qasim - avatar
+ 1
ODLNT thank you for your help, will read it
31st Dec 2021, 6:21 PM
David
David - avatar
+ 1
No problems, I'm glad I could help.
31st Dec 2021, 6:23 PM
ODLNT
ODLNT - avatar
0
Pariket Thakur I get this, but I dont understand why this syntax myBlock: {} is not recognized as a syntax error
31st Dec 2021, 6:13 PM
David
David - avatar
0
myBlock: { a = 0; break; a = 1; } console.log(a); // 0 Try this and you will get a syntax error.
1st Jan 2022, 4:50 AM
DaNil
DaNil - avatar
- 1
You Will Get syntex error
1st Jan 2022, 2:52 PM
Gurukant Pradip Patil
Gurukant Pradip Patil - avatar
- 1
I yet gcjxy vcnbcgxt xh such ucfbtxvkyc
2nd Jan 2022, 4:40 AM
Abd021 Rosnae