{"id":546,"date":"2025-05-19T11:03:51","date_gmt":"2025-05-19T11:03:51","guid":{"rendered":"https:\/\/buhave.com\/courses\/?p=546"},"modified":"2026-06-06T13:00:37","modified_gmt":"2026-06-06T13:00:37","slug":"control-flow","status":"publish","type":"post","link":"https:\/\/buhave.com\/courses\/java-script\/control-flow\/","title":{"rendered":"Control Flow"},"content":{"rendered":"<h2>Conditional statements (if, else, else if<span style=\"font-weight: 400\"><br \/>\n<\/span><\/h2>\n<h3>1. What Are Conditional Statements?<\/h3>\n<p>Conditional statements allow you to execute code based on conditions \u2014 they control flow and logic. For a quick refresher on JavaScript basics, see the <a href=\"https:\/\/buhave.com\/courses\/java-script\/basics-and-syntax\/\">Basics and Syntax<\/a> lesson.<\/p>\n<p style=\"text-align: center\"><em>if (condition) {<\/em><br \/>\n<em>\/\/ code runs if condition is true<\/em><br \/>\n<em>} else if (anotherCondition) {<\/em><br \/>\n<em>\/\/ runs if first is false, this is true<\/em><br \/>\n<em>} else {<\/em><br \/>\n<em>\/\/ runs if none of the above are true<\/em><br \/>\n<em>}<\/em><\/p>\n<h3>2. if Statement<\/h3>\n<p>Executes a block of code only if a condition is true. This pattern is common in real-world apps such as input validation and access control, as covered in the <a href=\"https:\/\/buhave.com\/courses\/java-script\/real-world-applications\/\">Real-World Applications<\/a> lesson.<\/p>\n<p style=\"text-align: center\"><em>let age = 18;<\/em><\/p>\n<p style=\"text-align: center\"><em>if (age &gt;= 18) {<\/em><br \/>\n<em>console.log(&#8220;You are an adult.&#8221;);<\/em><br \/>\n<em>}<\/em><\/p>\n<ul>\n<li>If age is 18 or more, the message is printed.<\/li>\n<li>If not, nothing happens.<\/li>\n<\/ul>\n<h3>3. else Statement<\/h3>\n<p>Provides an alternative block if the if condition is false.<\/p>\n<p style=\"text-align: center\"><em>let age = 16;<\/em><\/p>\n<p style=\"text-align: center\"><em>if (age &gt;= 18) {<\/em><br \/>\n<em>console.log(&#8220;You are an adult.&#8221;);<\/em><br \/>\n<em>} else {<\/em><br \/>\n<em>console.log(&#8220;You are a minor.&#8221;);<\/em><br \/>\n<em>}<\/em><\/p>\n<ul>\n<li>Outputs &#8220;You are a minor.&#8221;<\/li>\n<\/ul>\n<h3>4. else if Statement<\/h3>\n<p>Allows you to check multiple conditions in sequence.<\/p>\n<p style=\"text-align: center\"><em>let score = 85;<\/em><\/p>\n<p style=\"text-align: center\"><em>if (score &gt;= 90) {<\/em><br \/>\n<em>console.log(&#8220;Grade: A&#8221;);<\/em><br \/>\n<em>} else if (score &gt;= 80) {<\/em><br \/>\n<em>console.log(&#8220;Grade: B&#8221;);<\/em><br \/>\n<em>} else if (score &gt;= 70) {<\/em><br \/>\n<em>console.log(&#8220;Grade: C&#8221;);<\/em><br \/>\n<em>} else {<\/em><br \/>\n<em>console.log(&#8220;Grade: F&#8221;);<\/em><br \/>\n<em>}<\/em><\/p>\n<ul>\n<li>Only the first true condition block will run.<\/li>\n<\/ul>\n<h3>5. Comparison Operators in Conditions<\/h3>\n<table>\n<thead>\n<tr>\n<th>Operator<\/th>\n<th>Meaning<\/th>\n<th>Example<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"operator\">==<\/td>\n<td>Equal (loose)<\/td>\n<td class=\"example\">x == &#8220;5&#8221;<\/td>\n<\/tr>\n<tr>\n<td class=\"operator\">===<\/td>\n<td>Equal (strict)<\/td>\n<td class=\"example\">x === 5<\/td>\n<\/tr>\n<tr>\n<td class=\"operator\">!=<\/td>\n<td>Not equal (loose)<\/td>\n<td class=\"example\">x != &#8220;5&#8221;<\/td>\n<\/tr>\n<tr>\n<td class=\"operator\">!==<\/td>\n<td>Not equal (strict)<\/td>\n<td class=\"example\">x !== 5<\/td>\n<\/tr>\n<tr>\n<td class=\"operator\">&gt;<\/td>\n<td>Greater than<\/td>\n<td class=\"example\">x &gt; 10<\/td>\n<\/tr>\n<tr>\n<td class=\"operator\">&lt;<\/td>\n<td>Less than<\/td>\n<td class=\"example\">x &lt; 10<\/td>\n<\/tr>\n<tr>\n<td class=\"operator\">&gt;=<\/td>\n<td>Greater or equal<\/td>\n<td class=\"example\">x &gt;= 10<\/td>\n<\/tr>\n<tr>\n<td class=\"operator\">&lt;=<\/td>\n<td>Less or equal<\/td>\n<td class=\"example\">x &lt;= 10<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"note\">Note: Always prefer strict equality (=== and !==) to avoid type coercion issues.<\/p>\n<p>For practical examples of applying comparisons in conditions, see the <a href=\"https:\/\/buhave.com\/courses\/java-script\/loops-and-iteration\/\">Loops and Iteration<\/a> lesson.<\/p>\n<h3>6. Example \u2013 Login Check<\/h3>\n<p style=\"text-align: center\"><em>let username = &#8220;admin&#8221;;<\/em><br \/>\n<em>let password = &#8220;1234&#8221;;<\/em><\/p>\n<p style=\"text-align: center\"><em>if (username === &#8220;admin&#8221; &amp;&amp; password === &#8220;1234&#8221;) {<\/em><br \/>\n<em>console.log(&#8220;Login successful&#8221;);<\/em><br \/>\n<em>} else {<\/em><br \/>\n<em>console.log(&#8220;Invalid credentials&#8221;);<\/em><br \/>\n<em>}<\/em><\/p>\n<h3>7. Nested if Statements<\/h3>\n<p>You can nest one if inside another for more complex logic.<\/p>\n<p style=\"text-align: center\">let temp = 25;<\/p>\n<p style=\"text-align: center\">if (temp &gt; 0) {<br \/>\nif (temp &lt; 30) {<br \/>\nconsole.log(&#8220;Nice weather&#8221;);<br \/>\n}<br \/>\n}<\/p>\n<p>Nested conditionals are a common pattern when you need to check multiple related criteria. This concept is further explored in the <a href=\"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/\">Functions and Scope<\/a> lesson for managing scope and structure inside blocks.<\/p>\n<h3>8. Best Practices<\/h3>\n<ul>\n<li>Use === instead of == to avoid type coercion bugs.<\/li>\n<li>Keep conditions simple and readable.<\/li>\n<li>Use brackets {} even for one-line blocks to avoid logic errors.<\/li>\n<li>Avoid deeply nested ifs \u2014 consider switch, functions, or guards.<\/li>\n<\/ul>\n<h2>Switch statement<\/h2>\n<p>The switch statement in JavaScript is used to execute one block of code among many based on the value of an expression. It offers a cleaner alternative to multiple if&#8230;else if conditions, especially when dealing with many fixed options.<\/p>\n<h3>What is the switch Statement?<\/h3>\n<p>The switch statement evaluates an expression and executes code based on the matching case. It helps simplify code and improve readability.<\/p>\n<p><strong>Syntax of JavaScript switch<\/strong><\/p>\n<p style=\"text-align: center\"><em>switch (expression) {<\/em><br \/>\n<em>case value1:<\/em><br \/>\n<em>\/\/ Code to execute if expression === value1<\/em><br \/>\n<em>break;<\/em><br \/>\n<em>case value2:<\/em><br \/>\n<em>\/\/ Code to execute if expression === value2<\/em><br \/>\n<em>break;<\/em><br \/>\n<em>&#8230;<\/em><br \/>\n<em>default:<\/em><br \/>\n<em>\/\/ Code to execute if no case matches<\/em><br \/>\n<em>}<\/em><\/p>\n<ul>\n<li>expression: A value or variable you want to compare.<\/li>\n<li>case: Each possible match for the expression.<\/li>\n<li>break: Ends the current case block. Without it, execution continues (fall-through).<\/li>\n<li>default: Optional fallback if no match is found.<\/li>\n<\/ul>\n<p><strong>Example of switch Statement<\/strong><\/p>\n<p style=\"text-align: center\"><em>let day = 3;<\/em><br \/>\n<em>let dayName;<\/em><\/p>\n<p style=\"text-align: center\"><em>switch (day) {<\/em><br \/>\n<em>case 1:<\/em><br \/>\n<em>dayName = &#8220;Monday&#8221;;<\/em><br \/>\n<em>break;<\/em><br \/>\n<em>case 2:<\/em><br \/>\n<em>dayName = &#8220;Tuesday&#8221;;<\/em><br \/>\n<em>break;<\/em><br \/>\n<em>case 3:<\/em><br \/>\n<em>dayName = &#8220;Wednesday&#8221;;<\/em><br \/>\n<em>break;<\/em><br \/>\n<em>case 4:<\/em><br \/>\n<em>dayName = &#8220;Thursday&#8221;;<\/em><br \/>\n<em>break;<\/em><br \/>\n<em>case 5:<\/em><br \/>\n<em>dayName = &#8220;Friday&#8221;;<\/em><br \/>\n<em>break;<\/em><br \/>\n<em>case 6:<\/em><br \/>\n<em>dayName = &#8220;Saturday&#8221;;<\/em><br \/>\n<em>break;<\/em><br \/>\n<em>case 7:<\/em><br \/>\n<em>dayName = &#8220;Sunday&#8221;;<\/em><br \/>\n<em>break;<\/em><br \/>\n<em>default:<\/em><br \/>\n<em>dayName = &#8220;Invalid day&#8221;;<\/em><br \/>\n<em>}<\/em><\/p>\n<p style=\"text-align: center\"><em>console.log(dayName); \/\/ Output: Wednesday<\/em><\/p>\n<p><strong>Advantages of Using switch in JavaScript<\/strong><\/p>\n<ul>\n<li>Improves code readability<\/li>\n<li>Faster execution for long conditional chains<\/li>\n<li>Clear structure for handling many values of the same variable<\/li>\n<\/ul>\n<p><strong>Why Use break in Switch?<\/strong><\/p>\n<p>The break keyword prevents fall-through, where multiple case blocks execute unintentionally.<\/p>\n<p style=\"text-align: center\"><em>let color = &#8220;blue&#8221;;<\/em><\/p>\n<p style=\"text-align: center\"><em>switch (color) {<\/em><br \/>\n<em>case &#8220;red&#8221;:<\/em><br \/>\n<em>console.log(&#8220;Color is red&#8221;);<\/em><br \/>\n<em>break;<\/em><br \/>\n<em>case &#8220;blue&#8221;:<\/em><br \/>\n<em>console.log(&#8220;Color is blue&#8221;);<\/em><br \/>\n<em>break;<\/em><br \/>\n<em>case &#8220;green&#8221;:<\/em><br \/>\n<em>console.log(&#8220;Color is green&#8221;);<\/em><br \/>\n<em>break;<\/em><br \/>\n<em>default:<\/em><br \/>\n<em>console.log(&#8220;Color not recognized&#8221;);<\/em><br \/>\n<em>}<\/em><\/p>\n<p>Without break, JavaScript will execute all statements after the matched case, even if they don\u2019t match.<\/p>\n<h3>Grouping Multiple Cases Together<\/h3>\n<p><strong>Sometimes different cases share the same code block:<\/strong><\/p>\n<p style=\"text-align: center\"><em>let grade = &#8220;B&#8221;;<\/em><\/p>\n<p style=\"text-align: center\"><em>switch (grade) {<\/em><br \/>\n<em>case &#8220;A&#8221;:<\/em><br \/>\n<em>case &#8220;B&#8221;:<\/em><br \/>\n<em>case &#8220;C&#8221;:<\/em><br \/>\n<em>console.log(&#8220;You passed!&#8221;);<\/em><br \/>\n<em>break;<\/em><br \/>\n<em>case &#8220;D&#8221;:<\/em><br \/>\n<em>case &#8220;F&#8221;:<\/em><br \/>\n<em>console.log(&#8220;You failed.&#8221;);<\/em><br \/>\n<em>break;<\/em><br \/>\n<em>default:<\/em><br \/>\n<em>console.log(&#8220;Invalid grade&#8221;);<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>switch vs if&#8230;else in JavaScript<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>Feature<\/th>\n<th><span class=\"code\">switch<\/span><\/th>\n<th><span class=\"code\">if&#8230;else<\/span><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"feature\">Best for<\/td>\n<td>Multiple equality checks<\/td>\n<td>Complex conditions, ranges<\/td>\n<\/tr>\n<tr>\n<td class=\"feature\">Readability<\/td>\n<td>High for many values<\/td>\n<td>Better for logic combinations<\/td>\n<\/tr>\n<tr>\n<td class=\"feature\">Type of comparison<\/td>\n<td>Strict (===)<\/td>\n<td>Can be any condition<\/td>\n<\/tr>\n<tr>\n<td class=\"feature\">Performance<\/td>\n<td>Slightly better for many cases<\/td>\n<td>Comparable<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"note\"><strong>Tip:<\/strong> Use <span class=\"code\">switch<\/span> when comparing the same variable against multiple values,<br \/>\nand <span class=\"code\">if&#8230;else<\/span> when dealing with complex conditions or ranges.<\/p>\n<p><strong>Best Practices for Using switch<\/strong><\/p>\n<ul>\n<li>Use break in each case to avoid unwanted execution.<\/li>\n<li>Always include a default case for unknown values.<\/li>\n<li>Avoid complex expressions inside switch \u2014 keep it simple and readable.<\/li>\n<li>Ideal for menus, input validation, status checks, etc.<\/li>\n<\/ul>\n<p><strong>Conclusion:<\/strong> Why Use the switch Statement in JavaScript?<br \/>\nThe JavaScript switch statement is a clean, fast, and efficient way to handle multiple possible values for a single variable. Whether you&#8217;re building a web app, working on form validation, or setting up game logic, switch statements help keep your code readable and maintainable.<\/p>\n<h2>Truthy and falsy values<\/h2>\n<p>In JavaScript, every value has an inherent boolean value \u2014 either truthy or falsy \u2014 when used in a boolean context (e.g., inside if statements or logical operations).<\/p>\n<h3>Falsy Values<\/h3>\n<p>There are only 7 falsy values in JavaScript. These values evaluate to false when converted to a boolean.<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n<thead>\n<tr>\n<th>Value<\/th>\n<th>Type<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>false<\/td>\n<td>Boolean<\/td>\n<td>Literal false<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>Number<\/td>\n<td>Zero<\/td>\n<\/tr>\n<tr>\n<td>-0<\/td>\n<td>Number<\/td>\n<td>Negative zero<\/td>\n<\/tr>\n<tr>\n<td>0n<\/td>\n<td>BigInt<\/td>\n<td>Zero BigInt<\/td>\n<\/tr>\n<tr>\n<td>&#8220;&#8221;<\/td>\n<td>String<\/td>\n<td>Empty string<\/td>\n<\/tr>\n<tr>\n<td>null<\/td>\n<td>Object<\/td>\n<td>Represents no value<\/td>\n<\/tr>\n<tr>\n<td>undefined<\/td>\n<td>Undefined<\/td>\n<td>Variable not assigned a value<\/td>\n<\/tr>\n<tr>\n<td>NaN<\/td>\n<td>Number<\/td>\n<td>Not-a-Number<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>if (!0) {<\/em><br \/>\n<em>console.log(&#8220;This is falsy&#8221;);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>\/\/ Output: This is falsy<\/em><\/p>\n<p><strong>Truthy Values:<\/strong><\/p>\n<p>Everything else in JavaScript is considered truthy \u2014 it evaluates to true in a boolean context.<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n<thead>\n<tr>\n<th>Example Value<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>&#8220;hello&#8221;<\/td>\n<td>Non-empty string<\/td>\n<\/tr>\n<tr>\n<td>42<\/td>\n<td>Any non-zero number<\/td>\n<\/tr>\n<tr>\n<td>-42<\/td>\n<td>Negative numbers too<\/td>\n<\/tr>\n<tr>\n<td>[]<\/td>\n<td>Empty array<\/td>\n<\/tr>\n<tr>\n<td>{}<\/td>\n<td>Empty object<\/td>\n<\/tr>\n<tr>\n<td>function() {}<\/td>\n<td>Any function<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>if (&#8220;hello&#8221;) {<\/em><br \/>\n<em>console.log(&#8220;This is truthy&#8221;);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>\/\/ Output: This is truthy<\/em><\/p>\n<p><strong>Using Truthy\/Falsy in Conditions<\/strong><\/p>\n<p style=\"text-align: center\"><em>let name = &#8220;&#8221;;<\/em><\/p>\n<p style=\"text-align: center\"><em>if (name) {<\/em><br \/>\n<em>console.log(&#8220;Name is set&#8221;);<\/em><br \/>\n<em>} else {<\/em><br \/>\n<em>console.log(&#8220;Name is empty&#8221;);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>\/\/ Output: Name is empty<\/em><\/p>\n<p><strong>Common Pitfalls<\/strong><\/p>\n<p>0, &#8220;&#8221;, and null all evaluate to false, so be careful when checking values like:<\/p>\n<p style=\"text-align: center\"><em>let userInput = 0;<\/em><\/p>\n<p style=\"text-align: center\"><em>if (userInput) {<\/em><br \/>\n<em>console.log(&#8220;Input provided&#8221;);<\/em><br \/>\n<em>} else {<\/em><br \/>\n<em>console.log(&#8220;No input&#8221;);<\/em><br \/>\n<em>}<\/em><br \/>\n<em>\/\/ Output: No input \u2014 even though userInput is 0<\/em><\/p>\n<p><strong>Fix using explicit check:<\/strong><\/p>\n<p style=\"text-align: center\"><em>if (userInput !== null &amp;&amp; userInput !== undefined) {<\/em><br \/>\n<em>console.log(&#8220;Input provided&#8221;);<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>Boolean Conversion with Boolean() or !!<\/strong><\/p>\n<p>You can explicitly convert values:<\/p>\n<p style=\"text-align: center\"><em>Boolean(&#8220;&#8221;) \/\/ false<\/em><br \/>\n<em>Boolean(&#8220;Hi&#8221;) \/\/ true<\/em><br \/>\n<em>!!0 \/\/ false<\/em><br \/>\n<em>!![1, 2, 3] \/\/ true<\/em><\/p>\n<p><strong>Summary Table:<\/strong><\/p>\n<table class=\"truthy-falsy-table\">\n<thead>\n<tr>\n<th>Value<\/th>\n<th>Type<\/th>\n<th>Truthy\/Falsy<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>false<\/td>\n<td>Boolean<\/td>\n<td class=\"falsy\">Falsy<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>Number<\/td>\n<td class=\"falsy\">Falsy<\/td>\n<\/tr>\n<tr>\n<td>null<\/td>\n<td>Object<\/td>\n<td class=\"falsy\">Falsy<\/td>\n<\/tr>\n<tr>\n<td>undefined<\/td>\n<td>Undefined<\/td>\n<td class=\"falsy\">Falsy<\/td>\n<\/tr>\n<tr>\n<td>&#8220;abc&#8221;<\/td>\n<td>String<\/td>\n<td class=\"truthy\">Truthy<\/td>\n<\/tr>\n<tr>\n<td>{}<\/td>\n<td>Object<\/td>\n<td class=\"truthy\">Truthy<\/td>\n<\/tr>\n<tr>\n<td>[]<\/td>\n<td>Object<\/td>\n<td class=\"truthy\">Truthy<\/td>\n<\/tr>\n<tr>\n<td>function(){}<\/td>\n<td>Function<\/td>\n<td class=\"truthy\">Truthy<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Ternary operator<\/h2>\n<h3>What is the Ternary Operator in JavaScript?<\/h3>\n<p>The ternary operator provides a compact alternative to if-else statements for simple conditional assignments.<\/p>\n<p style=\"text-align: center\"><em>let status = age &gt;= 18 ? &#8220;adult&#8221; : &#8220;minor&#8221;;<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Conditional statements (if, else, else if 1. What Are Conditional Statements? Conditional statements allow you to execute code based on [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":547,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[],"class_list":["post-546","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.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Control Flow - Java Script Course<\/title>\n<meta name=\"description\" content=\"Control flow in JavaScript refers to the order in which individual statements, conditions, and loops are executed during program execution.\" \/>\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\/control-flow\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Control Flow - Java Script Course\" \/>\n<meta property=\"og:description\" content=\"Control flow in JavaScript refers to the order in which individual statements, conditions, and loops are executed during program execution.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/buhave.com\/courses\/java-script\/control-flow\/\" \/>\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:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-06-06T13:00:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Control-Flow-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=\"7 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\\\/control-flow\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/control-flow\\\/\"},\"author\":{\"name\":\"Naveed Safdar\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\"},\"headline\":\"Control Flow\",\"datePublished\":\"2025-05-19T11:03:51+00:00\",\"dateModified\":\"2026-06-06T13:00:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/control-flow\\\/\"},\"wordCount\":1158,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/control-flow\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Control-Flow-1.webp\",\"articleSection\":[\"Java Script Course\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/control-flow\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/control-flow\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/control-flow\\\/\",\"name\":\"Control Flow - Java Script Course\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/control-flow\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/control-flow\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Control-Flow-1.webp\",\"datePublished\":\"2025-05-19T11:03:51+00:00\",\"dateModified\":\"2026-06-06T13:00:37+00:00\",\"description\":\"Control flow in JavaScript refers to the order in which individual statements, conditions, and loops are executed during program execution.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/control-flow\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/control-flow\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/control-flow\\\/#primaryimage\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Control-Flow-1.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Control-Flow-1.webp\",\"width\":1200,\"height\":628,\"caption\":\"Control Flow\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/control-flow\\\/#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\":\"Control Flow\"}]},{\"@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":"Control Flow - Java Script Course","description":"Control flow in JavaScript refers to the order in which individual statements, conditions, and loops are executed during program execution.","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\/control-flow\/","og_locale":"en_US","og_type":"article","og_title":"Control Flow - Java Script Course","og_description":"Control flow in JavaScript refers to the order in which individual statements, conditions, and loops are executed during program execution.","og_url":"https:\/\/buhave.com\/courses\/java-script\/control-flow\/","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:51+00:00","article_modified_time":"2026-06-06T13:00:37+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Control-Flow-1.webp","type":"image\/webp"}],"author":"Naveed Safdar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Naveed Safdar","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/buhave.com\/courses\/java-script\/control-flow\/#article","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/java-script\/control-flow\/"},"author":{"name":"Naveed Safdar","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca"},"headline":"Control Flow","datePublished":"2025-05-19T11:03:51+00:00","dateModified":"2026-06-06T13:00:37+00:00","mainEntityOfPage":{"@id":"https:\/\/buhave.com\/courses\/java-script\/control-flow\/"},"wordCount":1158,"commentCount":0,"publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"image":{"@id":"https:\/\/buhave.com\/courses\/java-script\/control-flow\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Control-Flow-1.webp","articleSection":["Java Script Course"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/buhave.com\/courses\/java-script\/control-flow\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/buhave.com\/courses\/java-script\/control-flow\/","url":"https:\/\/buhave.com\/courses\/java-script\/control-flow\/","name":"Control Flow - Java Script Course","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/#website"},"primaryImageOfPage":{"@id":"https:\/\/buhave.com\/courses\/java-script\/control-flow\/#primaryimage"},"image":{"@id":"https:\/\/buhave.com\/courses\/java-script\/control-flow\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Control-Flow-1.webp","datePublished":"2025-05-19T11:03:51+00:00","dateModified":"2026-06-06T13:00:37+00:00","description":"Control flow in JavaScript refers to the order in which individual statements, conditions, and loops are executed during program execution.","breadcrumb":{"@id":"https:\/\/buhave.com\/courses\/java-script\/control-flow\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/buhave.com\/courses\/java-script\/control-flow\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/java-script\/control-flow\/#primaryimage","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Control-Flow-1.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Control-Flow-1.webp","width":1200,"height":628,"caption":"Control Flow"},{"@type":"BreadcrumbList","@id":"https:\/\/buhave.com\/courses\/java-script\/control-flow\/#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":"Control Flow"}]},{"@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\/546","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=546"}],"version-history":[{"count":3,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/546\/revisions"}],"predecessor-version":[{"id":1147,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/546\/revisions\/1147"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media\/547"}],"wp:attachment":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media?parent=546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/categories?post=546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/tags?post=546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}