{"id":667,"date":"2024-01-04T18:51:00","date_gmt":"2024-01-04T18:51:00","guid":{"rendered":"https:\/\/www.cmsgalaxy.com\/blog\/?p=667"},"modified":"2024-02-05T10:25:26","modified_gmt":"2024-02-05T10:25:26","slug":"javascript-loops","status":"publish","type":"post","link":"https:\/\/www.cmsgalaxy.com\/blog\/javascript-loops\/","title":{"rendered":"JavaScript Loops"},"content":{"rendered":"\n<p>In JavaScript, loops are used to execute a block of code repeatedly until a specified condition is met. There are several types of loops available:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. for Loop:<\/h3>\n\n\n\n<p>The <code>for<\/code> loop repeats a block of code a specified number of times.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (let i = 0; i &lt; 5; i++) {\n  console.log(i);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. while Loop:<\/h3>\n\n\n\n<p>The <code>while<\/code> loop repeats a block of code while a specified condition is true.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let i = 0;\nwhile (i &lt; 5) {\n  console.log(i);\n  i++;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. do\u2026while Loop:<\/h3>\n\n\n\n<p>The <code>do...while<\/code> loop is similar to the <code>while<\/code> loop, but it executes the block of code at least once before checking the condition.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let i = 0;\ndo {\n  console.log(i);\n  i++;\n} while (i &lt; 5);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Loop Control Statements:<\/h3>\n\n\n\n<p>JavaScript provides loop control statements to control the flow of loops:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>break<\/strong>: Terminates the loop immediately.<\/li>\n\n\n\n<li><strong>continue<\/strong>: Skips the current iteration of the loop and continues with the next iteration.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>for (let i = 0; i &lt; 10; i++) {\n  if (i === 5) {\n    break; \/\/ Exit the loop when i is 5\n  }\n  console.log(i);\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>for (let i = 0; i &lt; 10; i++) {\n  if (i === 5) {\n    continue; \/\/ Skip iteration when i is 5\n  }\n  console.log(i);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Nested Loops:<\/h3>\n\n\n\n<p>You can nest loops within each other to perform more complex iterations.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (let i = 0; i &lt; 3; i++) {\n  for (let j = 0; j &lt; 2; j++) {\n    console.log(`i: ${i}, j: ${j}`);\n  }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Looping Through Arrays:<\/h3>\n\n\n\n<p>You can use loops to iterate over arrays and perform operations on each element.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let fruits = &#91;'apple', 'banana', 'orange'];\n\nfor (let i = 0; i &lt; fruits.length; i++) {\n  console.log(fruits&#91;i]);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">forEach() Method:<\/h3>\n\n\n\n<p>The <code>forEach()<\/code> method is a built-in method for arrays that executes a provided function once for each array element.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits.forEach(function(fruit) {\n  console.log(fruit);\n});<\/code><\/pre>\n\n\n\n<p>Loops are essential for performing repetitive tasks and iterating over collections of data in JavaScript. Depending on the situation, you can choose the appropriate type of loop to achieve your desired outcome efficiently.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In JavaScript, loops are used to execute a block of code repeatedly until a specified condition is met. There are<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-667","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/comments?post=667"}],"version-history":[{"count":1,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/667\/revisions"}],"predecessor-version":[{"id":668,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/667\/revisions\/668"}],"wp:attachment":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/media?parent=667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/categories?post=667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/tags?post=667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}