<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Conditionals In JavaScript]]></title><description><![CDATA[Conditionals In JavaScript]]></description><link>https://ankesh-conditional-javascript.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 11 Oct 2026 12:06:53 GMT</lastBuildDate><atom:link href="https://ankesh-conditional-javascript.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Conditionals Statements]]></title><description><![CDATA[Understanding What Is Conditional means in programing language .
Let us see what a conditional statement does in a program. It simply creates two situations for any case or statement. For example, if you want to apply a filter during online shopping ...]]></description><link>https://ankesh-conditional-javascript.hashnode.dev/conditionals-statements</link><guid isPermaLink="true">https://ankesh-conditional-javascript.hashnode.dev/conditionals-statements</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[if-else]]></category><category><![CDATA[Conditional statement]]></category><category><![CDATA[Switch case]]></category><category><![CDATA[Chaiaurcode]]></category><dc:creator><![CDATA[ANKESH KUMAR SINGH]]></dc:creator><pubDate>Sat, 15 Feb 2025 05:46:41 GMT</pubDate><content:encoded><![CDATA[<h3 id="heading-understanding-what-is-conditional-means-in-programing-language"><strong>Understanding What Is Conditional means in programing language .</strong></h3>
<p>Let us see what a conditional statement does in a program. It simply creates two situations for any case or statement. For example, if you want to apply a filter during online shopping to show shirts only up to a maximum price of 1000 rupees, the conditional statement checks the price of the product. If the price is within the range, it shows the product (true), and the remaining products are hidden from you (false). Yes, you got it right; conditionals work on true and false.</p>
<p>What are Conditional Statements?</p>
<p>In simple terms, a conditional statement checks if something is true or false and then tells the program what to do based on that. Like in real life, if you are traveling to your friend's house and you get stuck at a junction, you ask your friend which direction to take, and they suggest one. Similarly, conditionals help a program decide which path to take, depending on the conditions you set..</p>
<p>Types Of Conditional Statement</p>
<ol>
<li><p>If-else (in Hindi, we can call it Agar-Magar).</p>
<p> Let us understand with a real-life example, how conditional statements work. Imagine you are trying to decide if you should go outside for a walk. You might base your decision on the weather.</p>
<p> Here’s how it works:</p>
<ol>
<li><p><strong>Condition:</strong> If it’s sunny outside.</p>
</li>
<li><p><strong>Action if True:</strong> You decide to go for a walk.</p>
</li>
<li><p><strong>Action if False:</strong> You stay inside because it’s raining.</p>
</li>
</ol>
</li>
</ol>
<p>    This decision-making process can be represented in a program using a conditional statement.</p>
<h3 id="heading-basic-structure-of-conditional-statements"><strong>Basic Structure of Conditional Statements</strong></h3>
<p>    The basic syntax of a conditional statement looks like this:</p>
<pre><code class="lang-javascript">    Copyif condition:
        # action <span class="hljs-keyword">if</span> the condition is <span class="hljs-literal">true</span>
    <span class="hljs-attr">else</span>:
        # action <span class="hljs-keyword">if</span> the condition is <span class="hljs-literal">false</span>
</code></pre>
<p>    Expanding with Multiple Conditions</p>
<p>    In real life, we might have more complex decisions to make. For example, what if you also want to check if it’s too cold to go for a walk?</p>
<p>    Here’s how you can expand the decision-making process:</p>
<ol>
<li><p><strong>Condition 1:</strong> If it’s sunny and not too cold, you’ll go for a walk.</p>
</li>
<li><p><strong>Condition 2:</strong> If it’s sunny but too cold, you might decide to stay inside.</p>
</li>
<li><p><strong>Condition 3:</strong> If it’s rainy, you’ll definitely stay inside.</p>
</li>
</ol>
<h3 id="heading-multiple-conditions-in-code"><strong>Multiple Conditions in Code</strong></h3>
<p>    Let’s add more conditions in JavaScript:</p>
<pre><code class="lang-javascript">    Copyweather = <span class="hljs-string">"sunny"</span>
    temperature = <span class="hljs-number">10</span>  # Temperature <span class="hljs-keyword">in</span> Celsius

    <span class="hljs-keyword">if</span> weather == <span class="hljs-string">"sunny"</span> and temperature &gt;= <span class="hljs-number">15</span>:
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"You can go for a walk!"</span>)
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> weather == <span class="hljs-string">"sunny"</span> and temperature &lt; <span class="hljs-number">15</span>:
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"It's sunny, but too cold for a walk. Stay inside."</span>)
    <span class="hljs-attr">else</span>:
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"It's raining. Stay inside!"</span>)
</code></pre>
<ul>
<li><p><strong>First Condition:</strong> If the weather is "sunny" and the temperature is 15°C or higher, you go for a walk.</p>
</li>
<li><p><strong>Second Condition (else if):</strong> If it’s sunny but too cold (less than 15°C), you stay inside.</p>
</li>
<li><p><strong>Else:</strong> If it’s not sunny, and likely raining, you stay inside.</p>
</li>
</ul>
<h3 id="heading-types-of-conditional-statements"><strong>Types of Conditional Statements</strong></h3>
<ol>
<li><p><strong>If Statement:</strong> This is the simplest form of condition, checking if something is true.</p>
<pre><code class="lang-javascript"> Copy    <span class="hljs-keyword">if</span> x &gt; <span class="hljs-number">10</span>:
     <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"x is greater than 10"</span>)
</code></pre>
</li>
<li><p><strong>If-Else Statement:</strong> This gives two possible outcomes—one if the condition is true, and another if it’s false.</p>
<pre><code class="lang-javascript"> Copylet x = <span class="hljs-number">22</span> 
 <span class="hljs-keyword">if</span> x &gt; <span class="hljs-number">10</span>:
     print(<span class="hljs-string">"x is greater than 10"</span>)
 <span class="hljs-attr">else</span>:
     print(<span class="hljs-string">"x is 10 or less"</span>)
</code></pre>
</li>
</ol>
<h3 id="heading-real-world-example-in-programming"><strong>Real-World Example in Programming</strong></h3>
<p>    Imagine you are building a simple website for a bank where users can check their account balance. You want to display a message depending on the account balance:</p>
<ol>
<li><p><strong>If the balance is above $1000:</strong> You show a message saying "You are a premium customer!"</p>
</li>
<li><p><strong>If the balance is between $500 and $1000:</strong> You show a message saying "You are a regular customer."</p>
</li>
<li><p><strong>If the balance is below $500:</strong> You show a message saying "Your account balance is low."</p>
</li>
</ol>
<p>    Here’s how that might look in JavaScript:</p>
<pre><code class="lang-javascript">    Copy <span class="hljs-keyword">let</span> balance = <span class="hljs-number">700</span>

    <span class="hljs-keyword">if</span> balance &gt; <span class="hljs-number">1000</span>:
        print(<span class="hljs-string">"You are a premium customer!"</span>)
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> balance &gt;= <span class="hljs-number">500</span>:
        print(<span class="hljs-string">"You are a regular customer."</span>)
    <span class="hljs-attr">else</span>:
        print(<span class="hljs-string">"Your account balance is low."</span>)
</code></pre>
<h1 id="heading-switch-case"><strong>Switch Case</strong></h1>
<p>    What is Switch-case ?</p>
<p>    Switch case is a second type of conditional statement used in programming languages. Now you might wonder why we use switch case when we already have if-else. The answer to this question relates to a real-life scenario. Imagine you go to a restaurant for lunch. I know you are a fitness freak, but let's assume this, okay? Now, suppose you have 200 thousand rupees, and you look at the menu to find two items whose total price is under or equal to 200. If nothing is listed around 200, then you just walk out of the restaurant, which is your default case.</p>
<p>    In the same way, when a program has various cases for one expression, we use switch case.</p>
<h3 id="heading-the-switch-case-structure"><strong>The Switch-Case Structure</strong></h3>
<p>    In most programming languages that support the <strong>switch-case</strong> statement, the structure looks like this:</p>
<pre><code class="lang-javascript">    Copyswitch (expression or variable) {
        <span class="hljs-keyword">case</span> value1:
            <span class="hljs-comment">// Code to run if variable matches value1</span>
            <span class="hljs-keyword">break</span>;
        <span class="hljs-keyword">case</span> value2:
            <span class="hljs-comment">// Code to run if variable matches value2</span>
            <span class="hljs-keyword">break</span>;
        <span class="hljs-keyword">case</span> value3:
            <span class="hljs-comment">// Code to run if variable matches value3</span>
            <span class="hljs-keyword">break</span>;
        <span class="hljs-keyword">default</span>:
            <span class="hljs-comment">// Code to run if no case matches (optional)</span>
    }
</code></pre>
<h3 id="heading-real-life-example-deciding-what-to-wear"><strong>Real-Life Example: Deciding What to Wear</strong></h3>
<p>    Let’s say you’re deciding what to wear based on the day of the week. Here’s a real-world breakdown:</p>
<ul>
<li><p><strong>Monday:</strong> Wear a suit .</p>
</li>
<li><p><strong>Tuesday:</strong> Wear casual.</p>
</li>
<li><p><strong>Wednesday:</strong> Wear a suit again.</p>
</li>
<li><p><strong>Thursday:</strong> Wear something sporty (for the gym).</p>
</li>
<li><p><strong>Friday:</strong> Wear a T-shirt (casual day before the weekend).</p>
</li>
<li><p><strong>Saturday and Sunday:</strong> Holiday( weekend vibes)</p>
</li>
</ul>
<p>    let's rewrite this in a <strong>switch-case</strong> statement to simplify things:</p>
<pre><code class="lang-javascript">    Copyday = <span class="hljs-string">"Monday"</span>

    <span class="hljs-keyword">switch</span>(day):
        <span class="hljs-keyword">case</span> <span class="hljs-string">"Monday"</span>:
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Wear a suit."</span>)
            <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">case</span> <span class="hljs-string">"Tuesday"</span>:
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Wear something comfortable."</span>)
            <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">case</span> <span class="hljs-string">"Wednesday"</span>:
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Wear a suit again."</span>)
            <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">case</span> <span class="hljs-string">"Thursday"</span>:
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Wear something sporty."</span>)
            <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">case</span> <span class="hljs-string">"Friday"</span>:
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Wear a T-shirt."</span>)
            <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">case</span> <span class="hljs-string">"Saturday"</span>:
        <span class="hljs-keyword">case</span> <span class="hljs-string">"Sunday"</span>:
           <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Wear whatever you like."</span>)
            <span class="hljs-keyword">break</span>
        <span class="hljs-attr">default</span>:
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Invalid day!"</span>)
</code></pre>
<h3 id="heading-why-use-switch-case"><strong>Why Use Switch-Case?</strong></h3>
<p>    Here’s why switch-case is useful:</p>
<ol>
<li><p><strong>Clean and Readable:</strong> It’s easier to read and follow compared to many <code>if-else-if</code> statements, especially when you have many conditions to check.</p>
</li>
<li><p><strong>Efficient:</strong> For large numbers of conditions, a switch-case is more efficient, as it avoids checking each condition one by one.</p>
</li>
<li><p><strong>Flexible:</strong> Switch-case can be used to handle multiple different values for a single variable (like days of the week, menu options, or choices).</p>
</li>
</ol>
]]></content:encoded></item></channel></rss>