<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Dwaipayan C(Dtech-Dbug)</title>
    <description>The latest articles on Forem by Dwaipayan C(Dtech-Dbug) (@dtechdbug).</description>
    <link>https://forem.com/dtechdbug</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F746801%2Fe989e25b-1c2d-4e6a-9ec1-28691f76d958.png</url>
      <title>Forem: Dwaipayan C(Dtech-Dbug)</title>
      <link>https://forem.com/dtechdbug</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dtechdbug"/>
    <language>en</language>
    <item>
      <title>JavaScript: Look! Loops!</title>
      <dc:creator>Dwaipayan C(Dtech-Dbug)</dc:creator>
      <pubDate>Thu, 23 Feb 2023 07:40:34 +0000</pubDate>
      <link>https://forem.com/dtechdbug/javascript-look-loops-4eae</link>
      <guid>https://forem.com/dtechdbug/javascript-look-loops-4eae</guid>
      <description>&lt;p&gt;Now that we have covered control flows and have a little bit of idea about conditionals, its time we move to one of the most important concepts of programming - &lt;em&gt;loops&lt;/em&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  What is s Loop?
&lt;/h3&gt;

&lt;p&gt;A loop is set of inctructions that are executed until a certain condition is met.&lt;/p&gt;

&lt;p&gt;How about a practical example? Just to reassure - it's most definitely not rocket science.&lt;/p&gt;

&lt;p&gt;Imagine, you have 4 identical boxes. One of the boxes have &lt;em&gt;insert your incentive&lt;/em&gt; what you are looking for. How would you find that?&lt;/p&gt;

&lt;p&gt;You would, most probably,search each of the boxes until you find just what you are looking for, right?&lt;/p&gt;

&lt;p&gt;In the programming paradigm, you are just essentially looping through the boxes until you find what you are looking for. &lt;/p&gt;

&lt;p&gt;Now, lets write a pseudo code for that? Shall we?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;loop&lt;/span&gt; &lt;span class="nx"&gt;through&lt;/span&gt; &lt;span class="nx"&gt;each&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="nx"&gt;boxes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;box&lt;/span&gt; &lt;span class="nx"&gt;has&lt;/span&gt; &lt;span class="nx"&gt;what&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="nx"&gt;are&lt;/span&gt; &lt;span class="nx"&gt;looking&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
 &lt;span class="k"&gt;break&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;loops&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;keep&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="nx"&gt;searching&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;until&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="nx"&gt;find&lt;/span&gt; &lt;span class="nx"&gt;what&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="nx"&gt;are&lt;/span&gt; &lt;span class="nx"&gt;looking&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That was easy.&lt;/p&gt;

&lt;p&gt;But wait. Hold On. There's a catch.&lt;br&gt;
Since all the boxes are identical, you have to make sure to somehow differentiate or mark the boxes that are already searched, without that - you could potentially end up searching the 4 boxes forever, unless ofcourse you exit/break out of it. Come to think of it?&lt;/p&gt;
&lt;h4&gt;
  
  
  What are the different kinds of loops used in programming?
&lt;/h4&gt;

&lt;p&gt;The two most widely used loops are - for-loops and while-loops.&lt;/p&gt;

&lt;p&gt;Let's discuss in short about both of them to gain a better understanding of each.&lt;/p&gt;
&lt;h4&gt;
  
  
  For-Loops
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;A for loop is a type of loop in programming that is used to execute a set of statements repeatedly for a fixed number of times.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Typically, they involve:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;a loop variable -&amp;gt; to keep track of the index of loop. Remember the 'marking' part in the practical example?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;a condition -&amp;gt; this determines on what condition will the loop stop, or in other words how long shall the loop run. In the practical example of box-searching - 'find what you are looking' was the condition. Until that was met you had to keep searching the boxes. Making sense?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;a step -&amp;gt; this increments the loop variable, after an iteration is completed to start the next iteration.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A general for loop syntax will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialise&lt;/span&gt; &lt;span class="nx"&gt;loop&lt;/span&gt; &lt;span class="nx"&gt;variable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;define&lt;/span&gt; &lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// code block&lt;/span&gt;
&lt;span class="c1"&gt;// what to do&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The control flows in this order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The condition is checked, if its true the control enters the code block, if it's false the control exits the loop.&lt;/li&gt;
&lt;li&gt;After the control is inside the code block the block is executed and the loop variable is stepped.&lt;/li&gt;
&lt;li&gt;Back to step 1.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Challenge!&lt;br&gt;
Guess the output of the below snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// guess the output?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  While-Loops
&lt;/h4&gt;

&lt;p&gt;A while loop is a type of loop in programming that repeatedly executes a block of code as long as a specified condition is true. &lt;/p&gt;

&lt;p&gt;The principles of the while-loop is pretty similar to for-loops,but the semantics are quiete different. &lt;/p&gt;

&lt;p&gt;For example a typical, while loop syntax will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// loop variable init&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
 &lt;span class="c1"&gt;// code block&lt;/span&gt;

 &lt;span class="c1"&gt;// step up or step down the loop variable&lt;/span&gt;
&lt;span class="c1"&gt;// in this case, since the loop var is initialised with 0, we will increment it by 1&lt;/span&gt;
&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: while implementing while loops, we have to be extra cautious about stepping up or stepping down the variable otherwise we might run into an infinite loop. &lt;/p&gt;

&lt;p&gt;Consider the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;RUNNING INIFINITE WHILE LOOP)
}
// this code doesnt know when to stop, therefore it wont.
// this can cause problems like memory leaks
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  When to use for and while loops?
&lt;/h4&gt;

&lt;p&gt;Anything that can be achieved with for loops can be achieved with while loops.&lt;/p&gt;

&lt;p&gt;However, they have their own specific use cases. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;For-Loops are the best when you know eaxctly how many times you want to execute the set of instructions.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;&lt;em&gt;While-Loops, when you dont know how long should a loop run, but you only know the condition on which the loop can exit.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Some important concepts/keywords of loops:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;break&lt;/code&gt; : the break keyword can be used to exit a loop prematurely when a certain condition is met, saving the time to iterate any further. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;continue&lt;/code&gt; : the continue keyword can be used to skip the current iteration and go to the next value. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To put things into perspective using the box-searching pseudo-code example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// considering there are 4 boxes, and one of them has your thing of interest in it&lt;/span&gt;

&lt;span class="c1"&gt;// note here i = represents the current box&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="nx"&gt;doesn&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;t have the thing of interest in it){
  continue;
}
else{
  break;
}
}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: you can initialize the loop variable with 1, but a more widely adopted approach is initializing it with 0.&lt;br&gt;
Interested to learn more? Read about 0-based indexing.&lt;/p&gt;

&lt;h3&gt;
  
  
  It's a Wrap!
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Loops are one of the most fundamental tools for problem solving.&lt;/li&gt;
&lt;li&gt;The most common types of loops are for-loops and while-loops.&lt;/li&gt;
&lt;li&gt;While both of them can be used interchangeably, they do have their own use case. &lt;/li&gt;
&lt;li&gt;For-loops are more generally used when the numbers of times the loop has to run is known or fixed.&lt;/li&gt;
&lt;li&gt;While-loops are more generally used when the number of times the loop should run is unknown and only the condition on which the loop exits is known.&lt;/li&gt;
&lt;li&gt;While-loops are particularly tricky, if not written properly as they might lead to problems like inifinite loops which in turn results to memory leaks and buggy code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With that, we have a slightly better idea about loops and how to use them in context.&lt;/p&gt;

&lt;h3&gt;
  
  
  Variations of For-loops in JS
&lt;/h3&gt;

&lt;p&gt;This part is only if you are willing to explore a little more. No harm if you skip this section.&lt;/p&gt;

&lt;p&gt;JavaScript has a couple of variations of for loops introduced in ECMAScript 2015:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;for...of: &lt;a href="https://www.w3schools.com/js/js_loop_forof.asp"&gt;read about it here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;for...in: &lt;a href="https://www.w3schools.com/jsref/jsref_forin.asp"&gt;read about it here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>JavaScript: Control Flows and Conditional Programming</title>
      <dc:creator>Dwaipayan C(Dtech-Dbug)</dc:creator>
      <pubDate>Mon, 08 Nov 2021 12:27:54 +0000</pubDate>
      <link>https://forem.com/dtechdbug/javascript-control-flows-and-conditional-programming-1a7h</link>
      <guid>https://forem.com/dtechdbug/javascript-control-flows-and-conditional-programming-1a7h</guid>
      <description>&lt;h3&gt;
  
  
  Control Flow
&lt;/h3&gt;

&lt;p&gt;If a program contains more than one statement - they are executed in an order. The order of execution is in such a way that it tells a story, from top to bottom.&lt;/p&gt;

&lt;p&gt;Consider the below snippet :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dtech-Dbug&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;My Name is :&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;myName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//o/p : My Name is :Dtech-Dbug&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above snippet, although hard-coded, really tells a story :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First we define a variable named &lt;code&gt;myName&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Then we print that in the console.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note :&lt;/strong&gt; Console.log() is a native binding in JavaScript, a function more specifically. All it does is print anything you pass in between the parenthesis onto the console.&lt;/p&gt;

&lt;p&gt;A rather simple schematic representation of the above code block would be a top-down arrow.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conditional Flows
&lt;/h3&gt;

&lt;p&gt;Not all the time will programs be super simple and a straight and simple top down arrow.&lt;br&gt;
There can(and WILL) very well be cases where the program needs to be branched and the execution needs to follow a certain branch based on the current situation at hand.&lt;/p&gt;

&lt;p&gt;Let us consider a real world scenario where branching and execution based on branching occurs.&lt;/p&gt;

&lt;p&gt;Imagine this is 2014 and you are excited to watch the FIFA WC final between Argentina and Germany. You are a supporter of Argentina and decided that if Argentina wins then you would drink a beer to celebrate.&lt;/p&gt;

&lt;p&gt;If we transform the above paragraph into pseudo-codes it will pretty simply look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Argentina&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;Drink&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;beer&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;celebrate&lt;/span&gt; &lt;span class="nx"&gt;all&lt;/span&gt; &lt;span class="nx"&gt;night&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, this is not actual code. This is a pseudo-code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pseudo-Code&lt;/strong&gt; : are text based details that help in designing algorithms or code.&lt;/p&gt;

&lt;p&gt;Pseudo-Codes can be very easily be converted into actual codes, as you can guess, by the looks of the above snippet.&lt;/p&gt;

&lt;p&gt;Back to the snippet of pseudo-code. While the pseudo-code talks about the situation that might follow the win of Argentina, it does not say anything about any other possibilities or occurrence.&lt;/p&gt;

&lt;p&gt;Now, if you remember the final game that year, Argentina did not make it. 😭😭 Because Germany Scored at 113' and bagged the WC. :')&lt;/p&gt;

&lt;p&gt;You were prepared for only if Argentina wins and that did not quite happen. You clearly missed out to think the opposite case i.e, what happens when Argentina does not win! So, what now?&lt;/p&gt;

&lt;p&gt;Well it turns out in programming(and in real world) - the onus is on the programmer to design an algorithm or code that is self defensive by handling the various cases that may occur.&lt;/p&gt;

&lt;p&gt;With that being mentioned, the previous snippet can be reworked to look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Argentina&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;Drink&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;beer&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;celebrate&lt;/span&gt; &lt;span class="nx"&gt;all&lt;/span&gt; &lt;span class="nx"&gt;night&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Germany&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;Drink&lt;/span&gt; &lt;span class="nx"&gt;three&lt;/span&gt; &lt;span class="nx"&gt;beers&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;forget&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;pain&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the program or the pseudo-code more precisely, is quite self-defensive as it is handling another possibility.&lt;/p&gt;

&lt;p&gt;It turns out in a crucial game like the FIFA WC - a match between two teams, say A and B, can really have one of two possibilities. Either A wins or B wins, neglecting any other factors that might cause the game to stop like natural causes.&lt;/p&gt;

&lt;p&gt;With this information at our disposal we can further modify the pseudo-code by thinking like, there can only be two situations in the final:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;either Argentina wins&lt;/li&gt;
&lt;li&gt;or Argentina does not win - i.e, Germany wins&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With this informal language, we can re-design the pseudo-code to look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// the situation when Argentina Wins&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Argentina&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;Drink&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;beer&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;celebrate&lt;/span&gt; &lt;span class="nx"&gt;all&lt;/span&gt; &lt;span class="nx"&gt;night&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// the situation when Argentina does not win i.e, Germany wins&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;Drink&lt;/span&gt; &lt;span class="nx"&gt;three&lt;/span&gt; &lt;span class="nx"&gt;beers&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;forget&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;pain&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code It Yourself (CIY)
&lt;/h3&gt;

&lt;p&gt;Imagine a problem statement where you are to categorize a number within the categories: Even, Prime, Odd, Even-Prime and print the categories onto the console.&lt;/p&gt;

&lt;p&gt;If you remember your numbers,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Even numbers are numbers completely divisible by 2 - i.e, leaves 0 as remainder.&lt;/li&gt;
&lt;li&gt;Prime numbers are numbers which are only divisible by itself and 1.&lt;/li&gt;
&lt;li&gt;Odd numbers are numbers which are not even - i.e, they are not completely divisible by 2.&lt;/li&gt;
&lt;li&gt;Even Prime numbers are numbers which have feats of both Even numbers and Prime numbers. There exists only one such number and that is 2.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, how shall we go about the problem and write a code that solves the given problem statement?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A little bit of interruption here - I would highly encourage you to try to start forming the pseudo-codes yourself and refer here if you are stuck. If you are very new to programming or JavaScript please stick by, sorry for the interruption.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Forming the Pseudo-Code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// if number is 0&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;equal&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Not a natural number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// if number is 2, it is even-prime&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;equal&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Even Prime Number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// check for prime&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;equal&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;equal&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Prime Number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// check for even&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;equal&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Even Number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// check for odd&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Odd Number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty verbose as it is but this is the pseudo-code that covers all the instances.&lt;br&gt;
And it follows a specific branch of execution for different values of the number - that is mentioned repeatedly in the pseudo-code.&lt;br&gt;
The actual code can be very well transformed from this snippet itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Points to note&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;we are preemptively checking for the case where the number can be 0. Bc, technically there is no reason not for that occurrence. And it is always a good idea to think and handle all the edge cases and make the program more self-defensive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the &lt;code&gt;%&lt;/code&gt; operator is called the modulo operator. It is an arithmetic operator that quite simply returns the remainder of integer divisions. So the expression &lt;code&gt;4 % 2&lt;/code&gt; basically returns the remainder value after dividing 4 by 2 - which is 0. Therefore, all even numbers have 0 as remainders when divided by 2.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;while using conditionals, there can be a lot of branches and you can use nested conditionals. Nested conditionals is a fancy term meaning using conditionals inside conditionals. Notice the &lt;code&gt;if-else&lt;/code&gt; blocks inside the first &lt;code&gt;else&lt;/code&gt; block. That is nested conditional so&lt;br&gt;
to speak.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There you have your first take home assignment - transform the pseudo-code into actual code and share it on Twitter or Linkedin and tag me, if you will! 🤗🥰&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping Up
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Execution of statements in a program occurs in a specific flow which essentially tells a story - a story that the programmer wrote and asked the machine to decipher. 📕&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Not all programs are easy and just a straight road. 🦕&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;More than many programs are branched and the execution follows a certain situation at hand.These kind of execution is called &lt;strong&gt;&lt;em&gt;conditional execution&lt;/em&gt;&lt;/strong&gt;. And the statements which represent conditional execution are typically in the form of &lt;code&gt;if(p) then q&lt;/code&gt; and they called conditional statements/sentences. 🦦&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the form &lt;code&gt;if(p) then q;&lt;/code&gt; p is more mathematically referred to as the &lt;strong&gt;hypothesis&lt;/strong&gt; of the conditional and q is called the &lt;strong&gt;conclusion&lt;/strong&gt; of the conditional. From our FIFA WC example above: Drinking beer and celebrating all night was a conclusion to the hypothesis if Argentina Wins. 🍺&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The onus is on us programmers to make the program as self defensive as possible by handling not just one obvious case but other cases as well. Analogous to the form 👇&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;then&lt;/span&gt; &lt;span class="nx"&gt;q&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;then&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The control only enters a block if the hypothesis is true. Consider the below snippet 🐳
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// x === 3 is hypothesis&lt;/span&gt;
  &lt;span class="c1"&gt;// hypothesis is false; since we declared x to be 2&lt;/span&gt;
  &lt;span class="c1"&gt;// this if block is skipped totally&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;x is three&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// control comes down to this else block&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;x is not three&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// output : x is not three&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If you are new to programming, consider solving the above assignment and tag me on twitter/linkedin w/ the end code. 🤝&lt;/li&gt;
&lt;li&gt;If you found this article a good read consider leaving a reaction or sharing the article w/ your friends who share similar interests. Comment below if you don't get something and I would be happy to cooperate. 🧙‍♂️&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Wanna talk about tech, life or even share a song suggestion?&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://linkfolio-dee.netlify.app/"&gt;Find me here 🧙‍♂️&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Relevant Hyperlinks
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dwaipayan.hashnode.dev/functions-algorithms-and-logic"&gt;What are Algorithms, in simple terms?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>javascript</category>
      <category>100daysofcode</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JavaScript: The Beginning</title>
      <dc:creator>Dwaipayan C(Dtech-Dbug)</dc:creator>
      <pubDate>Sun, 07 Nov 2021 09:28:47 +0000</pubDate>
      <link>https://forem.com/dtechdbug/javascript-the-beginning-3g6g</link>
      <guid>https://forem.com/dtechdbug/javascript-the-beginning-3g6g</guid>
      <description>&lt;h2&gt;
  
  
  Program Structures
&lt;/h2&gt;

&lt;p&gt;Learning programming is very less about memorizing the cryptic syntactic sugars and more of the programming fundamentals itself the bits which makes a program. Because the fundamentals are the same everywhere, across all programming languages - the syntactic sugars differ.&lt;/p&gt;

&lt;p&gt;Although we will be using JavaScript to understand the concepts and the glossy details. But rest assured most of the concepts are shared across all programming languages.&lt;/p&gt;

&lt;p&gt;Let us quickly go through the scope of this blog.&lt;/p&gt;

&lt;h3&gt;
  
  
  Table of contents
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Expressions? 🤯&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Statements? 🤔&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What are bindings? 😵&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Binding Conventions  😎&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Environment   🌳&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Conventions of well-designed programs  🎨&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h4&gt;
  
  
  Expressions
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Every block/line of code that produces a value is called an expression.&lt;/li&gt;
&lt;li&gt;Every value written literally like 'Programmer' or 'JavaScript' or any numerics like 2 is an expression.&lt;/li&gt;
&lt;li&gt;An Expression within a parenthesis (expression) is also an expression.&lt;/li&gt;
&lt;li&gt;Unary or Binary Operations involving expressions is also an expression.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the beauty of language based interface - as most of English can be directly used in logic formation and deductions.&lt;/p&gt;

&lt;p&gt;Consider the following example below :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If the program has bugs or the program involves division by 0. The program will crash.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is an excellent example of the beauty of language based interface. The above example is really comprised of two sentences :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the program has bugs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;OR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the program involves division by 0&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We used two sub sentences to form a single sentence - and similarly two sub expressions to form a single expression.&lt;/p&gt;

&lt;p&gt;The above example is essentially a &lt;em&gt;conditional statement&lt;/em&gt;, in the form of &lt;code&gt;if(p or q) then r&lt;/code&gt;.&lt;br&gt;
Where p , q, r are just statement variables which can be substituted by actual english.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;If an expression corresponds to a sentence fragment, a JavaScript statement corresponds to a full sentence. A program is a list of statements.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note :&lt;/strong&gt; A statement is an expression that has a consistent truth value. i.e, it can either be true or false but never both. On the contrary, a sentence is an expression that bears inconsistent truth values meaning for some cases it may be true and other cases false.&lt;/p&gt;


&lt;h4&gt;
  
  
  Statements
&lt;/h4&gt;

&lt;p&gt;We introduced the fundamenetals of statements just a few lines ago.&lt;br&gt;
The simplest statement in programming paradigm is an expression that ends with a semi-colon.&lt;/p&gt;

&lt;p&gt;Example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is it. That is a statement. Although very trivial and useless admittedly, but does not change the fact that the above lines are two statements.&lt;/p&gt;

&lt;p&gt;🎯 &lt;strong&gt;A statement only amounts to something when it affects the environment(in context of the program scope). It could be something as simple as printing something on the console or updating a variable which is then used by some other block of code. Either cases the statements are likely to affect the program and it's state. These changes and similar changes alike are called &lt;em&gt;SIDE EFFECTS&lt;/em&gt; .&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; JavaScript is lenient sometimes and ignores missing semi-colons in certain cases. But the best practice would be to use them - in order to ignore the nuances of missing terminating characters.&lt;/p&gt;




&lt;h3&gt;
  
  
  How does JavaScript keep an internal state or remember values? 🤔
&lt;/h3&gt;

&lt;p&gt;To catch and hold values, JavaScript uses something called &lt;strong&gt;Bindings&lt;/strong&gt; or simply variables.&lt;br&gt;
Consider the snippet below :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;Greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World, JavaScript is amazing!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above line is literally indicating a binding and it is another kind of statement.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The special keyword &lt;code&gt;let&lt;/code&gt; indicates that the sentence is going to define a binding.&lt;br&gt;
(More about these special keywords, soon)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Followed by the name of the binding.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And we immediately assigned a value to the binding by using the &lt;code&gt;=&lt;/code&gt; and an expression, which is &lt;code&gt;Hello World, JavaScript is amazing!&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code&gt;=&lt;/code&gt; in programming is assignment operator. Not equality operator!&lt;/p&gt;

&lt;p&gt;By the example statement - it creates a binding called Greet to point towards the value &lt;code&gt;Hello World, JavaScript is amazing!&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When a binding points towards a value - it does not essentially gets tied to it forever.&lt;br&gt;
Meaning, that at any point of time, we can use the &lt;code&gt;=&lt;/code&gt; operator again on existing bindings to make it point towards a new value.&lt;/p&gt;

&lt;p&gt;Continuation of the previous example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;Greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello Reader&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;Question&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;How are you liking JavaScript?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As obvious as it seems - we made the binding named &lt;em&gt;Greet&lt;/em&gt; point to a new value now. And also defined a new binding named &lt;em&gt;Question&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;When bindings are defined, we can use their names as expressions. Example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;Greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello Reader&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;Question&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;How are you liking JavaScript?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Greet&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;Question&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// the binding named welcome will hold the value "Hello Reader How are you liking JavaScript?"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above example may seem a little cryptic if you are an absolute beginner to programming. But what it is doing essentially is just concatenating two strings because we used an expression that includes a binary operation between two expressions &lt;code&gt;Greet + Question&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Consider a simple example for usage of defined bindings as expressions.&lt;br&gt;
Imagine Santa Clause 🎅 gives you 1000$ on Christmas and again 2000$ on New Year's eve.&lt;br&gt;
The equivalent code will look like :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;FirstGiftBySanta&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;SecondGiftBySanta&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;FirstGiftBySanta&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;SecondGiftBySanta&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//output : 3000 (1000 + 2000)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;When you define a binding, and do not assign it any value to point at, it ends up pointing to a bogus address/garbage values. Hence if you try to retrieve the value of such a binding you are likely to see &lt;code&gt;undefined&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;But, you can assign a value to it later in your code. Example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;LuckyNumber&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;LuckyNumber&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//output : undefined&lt;/span&gt;

&lt;span class="nx"&gt;LuckyNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;LuckyNumber&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//output : 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; To assign value to a pre-defined binding you do not have to implictly use the special &lt;code&gt;let&lt;/code&gt; keyword again. Why? We have already used it to define a binding &lt;code&gt;LuckyNumber&lt;/code&gt; in the above snippet.But not essentially asked it to point at a value. So in order to make a predefined binding point at something, we just simply use the &lt;code&gt;=&lt;/code&gt; operator and the expression we want the binding to grab or point at.&lt;/p&gt;

&lt;p&gt;You can also use a single special &lt;code&gt;let&lt;/code&gt; keyword to define more than bindings seperated by a comma. Like :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;WholeNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;NaturalNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;EvenNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Wholenumber&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;NaturalNumber&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;EvenNumber&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//output : 0 + 1 + 2 = 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this moment, let us not get into the nuances of the special keywords like &lt;code&gt;let, var, const&lt;/code&gt;&lt;br&gt;
Although similar, they have subtle difference.&lt;br&gt;
Check them out in details here, in this fine blog by FreeCodeCamp : &lt;a href="https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/"&gt;let vs. var vs. const in js&lt;/a&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  Binding Names
&lt;/h3&gt;

&lt;p&gt;Naming identifiers or bindings or variables is one of the things most people struggle with. But the best practise is &lt;strong&gt;naming it in such a way that is self explanatory and relevant to it's purpose&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Consider the below snippet :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sweet&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//output : Sweet&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;Dessert&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sweet&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Dessert&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//output : Sweet&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both the code blocks essentially does the same thing and produces the same value. But they differ largely in the way they are defined. A person can't just read x, and guess what it is pointing at, on the other hand the variable &lt;code&gt;Dessert&lt;/code&gt; is much more sensible than just &lt;code&gt;x&lt;/code&gt;. So any person reading the code can guess what the variable &lt;code&gt;Dessert&lt;/code&gt; is all about and be completely clueless about &lt;code&gt;x&lt;/code&gt;, unless they get to line where &lt;code&gt;x&lt;/code&gt; is defined.&lt;/p&gt;

&lt;p&gt;Although it does not make much sense or difference in toy programs like these, but in real world scenarios where the codebase is usually large and messy - naming convention has to be followed for better productivity and not waste time guessing what an identifier identifies!&lt;/p&gt;

&lt;h4&gt;
  
  
  Naming conventions
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Use identifiers relevant to it's purpose and avoid random variables.&lt;/li&gt;
&lt;li&gt;You can use characters, numerics to name variables but don't start a variable name with a digit.&lt;/li&gt;
&lt;li&gt;You can use sepcial characters like &lt;code&gt;_&lt;/code&gt; or &lt;code&gt;$&lt;/code&gt; to name variables, but no other special characters.&lt;/li&gt;
&lt;li&gt;You can not use reserved keywords to name variables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Reserved keywords are the bindings used by the program itself, native bindings of the program. Some examples of reserved keywords in JavaScript are :&lt;br&gt;
&lt;code&gt;catch try finally console propmt window process... etc.&lt;/code&gt;&lt;br&gt;
&lt;em&gt;The list is super long and does not need to be memorized.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Environment
&lt;/h3&gt;

&lt;p&gt;The ecosystem within which all the bindings and their values exist at a given point of time is called the &lt;em&gt;environmet&lt;/em&gt; or in a more fancy term &lt;em&gt;the world of the program&lt;/em&gt;.&lt;br&gt;
On startup, the environment is not empty as it contains the native bindings that are part of the program itself.Bindings like the reserved keywords and etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conventions Of writing well-designed programs
&lt;/h3&gt;

&lt;p&gt;There is no single answer to good coding conventions, just a few shared practices amongst developers keeping &lt;strong&gt;readability, reusability and maintainability&lt;/strong&gt; in mind. Below are some aspects that when taken care of can skyrocket productivity.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Identifiers -&lt;/strong&gt; Let us agree that naming bindings/varaiales or more mathematically identifiers is hard. It is really tempting to give it a random name to save time (which is totally okay for testing and trials) but it comes with more problems than benefits. Random names are not explanatory and anyone reasing the code will certainly be lost trying to guess what the identifier identifies, unless they get to the line where the identifier is defined. The best convention is to assign &lt;em&gt;self explanatory, and names relevant to the binding's purpose&lt;/em&gt;. Hence, ditch all those &lt;code&gt;x , y , z&lt;/code&gt; as far as possible and go for &lt;code&gt;userInput, arrayOfShoppingItems, primeNumber&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Indentations -&lt;/strong&gt; This is one of the aspects that makes codes more readable. Honestly, the computer does not care if you are indenting the codes or writing everything in a single line. Infact, you can write an entire program in a single line and if it is correct it will execute just fine. More interestingly, the machine codes that all the codes we write ultimately gets converted to (by the compiler software) for the computer to read and execute is a giant cryptic line of code which is super messy and absolutely not comprehensible by human. But the purpose of indentation is to make the code more readable - so that in the future you, the programmer or any other programmer working on the codebase can actually visually discern between the each statements and hence read the codes efficiently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Terminating Character -&lt;/strong&gt; Though there are some cases where absence of a &lt;code&gt;;&lt;/code&gt; does not affect the program(in High-level languages only). But it is the best practice to use it so as to save the nuances of remembering the scenarios where a &lt;code&gt;;&lt;/code&gt; matters and where not. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;That is it. Congratulations for writing your very own program 🎊&lt;/p&gt;

&lt;h3&gt;
  
  
  Acknowledgements and Hyperlinks
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.google.com/url?sa=i&amp;amp;url=https%3A%2F%2Fwww.wearedevelopers.com%2Fmagazine%2Ftop-programming-languages-to-learn&amp;amp;psig=AOvVaw3weFeCuIZ_XOALFRKu5uaa&amp;amp;ust=1636363124886000&amp;amp;source=images&amp;amp;cd=vfe&amp;amp;ved=0CAsQjRxqFwoTCPj-_e_1hfQCFQAAAAAdAAAAABAo"&gt;Cover Image: Google&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="(https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/)"&gt;FreeCodeCamp: let vs. var vs. const&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
