{"id":89,"date":"2026-05-01T15:22:44","date_gmt":"2026-05-01T15:22:44","guid":{"rendered":"https:\/\/gigz.pk\/javascript\/?post_type=lesson&#038;p=89"},"modified":"2026-05-01T15:22:45","modified_gmt":"2026-05-01T15:22:45","slug":"while-loop","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javascript\/?lesson=while-loop","title":{"rendered":"While Loop"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Introduction<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A while loop in JavaScript is used to repeatedly execute a block of code as long as a specified condition remains true. It is useful when the number of iterations is not known in advance and depends on a condition.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What is a While Loop<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A while loop checks the condition before running the code block. If the condition is true, the code executes. This process continues until the condition becomes false.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while (condition) {<br>  \/\/ code to execute<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">How It Works<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The loop first evaluates the condition<br>If the condition is true, the code inside the loop runs<br>After execution, the condition is checked again<br>The loop stops when the condition becomes false<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let i = 1;<br><br>while (i &lt;= 5) {<br>  console.log(i);<br>  i++;<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Explanation<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The loop starts with i equal to 1<br>It runs while i is less than or equal to 5<br>After each iteration, i increases by 1<br>The output will be numbers from 1 to 5<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Common Use Cases<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Repeating tasks when the number of iterations is unknown<br>Reading data until a condition is met<br>Creating simple games or interactive features<br>Validating input in certain scenarios<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Important Tips<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Always ensure the condition will eventually become false<br>Avoid infinite loops by updating the condition variable<br>Use break if you need to exit the loop early<\/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 understand<br>Flexible for condition-based repetition<br>Useful when iteration count is not fixed<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Conclusion<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The while loop is an essential control structure in JavaScript. It allows developers to execute code repeatedly based on a condition, making programs more dynamic and efficient.<\/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 > while Loop<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777648936608\"><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-1777648936417\"><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-89","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>While Loop - Learn JavaScript with GIGZ.PK<\/title>\n<meta name=\"description\" content=\"Learn while loop in JavaScript with examples and syntax to execute code repeatedly based on conditions easily and efficiently\" \/>\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=while-loop\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"While Loop - Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"og:description\" content=\"Learn while loop in JavaScript with examples and syntax to execute code repeatedly based on conditions easily and efficiently\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/javascript\/?lesson=while-loop\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-01T15:22:45+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=while-loop\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=while-loop\",\"name\":\"While Loop - Learn JavaScript with GIGZ.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\"},\"datePublished\":\"2026-05-01T15:22:44+00:00\",\"dateModified\":\"2026-05-01T15:22:45+00:00\",\"description\":\"Learn while loop in JavaScript with examples and syntax to execute code repeatedly based on conditions easily and efficiently\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=while-loop#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=while-loop\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=while-loop#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Fundamentals (Beginner Level) > Loops > while 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":"While Loop - Learn JavaScript with GIGZ.PK","description":"Learn while loop in JavaScript with examples and syntax to execute code repeatedly based on conditions easily and efficiently","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=while-loop","og_locale":"en_US","og_type":"article","og_title":"While Loop - Learn JavaScript with GIGZ.PK","og_description":"Learn while loop in JavaScript with examples and syntax to execute code repeatedly based on conditions easily and efficiently","og_url":"https:\/\/gigz.pk\/javascript\/?lesson=while-loop","og_site_name":"Learn JavaScript with GIGZ.PK","article_modified_time":"2026-05-01T15:22:45+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=while-loop","url":"https:\/\/gigz.pk\/javascript\/?lesson=while-loop","name":"While Loop - Learn JavaScript with GIGZ.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javascript\/#website"},"datePublished":"2026-05-01T15:22:44+00:00","dateModified":"2026-05-01T15:22:45+00:00","description":"Learn while loop in JavaScript with examples and syntax to execute code repeatedly based on conditions easily and efficiently","breadcrumb":{"@id":"https:\/\/gigz.pk\/javascript\/?lesson=while-loop#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/javascript\/?lesson=while-loop"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/javascript\/?lesson=while-loop#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javascript"},{"@type":"ListItem","position":2,"name":"JavaScript Fundamentals (Beginner Level) > Loops > while 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\/89","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=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}