{"id":87,"date":"2026-05-01T15:20:17","date_gmt":"2026-05-01T15:20:17","guid":{"rendered":"https:\/\/gigz.pk\/javascript\/?post_type=lesson&#038;p=87"},"modified":"2026-05-01T15:20:18","modified_gmt":"2026-05-01T15:20:18","slug":"for-loop","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javascript\/?lesson=for-loop","title":{"rendered":"For Loop"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Introduction<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A for loop in JavaScript is used to repeat a block of code a specific number of times. It is one of the most commonly used loops and is especially useful when working with arrays, lists, or repetitive tasks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding how a for loop works helps developers write efficient and organized code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What is a For Loop<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A for loop runs code again and again until a defined condition becomes false. It is commonly used when the number of iterations is known before the loop starts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A for loop has three main parts initialization condition and increment or decrement<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (initialization; condition; increment) {<br>  \/\/ code to execute<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Initialization sets the starting point<br>Condition defines how long the loop will run<br>Increment updates the loop variable after each iteration<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (let i = 0; i &lt; 5; i++) {<br>  console.log(\"Iteration number \" + i);<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This loop starts from 0 and runs until the value is less than 5 printing each step<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">How For Loop Works<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First the initialization runs once<br>Then the condition is checked<br>If true the code inside the loop executes<br>After execution the increment updates the variable<br>The process repeats until the condition becomes false<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Common Uses of For Loop<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Looping through arrays<br>Displaying lists of data<br>Performing repeated calculations<br>Generating dynamic content on web pages<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Looping Through an Array Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let fruits = &#91;\"Apple\", \"Banana\", \"Mango\"];<br><br>for (let i = 0; i &lt; fruits.length; i++) {<br>  console.log(fruits&#91;i]);<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This example prints each item from the array<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Advantages<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Simple and easy to use<br>Efficient for fixed repetitions<br>Improves code readability<br>Widely supported in all browsers<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Best Practices<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use meaningful variable names<br>Avoid infinite loops by setting correct conditions<br>Keep loop code clean and readable<br>Use let instead of var for better scope control<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Conclusion<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The for loop is a fundamental concept in JavaScript that allows developers to repeat tasks efficiently. Mastering loops is essential for building dynamic and interactive applications.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/javascript\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">JavaScript Fundamentals (Beginner Level) > Loops > for Loop<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777648786321\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777648786138\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"menu_order":0,"template":"","class_list":["post-87","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>For Loop - Learn JavaScript with GIGZ.PK<\/title>\n<meta name=\"description\" content=\"Learn JavaScript for loop with simple examples Understand syntax iteration and arrays to write efficient and dynamic code easily\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/javascript\/?lesson=for-loop\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"For Loop - Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"og:description\" content=\"Learn JavaScript for loop with simple examples Understand syntax iteration and arrays to write efficient and dynamic code easily\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/javascript\/?lesson=for-loop\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-01T15:20:18+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=for-loop\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=for-loop\",\"name\":\"For Loop - Learn JavaScript with GIGZ.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\"},\"datePublished\":\"2026-05-01T15:20:17+00:00\",\"dateModified\":\"2026-05-01T15:20:18+00:00\",\"description\":\"Learn JavaScript for loop with simple examples Understand syntax iteration and arrays to write efficient and dynamic code easily\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=for-loop#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=for-loop\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=for-loop#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Fundamentals (Beginner Level) > Loops > for Loop\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/\",\"name\":\"Learn JavaScript with GIGZ.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"For Loop - Learn JavaScript with GIGZ.PK","description":"Learn JavaScript for loop with simple examples Understand syntax iteration and arrays to write efficient and dynamic code easily","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:\/\/gigz.pk\/javascript\/?lesson=for-loop","og_locale":"en_US","og_type":"article","og_title":"For Loop - Learn JavaScript with GIGZ.PK","og_description":"Learn JavaScript for loop with simple examples Understand syntax iteration and arrays to write efficient and dynamic code easily","og_url":"https:\/\/gigz.pk\/javascript\/?lesson=for-loop","og_site_name":"Learn JavaScript with GIGZ.PK","article_modified_time":"2026-05-01T15:20:18+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/javascript\/?lesson=for-loop","url":"https:\/\/gigz.pk\/javascript\/?lesson=for-loop","name":"For Loop - Learn JavaScript with GIGZ.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javascript\/#website"},"datePublished":"2026-05-01T15:20:17+00:00","dateModified":"2026-05-01T15:20:18+00:00","description":"Learn JavaScript for loop with simple examples Understand syntax iteration and arrays to write efficient and dynamic code easily","breadcrumb":{"@id":"https:\/\/gigz.pk\/javascript\/?lesson=for-loop#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/javascript\/?lesson=for-loop"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/javascript\/?lesson=for-loop#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javascript"},{"@type":"ListItem","position":2,"name":"JavaScript Fundamentals (Beginner Level) > Loops > for Loop"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/javascript\/#website","url":"https:\/\/gigz.pk\/javascript\/","name":"Learn JavaScript with GIGZ.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/javascript\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson\/87","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}