{"id":140605,"date":"2018-08-30T06:50:02","date_gmt":"2018-08-30T06:50:02","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=140605"},"modified":"2018-08-30T06:50:02","modified_gmt":"2018-08-30T06:50:02","slug":"label-in-javascript","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/label-in-javascript\/","title":{"rendered":"Label in Javascript"},"content":{"rendered":"<p>While working with loops you might come across a situation where you want to exit a loop. Of course you can use break and continue statement to achieve this.But if you have multi level loops i.e loop within loop, and you want to exit through the outer level loop?.Break and continue statements are used to exit the current loop only , and let me tell you javascript do not have <em><strong>goto<\/strong><\/em> statement. Here comes <strong>Label<\/strong> to rescue.<\/p>\n<p>Label statement works as an identifier to identify a loop.You can use label statement with break and continue to break or continue the execution.We can create a label by using label name followed by colon.<br \/>\n<strong>Syntax<\/strong><br \/>\nnameofthelabel:<\/p>\n<p>A working example always speaks better than theoretical definition.So lets understand with an example.<\/p>\n<pre class=\"brush:js\">var x, y;\r\n\r\nouterloop: \/\/ label named as outerloop\r\nfor (x = 0; x &lt; 4; x++) \r\n{\r\n    innerloop: \/\/ label named as innerloop\r\n    for (y = 0; y &lt; 3; y++) \r\n    {\r\n       if (x == 1 &amp;&amp; y == 1)\r\n          continue innerloop;\r\n\r\n       if (x == 2 &amp;&amp; y == 2)\r\n          continue outerloop;\r\n\r\n\r\n       console.log('x = ' + x + ', y = ' + y);\r\n     }\r\n}<\/pre>\n<p>Output of the above script would be as follows.<\/p>\n<p>x = 0, y = 0<br \/>\nx = 0, y = 1<br \/>\nx = 0, y = 2<br \/>\nx = 1, y = 0<br \/>\nx = 1, y = 2<br \/>\nx = 2, y = 0<br \/>\nx = 2, y = 1<br \/>\nx = 3, y = 0<br \/>\nx = 3, y = 1<br \/>\nx = 3, y = 2<\/p>\n<p>As you can see x=1, y=1 and x=2 ,y=2 output is missing in the output.<br \/>\nIn the script i have labelled outer loop as outerloop and inner loop as innerloop.<\/p>\n<pre class=\"brush:js\">if (x == 1 &amp;&amp; y == 1)\r\n   continue innerloop;\r\n<\/pre>\n<p>Here when x and y equals to 1 we stopped the further execution and continued the innerloop using &#8220;continue innerloop&#8221; statement.That is why we do not see x=1,y=1 in the output.<\/p>\n<pre class=\"brush:js\">if (x == 2 &amp;&amp; y == 2)\r\n   continue outerloop;\r\n<\/pre>\n<p>Here when x and y equals to 2 we stopped the further execution and continued the outerloop using &#8220;continue outerloop&#8221; statement.That is why we do not see x=2,y=2 in the output.<\/p>\n<p>&nbsp;<\/p>\n<p>I hope using above example i have explained how label works.You can also use break statement .<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While working with loops you might come across a situation where you want to exit a loop. Of course you can use break and continue statement to achieve this.But if you have multi level loops i.e loop within loop, and you want to exit through the outer level loop?.Break and continue statements are used to <a href=\"https:\/\/webkul.com\/blog\/label-in-javascript\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[198,79],"tags":[7413,789,7412,7414],"class_list":["post-140605","post","type-post","status-publish","format-standard","hentry","category-javascript","category-jquery","tag-javascript-label","tag-label","tag-label-javascript","tag-loops"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Label in Javascript - Webkul Blog<\/title>\n<meta name=\"description\" content=\"Label statement works as an identifier to identify a loop.You can use label statement with break and continue to break or continue the execution\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/label-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Label in Javascript - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Label statement works as an identifier to identify a loop.You can use label statement with break and continue to break or continue the execution\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/label-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-08-30T06:50:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ajay\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ajay\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/label-in-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/label-in-javascript\/\"},\"author\":{\"name\":\"Ajay\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/ae31c92302339c5fb809cdf5e2ed1e6f\"},\"headline\":\"Label in Javascript\",\"datePublished\":\"2018-08-30T06:50:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/label-in-javascript\/\"},\"wordCount\":277,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"javascript label\",\"Label\",\"label javascript\",\"loops\"],\"articleSection\":[\"JavaScript\",\"jQuery\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/label-in-javascript\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/label-in-javascript\/\",\"url\":\"https:\/\/webkul.com\/blog\/label-in-javascript\/\",\"name\":\"Label in Javascript - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2018-08-30T06:50:02+00:00\",\"description\":\"Label statement works as an identifier to identify a loop.You can use label statement with break and continue to break or continue the execution\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/label-in-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/label-in-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/label-in-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Label in Javascript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/ae31c92302339c5fb809cdf5e2ed1e6f\",\"name\":\"Ajay\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b3fb9dd60ea66e716d97fb8685e9d8ed0c401990e2f86ae1e54dc3adcbdfaa19?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b3fb9dd60ea66e716d97fb8685e9d8ed0c401990e2f86ae1e54dc3adcbdfaa19?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Ajay\"},\"sameAs\":[\"http:\/\/webkul.com\"],\"url\":\"https:\/\/webkul.com\/blog\/author\/ajay\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Label in Javascript - Webkul Blog","description":"Label statement works as an identifier to identify a loop.You can use label statement with break and continue to break or continue the execution","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/label-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Label in Javascript - Webkul Blog","og_description":"Label statement works as an identifier to identify a loop.You can use label statement with break and continue to break or continue the execution","og_url":"https:\/\/webkul.com\/blog\/label-in-javascript\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2018-08-30T06:50:02+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Ajay","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Ajay","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/label-in-javascript\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/label-in-javascript\/"},"author":{"name":"Ajay","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/ae31c92302339c5fb809cdf5e2ed1e6f"},"headline":"Label in Javascript","datePublished":"2018-08-30T06:50:02+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/label-in-javascript\/"},"wordCount":277,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["javascript label","Label","label javascript","loops"],"articleSection":["JavaScript","jQuery"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/label-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/label-in-javascript\/","url":"https:\/\/webkul.com\/blog\/label-in-javascript\/","name":"Label in Javascript - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2018-08-30T06:50:02+00:00","description":"Label statement works as an identifier to identify a loop.You can use label statement with break and continue to break or continue the execution","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/label-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/label-in-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/label-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Label in Javascript"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/ae31c92302339c5fb809cdf5e2ed1e6f","name":"Ajay","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b3fb9dd60ea66e716d97fb8685e9d8ed0c401990e2f86ae1e54dc3adcbdfaa19?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b3fb9dd60ea66e716d97fb8685e9d8ed0c401990e2f86ae1e54dc3adcbdfaa19?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Ajay"},"sameAs":["http:\/\/webkul.com"],"url":"https:\/\/webkul.com\/blog\/author\/ajay\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/140605","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=140605"}],"version-history":[{"count":18,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/140605\/revisions"}],"predecessor-version":[{"id":140656,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/140605\/revisions\/140656"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=140605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=140605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=140605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}