{"id":57,"date":"2020-01-08T22:17:38","date_gmt":"2020-01-08T22:17:38","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/javascript\/?p=57"},"modified":"2020-01-08T22:17:38","modified_gmt":"2020-01-08T22:17:38","slug":"javascript-loops","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/","title":{"rendered":"JavaScript &#8211; Loops"},"content":{"rendered":"<p>As you know, a loop allows you to execute a block of code a number of times. In JavaScript, three types of loops are supported:<\/p>\n<ol>\n<li><a href=\"#t1\">While Loop<\/a><\/li>\n<li><a href=\"#t2\">Do&#8230;While Loop<\/a><\/li>\n<li><a href=\"#t3\">For Loop<\/a><\/li>\n<li><a href=\"#t4\">For&#8230;In Loop<\/a><\/li>\n<\/ol>\n<p>Then we would also learn of some loop statement such as break and continue statements in the next section.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t1\">1. While Loop<\/strong><\/h5>\n<p>The while loop is used to execute a block of code repeatedly so long as a test expression is true. The loop terminates once the expression is false. So for each iteration of the loop, the test expression is evaluated.<\/p>\n<figure id=\"attachment_58\" aria-describedby=\"caption-attachment-58\" style=\"width: 377px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/javascript\/wp-content\/uploads\/sites\/6\/2020\/01\/While-loop.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-58 \" src=\"https:\/\/www.kindsonthegenius.com\/javascript\/wp-content\/uploads\/sites\/6\/2020\/01\/While-loop.jpg\" alt=\"While loop\" width=\"377\" height=\"448\" srcset=\"https:\/\/www.kindsonthegenius.com\/javascript\/wp-content\/uploads\/sites\/6\/2020\/01\/While-loop.jpg 668w, https:\/\/www.kindsonthegenius.com\/javascript\/wp-content\/uploads\/sites\/6\/2020\/01\/While-loop-252x300.jpg 252w\" sizes=\"auto, (max-width: 377px) 100vw, 377px\" \/><\/a><figcaption id=\"caption-attachment-58\" class=\"wp-caption-text\">While loop<\/figcaption><\/figure>\n<p>Example of a While Loop<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;html&gt;<\/span>\r\n\t<span style=\"color: #007700;\">&lt;head&gt;<\/span>\r\n\t<span style=\"color: #007700;\">&lt;title&gt;<\/span>While Loop demo<span style=\"color: #007700;\">&lt;\/title&gt;<\/span>\r\n\t<span style=\"color: #007700;\">&lt;\/head&gt;<\/span>\r\n\r\n\t<span style=\"color: #007700;\">&lt;body&gt;<\/span>\r\n\r\n\t<span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">type =<\/span> <span style=\"background-color: #fff0f0;\">\"text\/javascript\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">var<\/span> counter <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n\t\t<span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Begining of while loop\"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\"&lt;br \/&gt;\"<\/span>);\r\n\t\t\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">while<\/span>(counter <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>) {\r\n\t\t\t<span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Current counter: \"<\/span> <span style=\"color: #333333;\">+<\/span> counter <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\"&lt;br \/&gt;\"<\/span>);\r\n\t\t\tcounter <span style=\"color: #333333;\">++<\/span>;\r\n\t\t}\r\n\t\t<span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"End of while loop\"<\/span>);\r\n\r\n\t<span style=\"color: #007700;\">&lt;\/script&gt;<\/span>\r\n\r\n\t<span style=\"color: #007700;\">&lt;\/body&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;\/html&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t2\">2. The do&#8230;while Loop<\/strong><\/h5>\n<p>The do&#8230;while loop is similar to the while loop with the exception that the conditional statement is checked at the end of the loop. Therefore, the body of the loop will always be executed at least once.<\/p>\n<p>An equivalent do&#8230;while loop for the while loop example is given below. But only the &lt;script&gt;&lt;\/script&gt; section is presented here.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">type =<\/span> <span style=\"background-color: #fff0f0;\">\"text\/javascript\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold;\">var<\/span> counter <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n\t<span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Begining of do...while loop\"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\"&lt;br \/&gt;\"<\/span>);\r\n\t<span style=\"color: #008800; font-weight: bold;\">do<\/span>{\r\n\t\t<span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Current counter: \"<\/span> <span style=\"color: #333333;\">+<\/span> counter <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\"&lt;br \/&gt;\"<\/span>);\r\n\t\tcounter <span style=\"color: #333333;\">++<\/span>;\r\n\t} <span style=\"color: #008800; font-weight: bold;\">while<\/span>(counter <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>) \r\n\t\r\n\t<span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"End of do...while loop\"<\/span>);\r\n\r\n<span style=\"color: #007700;\">&lt;\/script&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t3\">3. For Loop<\/strong><\/h5>\n<p>The for loop is used to execute a statement a given number of times. The flowchart for the for loop is given below:<\/p>\n<figure id=\"attachment_59\" aria-describedby=\"caption-attachment-59\" style=\"width: 287px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/javascript\/wp-content\/uploads\/sites\/6\/2020\/01\/For-Loop.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-59\" src=\"https:\/\/www.kindsonthegenius.com\/javascript\/wp-content\/uploads\/sites\/6\/2020\/01\/For-Loop-287x300.jpg\" alt=\"For Loop\" width=\"287\" height=\"300\" srcset=\"https:\/\/www.kindsonthegenius.com\/javascript\/wp-content\/uploads\/sites\/6\/2020\/01\/For-Loop-287x300.jpg 287w, https:\/\/www.kindsonthegenius.com\/javascript\/wp-content\/uploads\/sites\/6\/2020\/01\/For-Loop.jpg 530w\" sizes=\"auto, (max-width: 287px) 100vw, 287px\" \/><\/a><figcaption id=\"caption-attachment-59\" class=\"wp-caption-text\">For Loop Flowchart<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>The for loop statement contains three parts, namely:<\/p>\n<ul class=\"list\">\n<li><b>initialization<\/b> where we initialize our counter variable to a starting value. The initialization statement must always be\u00a0 executed before the loop begins.<\/li>\n<li><b>test statement<\/b> that will test if a given condition evaluates to true or not. If the condition is true, then the body of the the loop will be executed, otherwise the control will exit from the loop to the statements following the loop.<\/li>\n<li><b>iteration statement<\/b> where you can adjust the value your counter.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Example of For loop<\/p>\n<p>The code below is an example of For Loop that uses a loop to print number from 0 to 9<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;html&gt;<\/span>\r\n\t<span style=\"color: #007700;\">&lt;head&gt;<\/span>\r\n\t<span style=\"color: #007700;\">&lt;title&gt;<\/span>Loop demo<span style=\"color: #007700;\">&lt;\/title&gt;<\/span>\r\n\t<span style=\"color: #007700;\">&lt;\/head&gt;<\/span>\r\n\r\n\t<span style=\"color: #007700;\">&lt;body&gt;<\/span>\r\n\r\n\t<span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">type =<\/span> <span style=\"background-color: #fff0f0;\">\"text\/javascript\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">var<\/span> count;\r\n\t\t<span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Begining of loop\"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\"&lt;br \/&gt;\"<\/span>);\r\n\t\t\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">for<\/span>(count <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>; count <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>; count<span style=\"color: #333333;\">++<\/span>) {\r\n\t\t\t<span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Current count: \"<\/span> <span style=\"color: #333333;\">+<\/span> count);\r\n\t\t\t<span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"&lt;br \/&gt;\"<\/span>);\r\n\t\t}\r\n\t\t<span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"End of loop\"<\/span>);\r\n\r\n\t<span style=\"color: #007700;\">&lt;\/script&gt;<\/span>\r\n\r\n\t<span style=\"color: #007700;\">&lt;\/body&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;\/html&gt;<\/span>\r\n<\/pre>\n<p>Test this code yourself to see the output.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t4\">4. For&#8230;In Loop<\/strong><\/h5>\n<p>The for&#8230;in loop is used to loop through a collection of items.\u00a0 For example, you can use it to loop through an object&#8217;s properties. The syntax for the for&#8230;in loop is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">for<\/span> (variable <span style=\"color: #008800; font-weight: bold;\">in<\/span> object) {\r\n   statement or to execute\r\n}\r\n<\/pre>\n<p>In each iteration of the for&#8230;in loop, a property from object is assigned to to variable. The loop continues until all the properties of the object is completed.<\/p>\n<p>The example below is used to print all the properties of the browser&#8217;s navigator object.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;html&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;head&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;title&gt;<\/span>For...in Loop Demo<span style=\"color: #007700;\">&lt;\/title&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;\/head&gt;<\/span>\r\n   <span style=\"color: #007700;\">&lt;body&gt;<\/span>      \r\n      <span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">type =<\/span> <span style=\"background-color: #fff0f0;\">\"text\/javascript\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n         <span style=\"color: #888888;\">&lt;!--<\/span>\r\n            <span style=\"color: #008800; font-weight: bold;\">var<\/span> nProperty;\r\n            <span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Navigator Object Properties&lt;br \/&gt; \"<\/span>);        \r\n            <span style=\"color: #008800; font-weight: bold;\">for<\/span> (nProperty <span style=\"color: #008800; font-weight: bold;\">in<\/span> navigator) {\r\n               <span style=\"color: #007020;\">document<\/span>.write(nProperty);\r\n               <span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"&lt;br \/&gt;\"<\/span>);\r\n            }\r\n            <span style=\"color: #007020;\">document<\/span>.write (<span style=\"background-color: #fff0f0;\">\"End of Loop!\"<\/span>);\r\n         <span style=\"color: #888888;\">\/\/--&gt;<\/span>\r\n      <span style=\"color: #007700;\">&lt;\/script&gt;<\/span>      \r\n      <span style=\"color: #007700;\">&lt;p&gt;<\/span>Set the variable to different object and then try...<span style=\"color: #007700;\">&lt;\/p&gt;<\/span>\r\n   <span style=\"color: #007700;\">&lt;\/body&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/html&gt;<\/span>\r\n<\/pre>\n<p>If you execute the code above, you will have a list of items displayed on the web page. Some of them are shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Navigator Object Properties\r\nvendorSub\r\nproductSub\r\nvendor\r\nmaxTouchPoints\r\nhardwareConcurrency\r\ncookieEnabled\r\nappCodeName\r\nappName\r\nappVersion\r\nplatform\r\nproduct\r\nuserAgent\r\nlanguage\r\nlanguages\r\n...\r\n...\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In the next sections, we would now discuss Loop Control Statements<\/p>\n<p>Remember to watch the videos for clarification.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As you know, a loop allows you to execute a block of code a number of times. In JavaScript, three types of loops are supported: &hellip; <\/p>\n","protected":false},"author":395,"featured_media":60,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[7,5,8,6],"class_list":["post-57","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript-tutorials","tag-do-while-loop","tag-for-loop","tag-for-in-loop","tag-while-loop"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JavaScript - Loops - JavaScript Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript - Loops - JavaScript Tutorial\" \/>\n<meta property=\"og:description\" content=\"As you know, a loop allows you to execute a block of code a number of times. In JavaScript, three types of loops are supported: &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/\" \/>\n<meta property=\"og:site_name\" content=\"JavaScript Tutorial\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-08T22:17:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/javascript\/wp-content\/uploads\/sites\/6\/2020\/01\/Javascript-tutorial-8-Loops.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"856\" \/>\n\t<meta property=\"og:image:height\" content=\"388\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/javascript-loops\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/javascript-loops\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"JavaScript &#8211; Loops\",\"datePublished\":\"2020-01-08T22:17:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/javascript-loops\\\/\"},\"wordCount\":463,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/javascript-loops\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/wp-content\\\/uploads\\\/sites\\\/6\\\/2020\\\/01\\\/Javascript-tutorial-8-Loops.jpg\",\"keywords\":[\"Do...While Loop\",\"For Loop\",\"For...in Loop\",\"While Loop\"],\"articleSection\":[\"JavaScript Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/javascript-loops\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/javascript-loops\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/javascript-loops\\\/\",\"name\":\"JavaScript - Loops - JavaScript Tutorial\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/javascript-loops\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/javascript-loops\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/wp-content\\\/uploads\\\/sites\\\/6\\\/2020\\\/01\\\/Javascript-tutorial-8-Loops.jpg\",\"datePublished\":\"2020-01-08T22:17:38+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/javascript-loops\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/javascript-loops\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/javascript-loops\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/wp-content\\\/uploads\\\/sites\\\/6\\\/2020\\\/01\\\/Javascript-tutorial-8-Loops.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/wp-content\\\/uploads\\\/sites\\\/6\\\/2020\\\/01\\\/Javascript-tutorial-8-Loops.jpg\",\"width\":856,\"height\":388},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/javascript-loops\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript &#8211; Loops\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/\",\"name\":\"JavaScript Tutorial\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/javascript\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript - Loops - JavaScript Tutorial","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:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript - Loops - JavaScript Tutorial","og_description":"As you know, a loop allows you to execute a block of code a number of times. In JavaScript, three types of loops are supported: &hellip;","og_url":"https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/","og_site_name":"JavaScript Tutorial","article_published_time":"2020-01-08T22:17:38+00:00","og_image":[{"width":856,"height":388,"url":"https:\/\/www.kindsonthegenius.com\/javascript\/wp-content\/uploads\/sites\/6\/2020\/01\/Javascript-tutorial-8-Loops.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/javascript\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"JavaScript &#8211; Loops","datePublished":"2020-01-08T22:17:38+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/"},"wordCount":463,"commentCount":0,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/javascript\/wp-content\/uploads\/sites\/6\/2020\/01\/Javascript-tutorial-8-Loops.jpg","keywords":["Do...While Loop","For Loop","For...in Loop","While Loop"],"articleSection":["JavaScript Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/","url":"https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/","name":"JavaScript - Loops - JavaScript Tutorial","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/javascript\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/javascript\/wp-content\/uploads\/sites\/6\/2020\/01\/Javascript-tutorial-8-Loops.jpg","datePublished":"2020-01-08T22:17:38+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/javascript\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/javascript\/wp-content\/uploads\/sites\/6\/2020\/01\/Javascript-tutorial-8-Loops.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/javascript\/wp-content\/uploads\/sites\/6\/2020\/01\/Javascript-tutorial-8-Loops.jpg","width":856,"height":388},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/javascript\/javascript-loops\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/javascript\/"},{"@type":"ListItem","position":2,"name":"JavaScript &#8211; Loops"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/javascript\/#website","url":"https:\/\/www.kindsonthegenius.com\/javascript\/","name":"JavaScript Tutorial","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/javascript\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/javascript\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/javascript\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":1,"href":"https:\/\/www.kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":61,"href":"https:\/\/www.kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/posts\/57\/revisions\/61"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/media\/60"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}