<?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>DEV Community: Prashanth Vamanan Srinivasan</title>
    <description>The latest articles on DEV Community by Prashanth Vamanan Srinivasan (@sprashanthv).</description>
    <link>https://dev.to/sprashanthv</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%2F457666%2F75650e1d-1195-4240-b74f-36914eab9828.png</url>
      <title>DEV Community: Prashanth Vamanan Srinivasan</title>
      <link>https://dev.to/sprashanthv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sprashanthv"/>
    <language>en</language>
    <item>
      <title>How I made my first endless runner game in 7 days - Part I (Introduction and Basic Concepts)</title>
      <dc:creator>Prashanth Vamanan Srinivasan</dc:creator>
      <pubDate>Fri, 28 May 2021 15:30:30 +0000</pubDate>
      <link>https://dev.to/sprashanthv/how-i-made-my-first-endless-runner-game-in-7-days-part-i-introduction-and-basic-concepts-3oda</link>
      <guid>https://dev.to/sprashanthv/how-i-made-my-first-endless-runner-game-in-7-days-part-i-introduction-and-basic-concepts-3oda</guid>
      <description>&lt;h5&gt;
  
  
  Introduction
&lt;/h5&gt;

&lt;p&gt;In this post and the upcoming series of posts, I am going to share the technical concepts and development process behind my &lt;a href="https://simmer.io/@sprashanthv/jungle-runner"&gt;first Unity2D game&lt;/a&gt; which is an endless runner.&lt;/p&gt;

&lt;h5&gt;
  
  
  A sneak peek into the game
&lt;/h5&gt;

&lt;p&gt;In this game, the objective of the player character is to jump over the spawning obstacles without crashing into them. If the player collides with the obstacles, his health gets &lt;strong&gt;reduced by 10%&lt;/strong&gt; and eventually, the player dies when his health reaches zero.&lt;/p&gt;

&lt;p&gt;The ultimate goal behind the game is to survive for as long as possible without dying.&lt;/p&gt;

&lt;h5&gt;
  
  
  Gameplay
&lt;/h5&gt;

&lt;p&gt;Let's now take a sneak peek into our game before we proceed further.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/SM0fJVSqaOQ"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h5&gt;
  
  
  Gathering the Assets
&lt;/h5&gt;

&lt;p&gt;The game as mentioned is an infinite runner. It consists of a character selection screen at the beginning. To make this game we need the following assets:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Two-Character Sprites - &lt;a href="https://www.gameart2d.com/temple-run---free-sprites.html"&gt;Boy Sprite&lt;/a&gt; and &lt;a href="https://www.gameart2d.com/adventurer-girl---free-sprites.html"&gt;Girl Sprite&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Background Sprites - &lt;a href="https://craftpix.net/freebies/free-cartoon-forest-game-backgrounds/"&gt;Forest Sprites&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ground Sprite and Buttons  - &lt;a href="https://www.youtube.com/watch?v=gB1F9G0JXOo"&gt;Fahir From Awesome Tuts&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Health Bar Assets - &lt;a href="https://github.com/Brackeys/Health-Bar"&gt;Assets Owned By Brackeys&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Obstacle Sprites - &lt;a href="https://craftpix.net/freebies/free-jumping-up-game-objects/"&gt;Set 1&lt;/a&gt; and &lt;a href="https://craftpix.net/freebies/free-underwater-world-2d-game-objects/"&gt;Set 2&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Fundamental Concepts
&lt;/h5&gt;

&lt;p&gt;Now that we have the required assets, we need to understand a few fundamental concepts in Unity to further proceed to make our game.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Game Objects&lt;/li&gt;
&lt;li&gt;Components&lt;/li&gt;
&lt;li&gt;Rigid bodies &lt;/li&gt;
&lt;li&gt;Colliders&lt;/li&gt;
&lt;li&gt;Sprites&lt;/li&gt;
&lt;li&gt;Prefabs&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Game Objects
&lt;/h5&gt;

&lt;p&gt;Each entity in our game is called a game object. These are fundamental objects in Unity that can represent pretty much anything ranging from characters, props to the scenery. They can also act as containers for other game objects or components.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Examples: &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;When we drag and drop a sprite (a graphical image) into our scene a game object is automatically created which represents this sprite.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We may have a ground sprite that is very small in width and we might need to place several of these ground sprites next to each other to form the ground for our game. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In such cases, a parent empty game object is created and all these ground game objects are nested within this parent game object which acts as a container in this case.&lt;/p&gt;

&lt;h5&gt;
  
  
  Components
&lt;/h5&gt;

&lt;p&gt;Unity is a component-based system. The idea here is that we can attach components to the game objects in our scene to affect both their state (properties) and their behaviour (what they do).&lt;/p&gt;

&lt;p&gt;An example is the &lt;strong&gt;Transform&lt;/strong&gt; component which comes by default with every game object. This transform component has &lt;strong&gt;Position, Rotation and Scale&lt;/strong&gt; properties which can be tweaked as per our needs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8DT26_84--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rdjgq2iinc2za7s860xg.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8DT26_84--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rdjgq2iinc2za7s860xg.PNG" alt="Transform component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, we have a lot of &lt;strong&gt;in-built&lt;/strong&gt; components in Unity such as AudioSource, Colliders, Rigidbodies, Audio Listener etc. some of which we will see as we build our game.&lt;/p&gt;

&lt;p&gt;We can also create our own &lt;strong&gt;custom components&lt;/strong&gt; by writing C# scripts in Unity. Since Unity treats C# scripts also as components, they can be attached to any game object as we would normally do with other components.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A Real-World Example of Components and Game Objects&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Television is a game object whose job is to perform visual display. The stereo system and speakers of a TV are game objects themselves individually. &lt;/p&gt;

&lt;p&gt;Our stereo system is further composed of components such as Amplifier, radio tuner etc. These components function together inside a single game object (in this case our TV) to do its job.&lt;/p&gt;

&lt;h5&gt;
  
  
  Rigid Bodies
&lt;/h5&gt;

&lt;p&gt;A rigid body is a component which when attached to a game object enables fundamental physics behaviours on it such as force, acceleration, gravity among many others. It is a component that allows the game object to be controlled by physics.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Types of Rigid Bodies&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Static: When the body type is static the game object will neither be affected by physics forces nor due to gravity.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Kinematic: When we want the game object to be affected only by forces but not by gravity then we select the body type as Kinematic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dynamic: When we want the game object to be affected by both force and gravity then we select Dynamic as the body type.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Rigid bodies also allow us to control the mass of the game object, its angular drag a few of the important properties.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jJthiKZX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x9ub37705gl97ccu41w3.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jJthiKZX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x9ub37705gl97ccu41w3.PNG" alt="A Rigid body 2D Component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Colliders
&lt;/h5&gt;

&lt;p&gt;Colliders are the game objects that enable us to &lt;strong&gt;detect collisions&lt;/strong&gt; between game objects and also decide how to &lt;strong&gt;resolve these collisions&lt;/strong&gt; (i.e) what should happen when a collision occurs.&lt;/p&gt;

&lt;p&gt;There are various types of collider components available in Unity such as BoxCollider, EdgeCollider, PolygonCollider, CircleCollider, CapsuleCollider etc. What collider to use for a game object depends upon the geometry of it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gypATukQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mm015uplje86dsftcb7v.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gypATukQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mm015uplje86dsftcb7v.PNG" alt="Polygon Collider 2D"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Types of Collisions
&lt;/h5&gt;

&lt;p&gt;Collisions can be broadly classified into &lt;strong&gt;two&lt;/strong&gt; categories:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Trigger Collisions - There might be situations in our game where we want to detect collisions between two game objects but we want the colliding game object to be able to pass through the game object being collided with. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In such cases, we use trigger collisions. To enable such a collision we need to check the Is Trigger checkbox shown in the above image.&lt;/p&gt;

&lt;h5&gt;
  
  
  Examples of Trigger Collisions :
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;When the player collects a coin, we might need the player to pass through the coin but at the same time detect that he has collided with the coin so that it can be destroyed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the player passes through a door, we might want the boss of that level to appear. In this case, also we need to detect the collision of the player with the door but allow the player to pass through the door&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Solid Collisions - This is the default behaviour we get whenever we attach a collider component to a game object. In this type of collision, the colliding game object will be stopped from passing through by the game object being collided with.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5&gt;
  
  
  Examples of Solid Collision:
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Player colliding with obstacles in the game area&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Player colliding with enemies as a result of which the player's health gets reduced.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  C# Methods used to detect Collisions
&lt;/h5&gt;

&lt;p&gt;Unity Engine's API provides us with two separate methods (note: these methods will be covered in detail in the next series of posts) to detect the two types of collisions we discussed above&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Trigger Collisions - &lt;a href="https://docs.unity3d.com/ScriptReference/Collider2D.OnTriggerEnter2D.html"&gt;OnTriggerEnter2D()&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Solid Collisions - &lt;a href="https://docs.unity3d.com/ScriptReference/Collider2D.OnCollisionEnter2D.html"&gt;OnCollisionEnter2D()&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Sprites
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Sprites are graphical assets or images that we include in our game.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A sprite can be a single image or single frame or a set of images or frames (sprite sheets or sprite strips). If it is a set of images or frames then usually they are used to create the animations as well.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sprites in our games are rendered using the SpriteRenderer2D component.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yqIPzA4r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4tvjoawj6ao1rmbogngd.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yqIPzA4r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4tvjoawj6ao1rmbogngd.PNG" alt="SpriteRenderer2D"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is generally recommended to have sprite sheets rather than having individual images to represent the different states of the game object (such as walking, running etc.) since this is efficient both in terms of memory and performance.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Prefabs
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A prefab is a prefabricated game object. In other words, a game object which we have configured the way we want it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This is useful because once a game object is configured with all of its necessary components such as colliders, rigid bodies, sprite renderers, custom scripts etc. it is easier to add instances of these game objects directly in our game.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This is done by just dragging and dropping the prefab into the scene to create a new instance of it rather than going through the process of attaching all of these components again to create a new instance of this game object.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Example of Prefab
&lt;/h5&gt;

&lt;p&gt;We might want to spawn multiple instances of enemies in our game at repeated intervals. Rather than creating an enemy each time from scratch by attaching all the required components manually, we can create this enemy once and make it a prefab.&lt;/p&gt;

&lt;p&gt;Then whenever we need to create an instance of the enemy we can just instantiate this prefab so that the enemy is spawned in the scene.&lt;/p&gt;

&lt;h5&gt;
  
  
  Conclusion
&lt;/h5&gt;

&lt;p&gt;In this post, we have seen the required assets to make our game. We also learnt about the basic concepts in Unity needed to make our game. &lt;/p&gt;

&lt;p&gt;In the next post we will dive into the development of our game starting with the Player characters, their movement, jumping, animations as well as spawning obstacles.&lt;/p&gt;

&lt;p&gt;Until next time, stay safe and cheers :)&lt;/p&gt;

&lt;p&gt;If you would like to follow me on &lt;a href="https://twitter.com/SPrashanthV"&gt;Twitter&lt;/a&gt; please feel free to do so. I write about web development, game development and football ;)&lt;/p&gt;

</description>
    </item>
    <item>
      <title>JavaScript Internals - Part I (Execution Context)</title>
      <dc:creator>Prashanth Vamanan Srinivasan</dc:creator>
      <pubDate>Thu, 28 Jan 2021 16:01:04 +0000</pubDate>
      <link>https://dev.to/sprashanthv/javascript-internals-part-i-execution-context-4l8g</link>
      <guid>https://dev.to/sprashanthv/javascript-internals-part-i-execution-context-4l8g</guid>
      <description>&lt;h4&gt;
  
  
  Introduction
&lt;/h4&gt;

&lt;p&gt;In this series, we will be looking at some of the tricky topics and take a deep dive into the core fundamentals of the JavaScript language.&lt;/p&gt;

&lt;h5&gt;
  
  
  What happens when we execute JavaScript code ?
&lt;/h5&gt;

&lt;p&gt;Whenever we execute any JavaScript code, the JavaScript  engine behind the scenes creates what is known as an &lt;strong&gt;execution context&lt;/strong&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  Execution Context
&lt;/h5&gt;

&lt;p&gt;Assume we have a big vertical carton box in-front of us.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4zncdgd7i7qxwvhu4vnr.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4zncdgd7i7qxwvhu4vnr.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An execution context can be thought of like this. It is a big box inside which the JavaScript Engine executes all of our JavaScript code.&lt;/p&gt;

&lt;p&gt;It is also known as the &lt;strong&gt;Global Execution Context (GEC)&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Whenever a function is invoked the JS engine creates a brand new execution context (more on this in the upcoming posts).&lt;/p&gt;

&lt;p&gt;This new execution context resides within the execution context which was created when the JS program was first executed. &lt;/p&gt;

&lt;p&gt;Hence the execution context which is created first is known as the global execution context (since all other execution contexts created will reside within this).&lt;/p&gt;

&lt;h5&gt;
  
  
  What does the execution context contain ?
&lt;/h5&gt;

&lt;p&gt;The execution context is divided into two major components namely,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The Memory Component (or) Variable Environment&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Code Component (or) Thread of Execution&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  JS Program Execution with an Example
&lt;/h5&gt;

&lt;p&gt;Let us now see how the execution context along with its two major components comes into play with a simple example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feuwra5et59a9gim4210x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feuwra5et59a9gim4210x.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have a very simple JavaScript program where there is a variable a initialised with the value 10 and we have a function named greetUser which logs "Hello World" to the console.&lt;/p&gt;

&lt;p&gt;When this program is executed by the JavaScript engine, first a global execution context is created.&lt;/p&gt;

&lt;p&gt;In any execution context there are two steps which are followed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Memory creation phase&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Code execution phase&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Memory creation phase
&lt;/h5&gt;

&lt;p&gt;During the memory creation phase the JavaScript engine allocates memory to all the variables in the program and gives them a spatial or temporary value of &lt;strong&gt;undefined&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Whenever JavaScript engine sees any functions in the program during this phase the entire code inside the function is stored as such.&lt;/p&gt;

&lt;p&gt;Both the variables and functions are stored as &lt;strong&gt;key-value&lt;/strong&gt; pairs. &lt;/p&gt;

&lt;p&gt;Taking our example the memory component of our execution context looks like below: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fi4ze3m95aa60jk0uhv23.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fi4ze3m95aa60jk0uhv23.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the JavaScript engine sees that it has no more code to scan through to allocate memory and thus it proceeds to the code execution phase.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note : In the memory creation phase only the memory is allocated to the variables and functions are allocated and none of the code is executed. Hence line number 7 in the example will not be executed yet.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5&gt;
  
  
  Code execution phase
&lt;/h5&gt;

&lt;p&gt;In this phase, the JavaScript engine starts to execute our code line by line. Let us trace this through our example :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Line 1 is encountered and the JS engine updates the value of the variable a from undefined to 10.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lines 2 to 6 have nothing to be executed and hence JS engine proceeds to the next line.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Current state of the execution context&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsx9m3hhc5i5bvov81kia.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsx9m3hhc5i5bvov81kia.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Line 7 involves function invocation. Whenever a function invocation happens a new execution context is created.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JS engine creates a new execution context inside the current execution context. It goes through the memory creation phase for this execution context as well. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It sees that there are no variables or functions within our greetUser function and hence it proceeds to the code execution phase.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the code execution phase, it logs "Hello world" to the console.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3b5iwtazg45ji6txtfmy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3b5iwtazg45ji6txtfmy.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Final Steps
&lt;/h5&gt;

&lt;p&gt;Now the JS engine sees that there is no more code to be executed for the function greetUser and hence it clears the execution context allocated for it.&lt;/p&gt;

&lt;p&gt;Now it resumes back to the global execution context and sees that there is no more code to be executed here also and hence clears the global execution context as well.&lt;/p&gt;

&lt;p&gt;Now the program execution is complete.&lt;/p&gt;

&lt;h4&gt;
  
  
  Conclusion and Upcoming Post
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In this post we saw how JavaScript engine executes our program internally.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the next post we will see more such examples and I will explain the line by line execution of these examples.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Huge thanks to Akshay Saini (An engineer at Uber) for inspiring me to write this post. This series of posts his based on his playlist &lt;strong&gt;Namaste JavaScript&lt;/strong&gt; in his youtube channel. Do &lt;a href="https://www.youtube.com/playlist?list=PLlasXeu85E9cQ32gLCvAvr9vNaUccPVNP" rel="noopener noreferrer"&gt;check it out&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Until the next post, cheers and keep coding :)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScriptmas Advent Calendar 2020</title>
      <dc:creator>Prashanth Vamanan Srinivasan</dc:creator>
      <pubDate>Tue, 22 Dec 2020 08:37:44 +0000</pubDate>
      <link>https://dev.to/sprashanthv/it-s-time-for-some-javascriptmas-1j3b</link>
      <guid>https://dev.to/sprashanthv/it-s-time-for-some-javascriptmas-1j3b</guid>
      <description>&lt;p&gt;Hi all programmers out there !&lt;/p&gt;

&lt;p&gt;At the start of this month, &lt;a href="https://scrimba.com/"&gt;Scrimba&lt;/a&gt; started a fun yet thoughtful series of programming challenges using JavaScript known as JavaScriptmas.&lt;/p&gt;

&lt;p&gt;Those who aren't aware of what Scrimba is, it is an online platform teaching front-end development related technologies.&lt;/p&gt;

&lt;p&gt;The really cool thing about Scrimba is that you can edit the instructor's code as they are typing and make tweaks and see it live, which I believe is a great way of active learning or learning by doing&lt;/p&gt;

&lt;p&gt;Javascriptmas is a 24-day challenge launched by Scrimba which gives a challenge to be solved each day using JavaScript. I am a junior developer in a startup and I use javascript based technologies there on a daily basis.&lt;/p&gt;

&lt;p&gt;These series of challenges enabled me to solidify my javascript skills and also exposed me to newer ways of thinking and multiple ways of solving the same problem. &lt;/p&gt;

&lt;p&gt;So here are my solutions for each day of the Javascriptmas challenge&lt;/p&gt;

&lt;p&gt;(These are not the best solutions, just the ones which I thought were suited for the given problem)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Day 1 - &lt;a href="https://scrimba.com/scrim/co1c14000a35674f64aac9fae"&gt;Candies&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 2 - &lt;a href="https://scrimba.com/scrim/co78e4907be3d35ed9d314e12"&gt;Deposit Profit&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 3 - &lt;a href="https://scrimba.com/scrim/co78e4907be3d35ed9d314e12"&gt;Chunky Monkey&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 4 - &lt;a href="https://scrimba.com/learn/adventcalendar/note-at-1-03-co080420cbdd34f1762ffa29a"&gt;Century From Year&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 5 - &lt;a href="https://scrimba.com/learn/adventcalendar/note-at-1-13-coe30436da0f19bd8ba933248"&gt;Reverse A String&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 6 - &lt;a href="https://scrimba.com/learn/adventcalendar/note-at-0-54-coe4b4d3e8e2b44f4d4338e4d"&gt;Sort By Length&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 7 - &lt;a href="https://scrimba.com/learn/adventcalendar/note-at-1-29-co50643ef8b6a5ba625832436"&gt;Count Vowel Consonant&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 8 - &lt;a href="https://scrimba.com/learn/adventcalendar/note-at-1-28-co4e240ca9bdf629f1a64cb37"&gt;The Rolling Dice&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 9 - &lt;a href="https://scrimba.com/scrim/co6d447db9197f30ad051b90c"&gt;Sum Odd Fibonacci Numbers&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 10 - &lt;a href="https://scrimba.com/scrim/co3a44d02a3c9112845a293cf"&gt;Adjacent Elements Product&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 11 - &lt;a href="https://scrimba.com/scrim/co8f04680a92db0174d842b6d"&gt;Avoid Obstacles&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 12 - &lt;a href="https://scrimba.com/scrim/cod344632856c84c52557b5a8"&gt;Valid Time&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 13 - &lt;a href="https://scrimba.com/scrim/co1994da78b638af3aa1480c9"&gt;Extract Each Kth&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 14 - &lt;a href="https://scrimba.com/scrim/coaf8496f9049af395da4887b"&gt;Maximal Adjacent Difference&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 15 - &lt;a href="https://scrimba.com/scrim/co40941ecacfe1f6f7379afdc"&gt;JavaScript Carousel&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 16 - &lt;a href="https://scrimba.com/scrim/co0e94e9694e188e85a3a254a"&gt;Insert Dashes&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 17 - &lt;a href="https://scrimba.com/scrim/coa574deeb94b4a53edff695a"&gt;Different Symbols Naive&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 18 - &lt;a href="https://scrimba.com/scrim/co0a244e9a4ab556f0338566a"&gt;Array Previous Less&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 19 - &lt;a href="https://scrimba.com/scrim/co13245b8a9442acba2963b9d"&gt;Alphabet Subsequence&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 20 - &lt;a href="https://scrimba.com/scrim/co54a4541a20c26cee5ac9fc3"&gt;Domain Type&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 21 - &lt;a href="https://scrimba.com/scrim/co1ee4bb9b7d2b39ebd758915"&gt;Sum of 2&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 22 - &lt;a href="https://scrimba.com/scrim/co23a418eafd6747e67238572"&gt;Extract Matrix Column&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 23 - &lt;a href="https://scrimba.com/scrim/co55d4caa96a0bc10540e7c64"&gt;Social Media Input&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 24 - &lt;a href="https://scrimba.com/scrim/co8614e10b36a2cd789c51dc7"&gt;Test your agility&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Conclusion
&lt;/h4&gt;

&lt;p&gt;Taking part in such a challenge and solving a coding problem daily has taught me how to be consistent at something. Also this improves our thinking and enables us to come up with better and optimised solutions for future problems. &lt;/p&gt;

&lt;p&gt;If you haven't yet attempted JavaScriptmas, I strongly recommend you to do so as it is a lot of fun and at the same time you can win a lot of cool prizes and be part of an amazing community of like minded individuals.&lt;/p&gt;

&lt;p&gt;Until we meet next time, Happy Coding :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Beginner's Guide To Asynchronous JavaScript - Part 2</title>
      <dc:creator>Prashanth Vamanan Srinivasan</dc:creator>
      <pubDate>Thu, 03 Dec 2020 07:36:09 +0000</pubDate>
      <link>https://dev.to/sprashanthv/beginner-s-guide-to-asynchronous-javascript-part-2-1kh4</link>
      <guid>https://dev.to/sprashanthv/beginner-s-guide-to-asynchronous-javascript-part-2-1kh4</guid>
      <description>&lt;h4&gt;
  
  
  Recap
&lt;/h4&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/sprashanthv/your-guide-to-asynchronous-javascript-part-1-57pj"&gt;last post&lt;/a&gt; of this series we saw what is the difference between synchronous and asynchronous code and how JavaScript handles both of them.&lt;/p&gt;

&lt;h4&gt;
  
  
  What's in this post?
&lt;/h4&gt;

&lt;p&gt;In this article we will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Simulate asynchronous behaviour in JavaScript using setTimeout() method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Learn about HTTP requests and HTTP verbs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;APIs and API endpoints&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Asynchronous code simulation
&lt;/h4&gt;

&lt;p&gt;Assume we have the following code in a file named sandbox.js&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y63lkyG5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/23phuwqgrce37ljr992a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y63lkyG5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/23phuwqgrce37ljr992a.png" alt="Alt Text" width="880" height="590"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the setTimeout() call is asynchronous it is executed in a separate part of the browser and does not block the remaining statements. &lt;/p&gt;

&lt;p&gt;Once the 3 seconds has passed, the callback function is invoked printing the message inside the setTimeout() call.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: The handling internals of the setTimeout() call and where it is executed will be discussed in the upcoming posts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0EHPEUZY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xswwuya9dwlfyq8mszct.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0EHPEUZY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xswwuya9dwlfyq8mszct.png" alt="Alt Text" width="880" height="94"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  HTTP Requests
&lt;/h4&gt;

&lt;p&gt;HTTP is a &lt;strong&gt;protocol&lt;/strong&gt; which allows us to fetch resources from external sources. (This is the simplest possible definition).&lt;/p&gt;

&lt;p&gt;This protocol defines a set of request methods which denotes the specified action to be performed to obtain the resource. &lt;/p&gt;

&lt;p&gt;They are also known as &lt;strong&gt;request verbs&lt;/strong&gt; and there are four major ones - &lt;strong&gt;GET, POST, PATCH and DELETE&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;GET - GET requests are used for retrieving a specific resource.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;POST - POST requests are used for submitting data to an external server (such as signup, login etc.)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;PATCH - PATCH requests are used when we want to modify only certain attributes of a specific resource.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;DELETE - DELETE request is used to DELETE a specified resource.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Why it is useful?
&lt;/h4&gt;

&lt;p&gt;Whenever we develop any real-world application we might need to fetch data from external sources. The way we reach out to these external sources is by making a &lt;strong&gt;HTTP request&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Depending upon the type of HTTP verb used, the appropriate resource action (as mentioned above) will be performed.&lt;/p&gt;

&lt;h4&gt;
  
  
  API's and API Endpoints
&lt;/h4&gt;

&lt;p&gt;We make an HTTP request to something known as an &lt;strong&gt;API endpoint&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;An API (Application Programming Interface) in simplest terms allows applications to communicate with one another (as an example your application to communicate  with google' servers for logging in through gmail).&lt;/p&gt;

&lt;p&gt;This external server (google in this case) exposes to the public what is known as an &lt;strong&gt;API endpoint&lt;/strong&gt;. These are URLs that a particular API or server exposes to us which we can use to get data from these servers.&lt;/p&gt;

&lt;h4&gt;
  
  
  Illustration - HTTP request to an API endpoint
&lt;/h4&gt;

&lt;p&gt;Example API endpoint - &lt;a href="https://jsonplaceholder.typicode.com/todos/1"&gt;https://jsonplaceholder.typicode.com/todos/1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B9Kp6-G8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tteutyhmqxt0720p7cu8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B9Kp6-G8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tteutyhmqxt0720p7cu8.jpg" alt="Alt Text" width="880" height="660"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Usually most of the API's return response in a format known as JSON(JavaScript Object Notation - which will be covered later).&lt;/p&gt;

&lt;p&gt;Response data from the above request:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mTr4tnjd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rjhuwh5zmtt5r8stomsz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mTr4tnjd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rjhuwh5zmtt5r8stomsz.png" alt="Alt Text" width="880" height="535"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Typically whenever we request data from an external server we use a HTTP GET request.&lt;/p&gt;

&lt;h4&gt;
  
  
  Conclusion and Next Steps
&lt;/h4&gt;

&lt;p&gt;In this post we have seen how to simulate asynchronous code in JavaScript using setTimeout(). We also briefly touched upon the HTTP protocol, HTTP verbs, APIs and API endpoints.&lt;/p&gt;

&lt;p&gt;In the next part we will see a practical demo of how to make an HTTP request using JavaScript and discuss about the various response codes associated with HTTP requests.&lt;/p&gt;

&lt;p&gt;Thanks for reading this post and feel free to connect with me on &lt;a href="https://twitter.com/SPrashanthV"&gt;Twitter&lt;/a&gt; for more such content. Have a nice day :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Beginner's Guide To Asynchronous JavaScript - Part 1</title>
      <dc:creator>Prashanth Vamanan Srinivasan</dc:creator>
      <pubDate>Wed, 02 Dec 2020 07:38:41 +0000</pubDate>
      <link>https://dev.to/sprashanthv/your-guide-to-asynchronous-javascript-part-1-57pj</link>
      <guid>https://dev.to/sprashanthv/your-guide-to-asynchronous-javascript-part-1-57pj</guid>
      <description>&lt;h4&gt;
  
  
  What is Asynchronous Programming?
&lt;/h4&gt;

&lt;p&gt;The term asynchronous in programming means that a particular section or block of code can execute &lt;strong&gt;independently&lt;/strong&gt; of other sections of code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Use cases
&lt;/h4&gt;

&lt;p&gt;Asynchronous programming is generally useful to perform operations which consume a certain amount of time such as fetching data from a third party REST API or getting data from our own local database.&lt;/p&gt;

&lt;p&gt;We can think of asynchronous code as "&lt;strong&gt;Starting a task now and finishing it some time later&lt;/strong&gt;"&lt;/p&gt;

&lt;h4&gt;
  
  
  Synchronous vs Asynchronous
&lt;/h4&gt;

&lt;p&gt;JavaScript by design is Synchronous in nature. &lt;/p&gt;

&lt;p&gt;This means that it can &lt;strong&gt;execute only one statement&lt;/strong&gt; at any given time, meaning that the rest of the other statements need to wait before the completion of the previous statement.&lt;/p&gt;

&lt;p&gt;Hence synchronous code is also known as &lt;strong&gt;blocking&lt;/strong&gt; code.&lt;/p&gt;

&lt;p&gt;This is in contrast to asynchronous code where  other sections of code need not wait for the asynchronous piece of code to finish execution. &lt;/p&gt;

&lt;p&gt;Hence, Asynchronous code is also known as &lt;strong&gt;non-blocking&lt;/strong&gt; code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Synchronous Code - Illustration
&lt;/h4&gt;

&lt;p&gt;Assume we have a JavaScript file which contains some simple console.log() statements (printing to the console) and some statement making a network request (though we won't get into the actual asynchronous syntax just yet).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; console.log("Hello world!");   // Statement 1

 console.log("Print some random stuff") // Statement 2

 //Statement 3 - Making a network request

 console.log("Printing again") // Statement 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Considering the normal flow of execution in JavaScript, Statement 1 will be executed printing "Hello world!" to the console. Only when this has been completed Statement 2 will be executed. &lt;/p&gt;

&lt;p&gt;Since Statement 3 is a network request, it will take some time to complete and assuming the synchronous model is followed here, Statement 4 has to wait for the entire network request to complete before it can execute. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8pw_VWUg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/s9e90y1asspalw96pm4r.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8pw_VWUg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/s9e90y1asspalw96pm4r.jpg" alt="Synchronous_programming" width="880" height="660"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Statement 3 stalls or blocks the execution of Statement 4. This is the downfall of synchronous code and hence it is known as &lt;strong&gt;blocking code&lt;/strong&gt; as mentioned above. This is where asynchronous code comes into the picture.&lt;/p&gt;

&lt;h4&gt;
  
  
  Asynchronous Code - Illustration
&lt;/h4&gt;

&lt;p&gt;Instead of the statement 3 being a synchronous function to request data, let it be an asynchronous function (we will learn how to write asynchronous functions in the upcoming posts).&lt;/p&gt;

&lt;p&gt;Now what this means is that this function can &lt;strong&gt;start its execution and finish later&lt;/strong&gt; whenever the requested data is sent back from its source.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dvTr2KuI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rd5umdgnmu5eq6uj1ef2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dvTr2KuI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rd5umdgnmu5eq6uj1ef2.jpg" alt="Asynchronous Programming" width="880" height="660"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the asynchronous function completes its execution, it notifies that it has completed its task through a callback function (assume it is a function which notifies completion, though we will learn about callbacks in the upcoming posts).&lt;/p&gt;

&lt;h4&gt;
  
  
  How does this help ?
&lt;/h4&gt;

&lt;p&gt;JavaScript by nature is single-threaded which means that only one instruction can be executed at a given time (essentially the same thing as synchronous).&lt;/p&gt;

&lt;p&gt;Whenever JavaScript sees an asynchronous function call, it takes out the asynchronous call out of the single thread environment and runs it in a different part of the browser.&lt;/p&gt;

&lt;p&gt;This means that the rest of the statements can now execute without being blocked.&lt;/p&gt;

&lt;p&gt;Once the asynchronous call completes, it notifies the JavaScript engine with the response data (if any) and the application can then proceed to perform any operations with the data. &lt;/p&gt;

&lt;h4&gt;
  
  
  Conclusion
&lt;/h4&gt;

&lt;p&gt;In this post, we have seen both synchronous and asynchronous programming models. We also have seen how JavaScript handles synchronous and asynchronous blocks of code. &lt;/p&gt;

&lt;p&gt;In the upcoming posts of this series, we will talk more about asynchronous programming including the techniques used in JavaScript to achieve asynchronous programming along with a complete project.&lt;/p&gt;

&lt;p&gt;If you find this post helpful, please connect with me on &lt;a href="https://twitter.com/SPrashanthV"&gt;Twitter&lt;/a&gt; for more such interesting content.&lt;/p&gt;

&lt;p&gt;Thanks for checking out my post and have a happy day ahead :)&lt;/p&gt;

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