<?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: Dev Chhaniyara</title>
    <description>The latest articles on DEV Community by Dev Chhaniyara (@oneofthedevs).</description>
    <link>https://dev.to/oneofthedevs</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%2F469308%2F185a6e18-5507-40d8-85e9-87965c84d4f4.png</url>
      <title>DEV Community: Dev Chhaniyara</title>
      <link>https://dev.to/oneofthedevs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oneofthedevs"/>
    <language>en</language>
    <item>
      <title>Docker + Angular + Nginx</title>
      <dc:creator>Dev Chhaniyara</dc:creator>
      <pubDate>Mon, 23 Aug 2021 02:51:43 +0000</pubDate>
      <link>https://dev.to/oneofthedevs/docker-angular-nginx-37e4</link>
      <guid>https://dev.to/oneofthedevs/docker-angular-nginx-37e4</guid>
      <description>&lt;p&gt;So recently, I have been digging into DevOps and Cloud Technologies and came across this service called Google Cloud Run which lets you host your application, but the only thing that bugged me was it required a docker image, and at this point, I had no idea what docker, containers, images or any of this words meant. 😕&lt;/p&gt;

&lt;p&gt;So like any sane person with no social life on weekends 🤪 I started learning about Docker and at first, it did seem a little challenging but eventually, I think the process is quite easy and interesting.&lt;/p&gt;

&lt;p&gt;So, if you have no idea about Docker and containers, I have put some links at the bottom which were super useful to me.&lt;/p&gt;

&lt;h5&gt;
  
  
  But here's a summary of Docker 🐳
&lt;/h5&gt;

&lt;p&gt;Docker is a container runtime. That's it 🤯. It allows us to run containerized applications.&lt;/p&gt;

&lt;h5&gt;
  
  
  So what are containers? 📦
&lt;/h5&gt;

&lt;p&gt;Containers are lightweight, portable and isolated processes running on your system using the same OS kernel and user space but each with its own set of specific dependencies that do not conflict with each other.&lt;/p&gt;

&lt;h5&gt;
  
  
  And Images? 🙄
&lt;/h5&gt;

&lt;p&gt;A Docker image is a file used to execute code in a container. It contains application code, libraries, tools, dependencies and other files needed to run the application.&lt;/p&gt;

&lt;p&gt;So without wasting any more time, let's get started with the main topic &lt;/p&gt;

&lt;h1&gt;
  
  
  Dockerizing an Angular application
&lt;/h1&gt;

&lt;h5&gt;
  
  
  Now obviously there are certain things you need to have installed in your system
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Docker (&lt;a href="https://www.docker.com/products/docker-desktop"&gt;Download link&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;NodeJS (&lt;a href="https://nodejs.org/en/"&gt;Download link&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Angular CLI (&lt;a href="https://angular.io/guide/setup-local"&gt;Download link&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Visual studio code (Optional) (&lt;a href="https://code.visualstudio.com/"&gt;Download link&lt;/a&gt;) &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Step 1: Creating an Angular Application
&lt;/h3&gt;

&lt;p&gt;This one is pretty basic, we aren't going to build anything fancy here, just the pre-built template will work for this tutorial.&lt;/p&gt;

&lt;p&gt;To create your angular application, just open the terminal in the folder where you want to create your app, and write the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;ng new my-docker-angular-app
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now once this is done you should have an angular app generated, open it in VS code&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Creating Dockerfile and .dockerignore
&lt;/h3&gt;

&lt;h5&gt;
  
  
  Dockerfile
&lt;/h5&gt;

&lt;p&gt;In the main folder of your application, create a new file and name it "&lt;strong&gt;Dockerfile&lt;/strong&gt;". In the file, write the following commands&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;### STAGE 1:BUILD ###&lt;/span&gt;
&lt;span class="c"&gt;# Defining a node image to be used as giving it an alias of "build"&lt;/span&gt;
&lt;span class="c"&gt;# Which version of Node image to use depends on project dependencies &lt;/span&gt;
&lt;span class="c"&gt;# This is needed to build and compile our code &lt;/span&gt;
&lt;span class="c"&gt;# while generating the docker image&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; node:12.14-alpine AS build&lt;/span&gt;
&lt;span class="c"&gt;# Create a Virtual directory inside the docker image&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /dist/src/app&lt;/span&gt;
&lt;span class="c"&gt;# Copy files to virtual directory&lt;/span&gt;
&lt;span class="c"&gt;# COPY package.json package-lock.json ./&lt;/span&gt;
&lt;span class="c"&gt;# Run command in Virtual directory&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;npm cache clean &lt;span class="nt"&gt;--force&lt;/span&gt;
&lt;span class="c"&gt;# Copy files from local machine to virtual directory in docker image&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;npm run build &lt;span class="nt"&gt;--prod&lt;/span&gt;


&lt;span class="c"&gt;### STAGE 2:RUN ###&lt;/span&gt;
&lt;span class="c"&gt;# Defining nginx image to be used&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; nginx:latest AS ngi&lt;/span&gt;
&lt;span class="c"&gt;# Copying compiled code and nginx config to different folder&lt;/span&gt;
&lt;span class="c"&gt;# NOTE: This path may change according to your project's output folder &lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=build /dist/src/app/dist/my-docker-angular-app /usr/share/nginx/html&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; /nginx.conf  /etc/nginx/conf.d/default.conf&lt;/span&gt;
&lt;span class="c"&gt;# Exposing a port, here it means that inside the container &lt;/span&gt;
&lt;span class="c"&gt;# the app will be using Port 80 while running&lt;/span&gt;
&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 80&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  DockerIgnore
&lt;/h5&gt;

&lt;p&gt;If you have worked with git and know .gitignore, .dockerignore does the same thing, it specifies the files that we want to ignore while creating our docker image&lt;br&gt;
Normally it may contain node_modules, test files, etc...&lt;br&gt;
To create dockerignore, just create a file and name "&lt;strong&gt;.dockerignore&lt;/strong&gt;"&lt;br&gt;
For our application, just write the following inside the file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.git
.editorconfig
/.vscode/*
/node_modules
/e2e
/docs
.gitignore
*.zip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Create nginx.conf
&lt;/h3&gt;

&lt;p&gt;We'll be using Nginx to host the angular build inside the container. So for this, we need to create a configuration file for nginx.&lt;br&gt;
Create a file inside main folder and name it "&lt;strong&gt;nginx.conf&lt;/strong&gt;".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: We're telling nginx to listen to port 80 here as that is the port we had exposed in Dockerfile (refer). This needs to be same as what we defined in there&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="k"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kn"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kn"&gt;sendfile&lt;/span&gt; &lt;span class="no"&gt;on&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kn"&gt;default_type&lt;/span&gt; &lt;span class="nc"&gt;application/octet-stream&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kn"&gt;gzip&lt;/span&gt; &lt;span class="no"&gt;on&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kn"&gt;gzip_http_version&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="s"&gt;.1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kn"&gt;gzip_disable&lt;/span&gt;      &lt;span class="s"&gt;"MSIE&lt;/span&gt; &lt;span class="s"&gt;[1-6]&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="s"&gt;."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kn"&gt;gzip_min_length&lt;/span&gt;   &lt;span class="mi"&gt;256&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kn"&gt;gzip_vary&lt;/span&gt;         &lt;span class="no"&gt;on&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kn"&gt;gzip_proxied&lt;/span&gt;      &lt;span class="s"&gt;expired&lt;/span&gt; &lt;span class="s"&gt;no-cache&lt;/span&gt; &lt;span class="s"&gt;no-store&lt;/span&gt; &lt;span class="s"&gt;private&lt;/span&gt; &lt;span class="s"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kn"&gt;gzip_types&lt;/span&gt;        &lt;span class="nc"&gt;text/plain&lt;/span&gt; &lt;span class="nc"&gt;text/css&lt;/span&gt; &lt;span class="nc"&gt;application/json&lt;/span&gt; &lt;span class="nc"&gt;application/javascript&lt;/span&gt; &lt;span class="nc"&gt;application/x-javascript&lt;/span&gt; &lt;span class="nc"&gt;text/xml&lt;/span&gt; &lt;span class="nc"&gt;application/xml&lt;/span&gt; &lt;span class="nc"&gt;application/xml&lt;/span&gt;&lt;span class="s"&gt;+rss&lt;/span&gt; &lt;span class="nc"&gt;text/javascript&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kn"&gt;gzip_comp_level&lt;/span&gt;   &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kn"&gt;root&lt;/span&gt; &lt;span class="n"&gt;/usr/share/nginx/html&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kn"&gt;location&lt;/span&gt; &lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;try_files&lt;/span&gt; &lt;span class="nv"&gt;$uri&lt;/span&gt; &lt;span class="nv"&gt;$uri&lt;/span&gt;&lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="n"&gt;/index.html&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;404&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;h3&gt;
  
  
  Step 4: Creating a docker image
&lt;/h3&gt;

&lt;p&gt;To create a docker image, open a terminal in your project folder and write the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;docker build -t ng-docker-app:v1.0.0 -f ./Dockerfile .
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-t: Tag (if not specified, docker will take "latest" by default)&lt;br&gt;
-f: File (Write the path to your Dockerfile)&lt;/p&gt;

&lt;p&gt;After this, we should have a docker image created in your system. To get the list of docker images in your system, write the following in the terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;docker image ls
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 5: Create a docker container
&lt;/h3&gt;

&lt;p&gt;To create and host your docker container, write the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;docker run -p 8000:80 -d ng-docker-app:v1.0.0
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-p: Port&lt;br&gt;
Here you'll need to define a port on which the container will be hosted and the port on which app is hosted inside the container&lt;br&gt;
Syntex: &amp;lt;host-port&amp;gt;:&amp;lt;docker-port&amp;gt;&lt;br&gt;
-d: Detach &lt;br&gt;
If this is not specified, the docker will keep the console running&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: We had exposed port 80 in Dockerfile and assigned nginx to listen to port 80, so &amp;lt;docker-port&amp;gt; has to be the same here as well&lt;/p&gt;

&lt;p&gt;To get the list of currently running containers in your system, you can get this by typing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;docker container ls
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Finally
&lt;/h3&gt;

&lt;p&gt;Voila!!🎉 If you have followed each step, you should have your docker container running on Port 8000 and your application running on &lt;a href="http://localhost:8000"&gt;localhost:8000&lt;/a&gt; 😎&lt;/p&gt;

&lt;h1&gt;
  
  
  Referances
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://medium.com/@tushar0618/installing-docker-desktop-on-window-10-501e594fc5eb"&gt;How to install docker on windows 10?&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/crowdbotics/a-complete-one-by-one-guide-to-install-docker-on-your-mac-os-using-homebrew-e818eb4cfc3"&gt;How to install docker on Mac OS?&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-18-04"&gt;How to install docker on Ubuntu&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.freecodecamp.org/news/what-is-docker-used-for-a-docker-container-tutorial-for-beginners/"&gt;Freecodecamp - What is docker used for?&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.freecodecamp.org/news/the-docker-handbook/"&gt;Freecodecamp - Docker handbook&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.ibm.com/in-en/cloud/learn/docker#toc-docker-too-_SKIsywS"&gt;IBM - What is docker?&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>docker</category>
      <category>nginx</category>
    </item>
    <item>
      <title>Basic JS Interview: var,  let and const</title>
      <dc:creator>Dev Chhaniyara</dc:creator>
      <pubDate>Tue, 30 Mar 2021 11:41:42 +0000</pubDate>
      <link>https://dev.to/oneofthedevs/basic-js-interview-var-let-and-const-4bd3</link>
      <guid>https://dev.to/oneofthedevs/basic-js-interview-var-let-and-const-4bd3</guid>
      <description>&lt;p&gt;&lt;strong&gt;"What's the difference between var, let and const?".&lt;/strong&gt; Okay, this is a question asked in almost all the javascript interviews and something I did get messed up in the beginning. So here let me show how I got my head around this topic with an easy example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NmWA3zN---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A3GjWzYVaEqoQJivk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NmWA3zN---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A3GjWzYVaEqoQJivk.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before ES6, var was the only keyword you could use to declare a variable, but it had some problems. So in ES6, let and const were introduced to rectify those problems. Now let and const are almost similar in many ways so for the sake of simplicity I'll first get into differences between var and let and then move to let and const.&lt;/p&gt;




&lt;p&gt;The first context in which var and let are different is how they interpret scope. What is a scope you may ask? Well in basic terms &lt;strong&gt;Scope determines the accessibility of variables to JavaScript.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In any programming language, there are 2 main types of scope.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Global Scope&lt;/strong&gt;: Anything declared outside of all { } is considered to be in the global scope.&lt;br&gt;
&lt;strong&gt;2. Local Scope&lt;/strong&gt;: Anything declared inside any { }, that is considered to be in the local scope.&lt;/p&gt;

&lt;p&gt;Here, &lt;strong&gt;var is function scoped&lt;/strong&gt; which means it only recognizes functions as having a separate scope. This can lead to something like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WnI64Rfl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AouO7PcqxiUDDa38q6jmWCA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WnI64Rfl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AouO7PcqxiUDDa38q6jmWCA.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have used any other programming language you can see that i shouldn't be allowed to exist outside the loop but while being declared with var, it can. Now let's see how the same scenario will play out using let.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--InFacH6b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AzFbWVaxTvy652cazKs1J5A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--InFacH6b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AzFbWVaxTvy652cazKs1J5A.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see we get an error as i does not exist outside the scope of the for loop. The same type of scenarios can be generated using if statement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E2TWaTsm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AtFUA3BohF3lOoTiQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E2TWaTsm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AtFUA3BohF3lOoTiQ.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;The second context in which var and let differs is hoisting. Hoisting in itself is a little tricky to understand but in layman's terms &lt;strong&gt;Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution.&lt;/strong&gt; Variables declared with var are hoisted which leads to cases such as this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BVP8j6IQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AImbwaGevE2yd1hnO10C-RQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BVP8j6IQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AImbwaGevE2yd1hnO10C-RQ.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The output is undefined even though the variable is declared after we tried to print it, that is because during hoisting all variable and function declaration are moved to the top, also notice that we are getting undefined instead of 100, this is because hoisting only declares the variable it's assignment happens as per the normal execution of the code. But in many projects getting undefined instead of error can lead to problems, so this can be solved using let.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xz8Tjsr7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AFfldkdM_IVX2IRWAfSPByA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xz8Tjsr7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AFfldkdM_IVX2IRWAfSPByA.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;The third and last difference is variables declared using &lt;strong&gt;var can be redeclared&lt;/strong&gt; without getting an error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qCLpF87a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A1VPPR5TEbl5J7Stb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qCLpF87a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A1VPPR5TEbl5J7Stb.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This can lead to major problems when you're dealing with files with thousands of lines of code (also please try to keep the variable name as meaningful as you can).&lt;/p&gt;




&lt;p&gt;Alright, those were the major differences between var and let and coming to let and const. The only difference between let and const is that let can be reassigned and const can be not (mutable and immutable).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nyUaJaZq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1200/1%2ABhuOmgildDg-3s-0oxra7A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nyUaJaZq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1200/1%2ABhuOmgildDg-3s-0oxra7A.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can still change the values inside an object or array even though it is declared with a const keyword.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RtU1EB2i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2Aj0x3WGZIZKM4qm5YLm54nA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RtU1EB2i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2Aj0x3WGZIZKM4qm5YLm54nA.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is because of how memory allocation happens for those elements. Values here are stored as a reference and not as the actual value itself.&lt;/p&gt;




&lt;p&gt;Conclusion&lt;br&gt;
In this article, we went through the key differences between var, let and const as well as the basics of scopes and hoisting in JavaScript. As you saw, var had some issues and let &amp;amp; const are introduces to rectify those issue, so my suggestion would be to avoid using var as much as possible. I know there are still many legacy code projects that use var and will continue to do so, but at-least when building new projects, use the newer and cooler stuff 😎&lt;/p&gt;




&lt;p&gt;References &lt;br&gt;
&lt;a href="https://medium.com/r/?url=https%3A%2F%2Fwww.digitalocean.com%2Fcommunity%2Ftutorials%2Funderstanding-variables-scope-hoisting-in-javascript"&gt;Understanding Variables, Scope, and Hoisting in JavaScript | DigitalOcean&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/"&gt;Var, Let, and Const - What's the Difference? (freecodecamp.org)&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>variables</category>
    </item>
    <item>
      <title>My first blog post</title>
      <dc:creator>Dev Chhaniyara</dc:creator>
      <pubDate>Sun, 07 Mar 2021 20:43:12 +0000</pubDate>
      <link>https://dev.to/oneofthedevs/my-first-blog-post-1pid</link>
      <guid>https://dev.to/oneofthedevs/my-first-blog-post-1pid</guid>
      <description>&lt;p&gt;So as you can tell, this is my first (ever) blog post, hopefully not the last, I'll most probably write about Web-Development, HTML, CSS and javascript in a fun way (will try to use a lot of memes) but as I learn new stuff, I'll try to write about them too.&lt;/p&gt;

&lt;p&gt;So, will probably see you in the next post.&lt;/p&gt;

&lt;p&gt;Cheers😉&lt;/p&gt;

</description>
      <category>firstpost</category>
    </item>
  </channel>
</rss>
