{"id":549,"date":"2025-05-19T11:03:19","date_gmt":"2025-05-19T11:03:19","guid":{"rendered":"https:\/\/buhave.com\/courses\/?p=549"},"modified":"2025-05-20T12:57:18","modified_gmt":"2025-05-20T12:57:18","slug":"loops-and-iteration","status":"publish","type":"post","link":"https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/","title":{"rendered":"Loops and Iteration"},"content":{"rendered":"<h2>for, while, and do&#8230;while loops<\/h2>\n<p>Loops allow you to repeat a block of code as long as a condition is true. JavaScript provides several types of loops \u2014 each suited for different scenarios.<\/p>\n<h3>1. for Loop<\/h3>\n<p><strong>Best For:<\/strong><\/p>\n<ul>\n<li>When the number of iterations is known<\/li>\n<li>Looping through arrays or counters<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for (initialization; condition; increment) {<\/em><br \/>\n<em>\/\/ Code block to execute<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for (let i = 1; i &lt;= 5; i++) {<\/em><br \/>\n<em>console.log(&#8220;Count: &#8221; + i);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>\/\/ Output: Count: 1 &#8230; Count: 5<\/em><\/p>\n<p><strong>How it works:<\/strong><\/p>\n<ul>\n<li>initialization runs once at the beginning<\/li>\n<li>condition is checked before each iteration<\/li>\n<li>increment runs after each iteration<\/li>\n<\/ul>\n<h3>2. while Loop<\/h3>\n<p><strong>Best For:<\/strong><\/p>\n<ul>\n<li>When the number of iterations is unknown<\/li>\n<li>Running a loop as long as a condition remains true<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>while (condition) {<\/em><br \/>\n<em>\/\/ Code block to execute<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>let i = 1;<\/em><br \/>\n<em>while (i &lt;= 5) {<\/em><br \/>\n<em>console.log(&#8220;Count: &#8221; + i);<\/em><br \/>\n<em>i++;<\/em><br \/>\n<em>}<\/em><br \/>\n<em>\/\/ Output: Count: 1 &#8230; Count: 5<\/em><\/p>\n<p><strong>How it works:<\/strong><\/p>\n<ul>\n<li>The condition is evaluated before each loop<\/li>\n<li>If false, the loop exits immediately<\/li>\n<\/ul>\n<h3>3. do&#8230;while Loop<\/h3>\n<p><strong>Best For:<\/strong><\/p>\n<ul>\n<li>When you want the loop to run at least once, even if the condition is false<\/li>\n<li>Validating user input, menu systems, etc.<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>do {<\/em><br \/>\n<em>\/\/ Code block to execute<\/em><br \/>\n<em>} while (condition);<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>let i = 1;<\/em><br \/>\n<em>do {<\/em><br \/>\n<em>console.log(&#8220;Count: &#8221; + i);<\/em><br \/>\n<em>i++;<\/em><br \/>\n<em>} while (i &lt;= 5);<\/em><br \/>\n<em>\/\/ Output: Count: 1 &#8230; Count: 5<\/em><\/p>\n<p><strong>How it works:<\/strong><\/p>\n<p>The code block executes once before checking the condition<\/p>\n<p><strong>Comparison Table:<\/strong><\/p>\n<table class=\"loop-comparison\">\n<thead>\n<tr>\n<th>Feature<\/th>\n<th>for Loop<\/th>\n<th>while Loop<\/th>\n<th>do&#8230;while Loop<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"highlight\">Use case<\/td>\n<td>Known iterations<\/td>\n<td>Unknown iterations<\/td>\n<td>Run at least once<\/td>\n<\/tr>\n<tr>\n<td class=\"highlight\">Condition check<\/td>\n<td>Before each iteration<\/td>\n<td>Before each iteration<\/td>\n<td>After first iteration<\/td>\n<\/tr>\n<tr>\n<td class=\"highlight\">Runs if false?<\/td>\n<td class=\"no\">No<\/td>\n<td class=\"no\">No<\/td>\n<td class=\"yes\">Yes (runs once)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Common Mistakes to Avoid<\/strong><\/p>\n<ul>\n<li>Forgetting to update the counter \u2192 infinite loop<\/li>\n<li>Using = instead of == or === in condition<\/li>\n<li>Not adding a base case in while or do&#8230;while<\/li>\n<\/ul>\n<p><strong>Best Practices<\/strong><\/p>\n<ul>\n<li>Use for when working with arrays or indexes<\/li>\n<li>Use while when looping until a dynamic condition is met<\/li>\n<li>Use do&#8230;while when you need at least one execution<\/li>\n<\/ul>\n<h2>Break, continue, and nested loops<\/h2>\n<h3 data-start=\"94\" data-end=\"120\">1. Break Statement<\/h3>\n<p class=\"\" data-start=\"121\" data-end=\"204\">The break statement is used to immediately exit a loop or switch statement.<\/p>\n<p data-start=\"206\" data-end=\"221\"><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\" data-start=\"206\" data-end=\"221\"><em>for (let i = 1; i &lt;= 5; i++) {<\/em><br \/>\n<em>if (i === 3) break;<\/em><br \/>\n<em>console.log(i);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>\/\/ Output: 1, 2<\/em><\/p>\n<ul>\n<li data-start=\"206\" data-end=\"221\">The loop stops completely when <code data-start=\"359\" data-end=\"368\">i === 3<\/code>.<\/li>\n<\/ul>\n<h3>2. continue Statement<\/h3>\n<p>The continue statement is used to skip the current iteration and jump to the next one.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for (let i = 1; i &lt;= 5; i++) {<\/em><br \/>\n<em>if (i === 3) continue;<\/em><br \/>\n<em>console.log(i);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>\/\/ Output: 1, 2, 4, 5<\/em><\/p>\n<ul>\n<li>When i === 3, it skips that iteration but continues looping.<\/li>\n<\/ul>\n<h3>3. Nested Loops<\/h3>\n<p>A loop inside another loop.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for (let i = 1; i &lt;= 3; i++) {<\/em><br \/>\n<em>for (let j = 1; j &lt;= 3; j++) {<\/em><br \/>\n<em>console.log(`i: ${i}, j: ${j}`);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>}<\/em><\/p>\n<ul>\n<li>The inner loop completes all iterations before the outer loop moves on.<\/li>\n<\/ul>\n<p><strong>Output:<\/strong><\/p>\n<p style=\"text-align: center\"><em>i: 1, j: 1<\/em><br \/>\n<em>i: 1, j: 2<\/em><br \/>\n<em>i: 1, j: 3<\/em><br \/>\n<em>i: 2, j: 1<\/em><br \/>\n<em>&#8230;<\/em><\/p>\n<p><strong>Using break &amp; continue in Nested Loops<\/strong><\/p>\n<p><strong>Break from Inner Loop Only<\/strong><\/p>\n<p style=\"text-align: center\"><em>for (let i = 1; i &lt;= 3; i++) {<\/em><br \/>\n<em>for (let j = 1; j &lt;= 3; j++) {<\/em><br \/>\n<em>if (j === 2) break;<\/em><br \/>\n<em>console.log(`i: ${i}, j: ${j}`);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>Break from All Loops (Label)<\/strong><\/p>\n<p style=\"text-align: center\"><em>outerLoop: for (let i = 1; i &lt;= 3; i++) {<\/em><br \/>\n<em>for (let j = 1; j &lt;= 3; j++) {<\/em><br \/>\n<em>if (j === 2) break outerLoop;<\/em><br \/>\n<em>console.log(`i: ${i}, j: ${j}`);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>Common Mistakes:<\/strong><\/p>\n<ul>\n<li>Infinite loops by forgetting to break or increment.<\/li>\n<li>Overusing nested loops \u2192 Slow performance.<\/li>\n<\/ul>\n<h2>Looping through arrays and strings<\/h2>\n<h3>Looping Through Arrays in JavaScript<\/h3>\n<p>Arrays are ordered collections, and there are multiple ways to loop through them.<\/p>\n<p><strong>1. Using a for Loop<\/strong><\/p>\n<p>Traditional and flexible.<\/p>\n<p style=\"text-align: center\"><em>const fruits = [&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;];<\/em><\/p>\n<p style=\"text-align: center\"><em>for (let i = 0; i &lt; fruits.length; i++) {<\/em><br \/>\n<em>console.log(fruits[i]);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>\/\/ Output: apple, banana, cherry<\/em><\/p>\n<ul>\n<li>Access each element by its index (i).<\/li>\n<\/ul>\n<p><strong>2. Using for&#8230;of Loop<\/strong><\/p>\n<p>Simpler and cleaner for arrays.<\/p>\n<p style=\"text-align: center\"><em>const fruits = [&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;];<\/em><\/p>\n<p style=\"text-align: center\"><em>for (let fruit of fruits) {<\/em><br \/>\n<em>console.log(fruit);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>\/\/ Output: apple, banana, cherry<\/em><\/p>\n<ul>\n<li>Directly gives you the value, not the index.<\/li>\n<\/ul>\n<p><strong>3. Using forEach() Method<\/strong><\/p>\n<p>Functional style.<\/p>\n<p style=\"text-align: center\"><em>const fruits = [&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;];<\/em><\/p>\n<p style=\"text-align: center\"><em>fruits.forEach(function(fruit, index) {<\/em><br \/>\n<em>console.log(index, fruit);<\/em><br \/>\n<em>});<\/em><br \/>\n<em>\/\/ Output: 0 apple, 1 banana, 2 cherry<\/em><\/p>\n<p><strong>Can also use arrow functions:<\/strong><\/p>\n<p style=\"text-align: center\"><em>fruits.forEach((fruit, index) =&gt; console.log(index, fruit));<\/em><\/p>\n<p><strong>4. Using map() (if you want a new array)<\/strong><\/p>\n<p style=\"text-align: center\"><em>const upperFruits = fruits.map(fruit =&gt; fruit.toUpperCase());<\/em><br \/>\n<em>console.log(upperFruits);<\/em><br \/>\n<em>\/\/ Output: [&#8220;APPLE&#8221;, &#8220;BANANA&#8221;, &#8220;CHERRY&#8221;]<\/em><\/p>\n<ul>\n<li>map() is for transforming arrays while looping.<\/li>\n<\/ul>\n<h3>Looping Through Strings in JavaScript<\/h3>\n<p>Strings are like arrays of characters.<\/p>\n<p><strong>1. Using a for Loop<\/strong><\/p>\n<p style=\"text-align: center\"><em>const str = &#8220;hello&#8221;;<\/em><\/p>\n<p style=\"text-align: center\"><em>for (let i = 0; i &lt; str.length; i++) {<\/em><br \/>\n<em>console.log(str[i]);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>\/\/ Output: h, e, l, l, o<\/em><\/p>\n<ul>\n<li>Access each character by index.<\/li>\n<\/ul>\n<p><strong>2. Using for&#8230;of Loop<\/strong><\/p>\n<p style=\"text-align: center\"><em>const str = &#8220;hello&#8221;;<\/em><\/p>\n<p style=\"text-align: center\"><em>for (let char of str) {<\/em><br \/>\n<em>console.log(char);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>\/\/ Output: h, e, l, l, o<\/em><\/p>\n<ul>\n<li>Very clean and readable.<\/li>\n<\/ul>\n<p><strong>3. Using split() and forEach()<\/strong><\/p>\n<p>First convert string to array.<\/p>\n<p style=\"text-align: center\"><em>const str = &#8220;hello&#8221;;<\/em><\/p>\n<p style=\"text-align: center\"><em>str.split(&#8220;&#8221;).forEach(char =&gt; console.log(char));<\/em><br \/>\n<em>\/\/ Output: h, e, l, l, o<\/em><\/p>\n<ul>\n<li>Useful if you want array methods while looping.<\/li>\n<\/ul>\n<p><strong>Summary Table:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>Loop Type<\/th>\n<th>Best For<\/th>\n<th>Notes<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>for loop<\/td>\n<td>Full control (index, value)<\/td>\n<td>Flexible, a bit longer<\/td>\n<\/tr>\n<tr>\n<td>for&#8230;of loop<\/td>\n<td>Easy value access<\/td>\n<td>Cleaner for arrays and strings<\/td>\n<\/tr>\n<tr>\n<td>forEach()<\/td>\n<td>Functional iteration<\/td>\n<td>Cannot break or continue<\/td>\n<\/tr>\n<tr>\n<td>map()<\/td>\n<td>Transform and return new array<\/td>\n<td>Does not modify original array<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Best Practices<\/strong><\/p>\n<ul>\n<li>Use for&#8230;of for simple value access.<\/li>\n<li>Use for if you need the index.<\/li>\n<li>Use forEach() for quick iteration without needing breaks.<\/li>\n<li>Use map() when you want to create a new array.<\/li>\n<\/ul>\n<h2>for&#8230;in vs. for&#8230;of<\/h2>\n<p>Both for&#8230;in and for&#8230;of are looping constructs, but they are used for different types of iteration.<\/p>\n<table>\n<thead>\n<tr>\n<th>Feature<\/th>\n<th>for&#8230;in<\/th>\n<th>for&#8230;of<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>Iterates over<\/strong><\/td>\n<td>Keys (property names)<\/td>\n<td>Values (data inside)<\/td>\n<\/tr>\n<tr>\n<td><strong>Works with<\/strong><\/td>\n<td>Objects, Arrays (not recommended)<\/td>\n<td>Arrays, Strings, Maps, Sets, etc.<\/td>\n<\/tr>\n<tr>\n<td><strong>Value inside loop<\/strong><\/td>\n<td>Key\/index<\/td>\n<td>Actual value<\/td>\n<\/tr>\n<tr>\n<td><strong>Suitable for<\/strong><\/td>\n<td>Objects<\/td>\n<td>Arrays, Strings, Iterables<\/td>\n<\/tr>\n<tr>\n<td><strong>Should you use on arrays?<\/strong><\/td>\n<td class=\"warning\">Not ideal \u2014 may cause bugs<\/td>\n<td class=\"success\">Perfect fit<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>1. for&#8230;in Loop (for Object Properties)<\/h3>\n<p>The for&#8230;in loop is mainly for enumerating object properties.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for (let key in object) {<\/em><br \/>\n<em>\/\/ Code block<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>const user = { name: &#8220;Alice&#8221;, age: 25, country: &#8220;USA&#8221; };<\/em><\/p>\n<p style=\"text-align: center\"><em>for (let key in user) {<\/em><br \/>\n<em>console.log(`${key}: ${user[key]}`);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>\/\/ Output:<\/em><br \/>\n<em>\/\/ name: Alice<\/em><br \/>\n<em>\/\/ age: 25<\/em><br \/>\n<em>\/\/ country: USA<\/em><\/p>\n<ul>\n<li>key is the property name (&#8220;name&#8221;, &#8220;age&#8221;, etc.)<\/li>\n<li>You access the value using object[key].<\/li>\n<\/ul>\n<h3>2. for&#8230;of Loop (for Iterable Values)<\/h3>\n<p>The for&#8230;of loop is used to iterate over values of iterable objects like Arrays, Strings, Maps, Sets.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for (let value of iterable) {<\/em><br \/>\n<em>\/\/ Code block<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>Example with Array:<\/strong><\/p>\n<p style=\"text-align: center\"><em>const colors = [&#8220;red&#8221;, &#8220;green&#8221;, &#8220;blue&#8221;];<\/em><\/p>\n<p style=\"text-align: center\"><em>for (let color of colors) {<\/em><br \/>\n<em>console.log(color);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>\/\/ Output:<\/em><br \/>\n<em>\/\/ red<\/em><br \/>\n<em>\/\/ green<\/em><br \/>\n<em>\/\/ blue<\/em><\/p>\n<ul>\n<li>color is the actual value inside the array.<\/li>\n<\/ul>\n<p><strong>Example with String:<\/strong><\/p>\n<p style=\"text-align: center\"><em>const name = &#8220;Bob&#8221;;<\/em><\/p>\n<p style=\"text-align: center\"><em>for (let char of name) {<\/em><br \/>\n<em>console.log(char);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>\/\/ Output: B, o, b<\/em><\/p>\n<p><strong>Important Notes<\/strong><\/p>\n<ul>\n<li>for&#8230;in loops over all enumerable properties, including those inherited through the prototype chain.<br \/>\nThat&#8217;s why it\u2019s not recommended for arrays \u2014 you might get unexpected keys.<\/li>\n<li>for&#8230;of works only on iterables (arrays, strings, maps, sets) and gives you clean, value-based iteration.<\/li>\n<\/ul>\n<p><strong>Real World Tip:<\/strong><\/p>\n<ul>\n<li>When looping over arrays or strings\u00a0 use for&#8230;of.<\/li>\n<li>When looping over object properties\u00a0 use for&#8230;in.<\/li>\n<\/ul>\n<p><strong>Quick Summary:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>Use Case<\/th>\n<th>Loop Type<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Looping object properties<\/td>\n<td class=\"loop-type for-in\">for&#8230;in<\/td>\n<\/tr>\n<tr>\n<td>Looping array elements<\/td>\n<td class=\"loop-type for-of\">for&#8230;of<\/td>\n<\/tr>\n<tr>\n<td>Looping string characters<\/td>\n<td class=\"loop-type for-of\">for&#8230;of<\/td>\n<\/tr>\n<tr>\n<td>Looping Map or Set<\/td>\n<td class=\"loop-type for-of\">for&#8230;of<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>for, while, and do&#8230;while loops Loops allow you to repeat a block of code as long as a condition is true. JavaScript provides several types of loops \u2014 each suited for different scenarios. 1. for Loop Best For: When the number of iterations is known<\/p>\n","protected":false},"author":1,"featured_media":550,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-549","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-script"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Loops and Iteration - Java Script Course<\/title>\n<meta name=\"description\" content=\"Loops and iteration repeatedly execute code while a condition is true, helping efficiently traverse arrays and perform repeated actions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Loops and Iteration - Java Script Course\" \/>\n<meta property=\"og:description\" content=\"Loops and iteration repeatedly execute code while a condition is true, helping efficiently traverse arrays and perform repeated actions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/\" \/>\n<meta property=\"og:site_name\" content=\"BUHAVE\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/BeYouHave\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/naveedsafdarawan\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-19T11:03:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-20T12:57:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Loops-and-Iteration-1.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Naveed Safdar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Naveed Safdar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/loops-and-iteration\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/loops-and-iteration\\\/\"},\"author\":{\"name\":\"Naveed Safdar\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\"},\"headline\":\"Loops and Iteration\",\"datePublished\":\"2025-05-19T11:03:19+00:00\",\"dateModified\":\"2025-05-20T12:57:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/loops-and-iteration\\\/\"},\"wordCount\":1160,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/loops-and-iteration\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Loops-and-Iteration-1.webp\",\"articleSection\":[\"Java Script Course\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/loops-and-iteration\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/loops-and-iteration\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/loops-and-iteration\\\/\",\"name\":\"Loops and Iteration - Java Script Course\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/loops-and-iteration\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/loops-and-iteration\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Loops-and-Iteration-1.webp\",\"datePublished\":\"2025-05-19T11:03:19+00:00\",\"dateModified\":\"2025-05-20T12:57:18+00:00\",\"description\":\"Loops and iteration repeatedly execute code while a condition is true, helping efficiently traverse arrays and perform repeated actions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/loops-and-iteration\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/loops-and-iteration\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/loops-and-iteration\\\/#primaryimage\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Loops-and-Iteration-1.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Loops-and-Iteration-1.webp\",\"width\":1200,\"height\":628,\"caption\":\"Loops and Iteration\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/loops-and-iteration\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Courses\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Script Course\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/learn\\\/java-script\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Loops and Iteration\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"name\":\"BUHAVE\",\"description\":\"Courses - Learn Online for Free\",\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/buhave.com\\\/courses\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\",\"name\":\"BUHAVE\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"width\":375,\"height\":75,\"caption\":\"BUHAVE\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/BeYouHave\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/buhave\",\"https:\\\/\\\/www.youtube.com\\\/@buhave\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\",\"name\":\"Naveed Safdar\",\"description\":\"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/naveedsafdar\\\/\",\"https:\\\/\\\/www.facebook.com\\\/naveedsafdarawan\\\/\",\"https:\\\/\\\/www.youtube.com\\\/@naveedsafdar\"],\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/author\\\/naveed-safdar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Loops and Iteration - Java Script Course","description":"Loops and iteration repeatedly execute code while a condition is true, helping efficiently traverse arrays and perform repeated actions.","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:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/","og_locale":"en_US","og_type":"article","og_title":"Loops and Iteration - Java Script Course","og_description":"Loops and iteration repeatedly execute code while a condition is true, helping efficiently traverse arrays and perform repeated actions.","og_url":"https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/","og_site_name":"BUHAVE","article_publisher":"https:\/\/www.facebook.com\/BeYouHave\/","article_author":"https:\/\/www.facebook.com\/naveedsafdarawan\/","article_published_time":"2025-05-19T11:03:19+00:00","article_modified_time":"2025-05-20T12:57:18+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Loops-and-Iteration-1.webp","type":"image\/webp"}],"author":"Naveed Safdar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Naveed Safdar","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/#article","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/"},"author":{"name":"Naveed Safdar","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca"},"headline":"Loops and Iteration","datePublished":"2025-05-19T11:03:19+00:00","dateModified":"2025-05-20T12:57:18+00:00","mainEntityOfPage":{"@id":"https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/"},"wordCount":1160,"commentCount":0,"publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"image":{"@id":"https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Loops-and-Iteration-1.webp","articleSection":["Java Script Course"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/","url":"https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/","name":"Loops and Iteration - Java Script Course","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/#website"},"primaryImageOfPage":{"@id":"https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/#primaryimage"},"image":{"@id":"https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Loops-and-Iteration-1.webp","datePublished":"2025-05-19T11:03:19+00:00","dateModified":"2025-05-20T12:57:18+00:00","description":"Loops and iteration repeatedly execute code while a condition is true, helping efficiently traverse arrays and perform repeated actions.","breadcrumb":{"@id":"https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/#primaryimage","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Loops-and-Iteration-1.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Loops-and-Iteration-1.webp","width":1200,"height":628,"caption":"Loops and Iteration"},{"@type":"BreadcrumbList","@id":"https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Courses","item":"https:\/\/buhave.com\/courses\/"},{"@type":"ListItem","position":2,"name":"Java Script Course","item":"https:\/\/buhave.com\/courses\/learn\/java-script\/"},{"@type":"ListItem","position":3,"name":"Loops and Iteration"}]},{"@type":"WebSite","@id":"https:\/\/buhave.com\/courses\/#website","url":"https:\/\/buhave.com\/courses\/","name":"BUHAVE","description":"Courses - Learn Online for Free","publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/buhave.com\/courses\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/buhave.com\/courses\/#organization","name":"BUHAVE","url":"https:\/\/buhave.com\/courses\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","width":375,"height":75,"caption":"BUHAVE"},"image":{"@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/BeYouHave\/","https:\/\/www.linkedin.com\/company\/buhave","https:\/\/www.youtube.com\/@buhave"]},{"@type":"Person","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca","name":"Naveed Safdar","description":"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.","sameAs":["https:\/\/www.linkedin.com\/in\/naveedsafdar\/","https:\/\/www.facebook.com\/naveedsafdarawan\/","https:\/\/www.youtube.com\/@naveedsafdar"],"url":"https:\/\/buhave.com\/courses\/author\/naveed-safdar\/"}]}},"_links":{"self":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/comments?post=549"}],"version-history":[{"count":1,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/549\/revisions"}],"predecessor-version":[{"id":551,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/549\/revisions\/551"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media\/550"}],"wp:attachment":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media?parent=549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/categories?post=549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/tags?post=549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}