{"id":2565,"date":"2023-11-07T01:40:04","date_gmt":"2023-11-07T00:40:04","guid":{"rendered":"https:\/\/bornoe.org\/blog\/?p=2565"},"modified":"2023-11-30T02:05:34","modified_gmt":"2023-11-30T01:05:34","slug":"javascript-loops-cheat-sheat","status":"publish","type":"post","link":"https:\/\/bornoe.org\/blog\/2023\/11\/javascript-loops-cheat-sheat\/","title":{"rendered":"JavaScript loops cheat sheat"},"content":{"rendered":"\n<p>This cheat sheet provides a quick reference guide to the various loop types in JavaScript, including their syntax, descriptions,<br>and examples. Loops are an essential part of any programming language, including JavaScript. They allow you to execute a<br>block of code repeatedly, enabling you to automate repetitive tasks and iterate over data structures.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">for loop<\/h2>\n\n\n\n<p>The for loop is used when the number of iterations is known in advance. It initializes a variable, checks a condition, and increments or decrements the variable value. The loop continues until the condition becomes false.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>for (initialization; condition; increment\/decrement) {\n                \/\/ code block to be executed\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\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<p>Output in console<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1\n2\n3\n4     <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">while loop<\/h2>\n\n\n\n<p>The while loop is used when the number of iterations is not known in advance. It executes the code block as long as the condition remains true.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>while (condition) {\n    \/\/ code block to be executed\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>let i = 0;\n\nwhile (i &lt; 5) {\n    console.log(i);\n    i++;\n}<\/code><\/pre>\n\n\n\n<p>Output in console<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1\n2\n3\n4       <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">do-while loop:<\/h2>\n\n\n\n<p>The do-while loop is similar to the while loop, but it always executes the code block at least once before checking the condition.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>do {\n    \/\/ code block to be executed\n} while (condition);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>let i = 0;\n\ndo {\n        console.log(i);\n        i++;\n} while (i &lt; 5);<\/code><\/pre>\n\n\n\n<p>Output in console<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1\n2\n3\n4       <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">for&#8230;in loop<\/h2>\n\n\n\n<p>The for&#8230;in loop iterates over the enumerable properties of an object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>for (variable in object) {\n    \/\/ code block to be executed\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const obj = { a: 1, b: 2, c: 3 }; \/\/ initiating a a const object\n\nfor (let key in obj) {\n    console.log(`${key}: ${obj&#91;key]}`);\n}<\/code><\/pre>\n\n\n\n<p>Output in console<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a: 1\nb: 2\nc: 3<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">for&#8230;of loop<\/h2>\n\n\n\n<p>The for&#8230;of loop is used to iterate over iterable objects, such as arrays or strings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>for (variable of iterable) {\n   \/\/ code block to be executed\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr = &#91;1, 2, 3, 4, 5]; \/\/ Initiating an array\n\nfor (let value of arr) {\n    console.log(value);\n}<\/code><\/pre>\n\n\n\n<p>Output in console<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This cheat sheet provides a quick reference guide to the various loop types in JavaScript, including their syntax, descriptions,and examples. Loops are an essential part of any programming language, including JavaScript. They allow you to execute ablock of code repeatedly, enabling you to automate repetitive tasks and iterate over data structures. for loop The for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-2565","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/posts\/2565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/comments?post=2565"}],"version-history":[{"count":3,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/posts\/2565\/revisions"}],"predecessor-version":[{"id":2658,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/posts\/2565\/revisions\/2658"}],"wp:attachment":[{"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/media?parent=2565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/categories?post=2565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/tags?post=2565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}