<?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: Akash Deshpande</title>
    <description>The latest articles on DEV Community by Akash Deshpande (@akashd1995).</description>
    <link>https://dev.to/akashd1995</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%2F555355%2Fc7f7f7f2-c7f4-4146-95d0-6703b70beba3.jpg</url>
      <title>DEV Community: Akash Deshpande</title>
      <link>https://dev.to/akashd1995</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akashd1995"/>
    <language>en</language>
    <item>
      <title>Difference between var, let and const - Learn by example - JavaScript Basics</title>
      <dc:creator>Akash Deshpande</dc:creator>
      <pubDate>Tue, 10 Aug 2021 13:54:14 +0000</pubDate>
      <link>https://dev.to/akashd1995/difference-between-var-let-and-const-learn-by-example-javascript-basics-13h7</link>
      <guid>https://dev.to/akashd1995/difference-between-var-let-and-const-learn-by-example-javascript-basics-13h7</guid>
      <description>&lt;p&gt;Declaring variables in JavaScript since ES6 can be done in different ways. Most prominent of those are using &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Basic example on &lt;code&gt;var&lt;/code&gt;
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Example 1 */
x = 10;
function updatex() {
  x = 20;
}
updatex();
console.log(x); //20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Example 2 */
x = 10;
function updatex() {
  var x = 20;
}
updatex();
console.log(x); //10;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In these examples, we see that declaring variables as var limits their scope to the particular function, in this case the function &lt;code&gt;updatex()&lt;/code&gt;. If we do not declare variable as var, it climbs up to global scope, declared and initializes itself there.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Example on &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;var&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; are similar in nature, but are not the same. Let's consider following examples.&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(x); //undefined
console.log(y); //ReferenceError

var x = 10;
let y = 15;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fd-PRN6_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lk6uumujl7ie8jtpqaxi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fd-PRN6_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lk6uumujl7ie8jtpqaxi.png" alt="uncaught error"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we try to console log variables before they are declared in the program. &lt;code&gt;var&lt;/code&gt; variable logs &lt;em&gt;undefined&lt;/em&gt; and &lt;code&gt;let&lt;/code&gt; variable throws an &lt;em&gt;Uncaught ReferenceError&lt;/em&gt;. This happens because of &lt;strong&gt;hoisting&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/js/js_hoisting.asp"&gt;According to W3schools,&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this example, &lt;code&gt;var&lt;/code&gt; variable is hoisted to the top of the block, declared there, but not initialized, whereas &lt;code&gt;let&lt;/code&gt; variable is hoisted to the top of the block (i.e. the block of the code is aware of the variable) but it cannot be used until it has been declared. The time from which the variable is used till the time where the variable is initialized is called &lt;strong&gt;temporal dead zone&lt;/strong&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  3. Example on &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;So far we have seen direct declaration of variables, using &lt;code&gt;var&lt;/code&gt; and using &lt;code&gt;let&lt;/code&gt;. Now let's see &lt;code&gt;const&lt;/code&gt;. As you might have guessed, &lt;code&gt;const&lt;/code&gt; stands for constant. A constant is a variable which once declared and initialized remains the same and cannot be redeclared or changed through assignment. &lt;em&gt;Does this mean &lt;code&gt;const&lt;/code&gt; is immutable?&lt;/em&gt; Not really, but we will check that out later. First let's see the behavior of &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;. Let's consider these examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Example 1 */
// we've seen this before
x = 10; //ReferenceError
let x;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Example 2 */
y = 20;
const y; //SyntaxError
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Example 3 */
y = 20; //ReferenceError
const y = 20;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've seen example 1. It throws a Reference Error. In example 2, &lt;code&gt;y&lt;/code&gt; being a &lt;code&gt;const&lt;/code&gt; variable throws a &lt;em&gt;SyntaxError&lt;/em&gt; because after declaration, a &lt;code&gt;const&lt;/code&gt; variable must be immediately initialized. We cover this case in example 3, but as &lt;code&gt;const&lt;/code&gt; variables cannot be redeclared or reassigned, it throws a &lt;em&gt;ReferenceError&lt;/em&gt;. So a proper way to declare &lt;code&gt;const&lt;/code&gt; variable would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const y = 20;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; have same scope properties. Unlike &lt;code&gt;var&lt;/code&gt; variables, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variables are hoisted but cannot be used until declared, and in case of &lt;code&gt;const&lt;/code&gt;, initialized. Now that you have a good understanding of scope and hoisting of different types of variables in JS, let's talk about immutability of &lt;code&gt;const&lt;/code&gt; variables.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Immutability of &lt;code&gt;const&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;JS has 2 types of variables:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Primitive types: &lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;null&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt;, &lt;code&gt;string&lt;/code&gt;, and &lt;code&gt;symbol&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Reference types: &lt;code&gt;object&lt;/code&gt;, &lt;code&gt;array&lt;/code&gt; and &lt;code&gt;function&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Declaring a &lt;code&gt;const&lt;/code&gt; variable to a primitive makes it immutable. But if it is initialized with a non-primitive or reference type, then it is possible to mutate the &lt;code&gt;const&lt;/code&gt; variable. Let's take a few examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Example 1 */
let arr1 = [10, 20];
arr2 = [30, 40]; // No error

const arr2 = [50, 60];
arr2 = [70, 80]; // SyntaxError, redeclaraion
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Example 2 */
const arr2 = [50, 60];
arr2[0] = 70;
arr2[1] = 80;
console.log(arr2); // [70, 80]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;As you can see, with respect to reference type variables, it's not possible to preserve their immutability.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Conclusion
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;var&lt;/code&gt; variables are function scoped, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variables are block scoped. &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are hoisted, but &lt;code&gt;let&lt;/code&gt; cannot be used until declared and &lt;code&gt;const&lt;/code&gt; variables cannot be used until they are declared + initialized.&lt;/p&gt;

&lt;h3&gt;
  
  
  Outro
&lt;/h3&gt;

&lt;p&gt;Enjoyed the article?  &lt;a href="https://dev.to/akashd1995"&gt;Follow me on Dev&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Connect with me:&lt;br&gt;
&lt;a href="https://twitter.com/akashd1995/"&gt;Twitter&lt;/a&gt; | &lt;a href="https://www.linkedin.com/in/akash-deshpande/"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>How to: sticky top nav-bar (HTML + CSS)</title>
      <dc:creator>Akash Deshpande</dc:creator>
      <pubDate>Tue, 26 Jan 2021 13:29:08 +0000</pubDate>
      <link>https://dev.to/akashd1995/how-to-sticky-top-nav-bar-html-css-454j</link>
      <guid>https://dev.to/akashd1995/how-to-sticky-top-nav-bar-html-css-454j</guid>
      <description>&lt;p&gt;A common CSS newbie issue we face is, how to code a sticky top bar. This could be the nav-bar, a quick contact bar, quick access bar, etc. Let's assume we want to build a top nav-bar. So the requirements are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nav bar should be on top&lt;/li&gt;
&lt;li&gt;It should stick to the top of the page&lt;/li&gt;
&lt;li&gt;It should scroll with the page&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before we begin, there are some things you should know, they are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic knowledge of HTML&lt;/li&gt;
&lt;li&gt;Basic knowledge of CSS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Excited to learn? Let's jump in!&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Basic code block
&lt;/h3&gt;

&lt;p&gt;To make a sticky nav-bar, let's build a nav-bar first. We will use &lt;code&gt;header&lt;/code&gt; tag for this. For the rest of the content, we will use &lt;code&gt;main&lt;/code&gt; tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  &amp;lt;header&amp;gt;
  &amp;lt;/header&amp;gt;

  &amp;lt;main&amp;gt;
  &amp;lt;/main&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Let's customize a bit with CSS
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* CSS */
/* imports */
@import url('https://fonts.googleapis.com/css2?family=Lato:wght@400;900&amp;amp;display=swap');

body{
  padding: 0;
  margin: 0;
  font-family: "Lato", sans-serif;
}

header {
  height: 80px;
  background-color: #eee;
}

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

&lt;/div&gt;



&lt;p&gt;In the code above, we've added styling for body, header and main. We've also imported Lato font (because I love sans-serif fonts 😁).&lt;/p&gt;

&lt;h4&gt;
  
  
  Body tag
&lt;/h4&gt;

&lt;p&gt;We remove margin and padding on &lt;code&gt;body&lt;/code&gt; because when an element takes full width, default padding and margin of body interferes with the full width of the element, in this case, &lt;code&gt;header&lt;/code&gt; tag. &lt;/p&gt;

&lt;h4&gt;
  
  
  Header tag
&lt;/h4&gt;

&lt;p&gt;We add a static height of 80px to the &lt;code&gt;header&lt;/code&gt;, and change its color to #eee to differentiate it with rest of the content.&lt;/p&gt;

&lt;p&gt;Let's add some content to the header/nav-bar. Generally, nav-bar has a logo and some nav-links. Logo would have an image of some sort, but for ease of the tutorial, we will just replace it with text.&lt;/p&gt;

&lt;h4&gt;
  
  
  HTML Part:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;header&amp;gt;
  &amp;lt;div class="logo"&amp;gt;
    Logo
  &amp;lt;/div&amp;gt;
  &amp;lt;ul class="nav-links"&amp;gt;
    &amp;lt;li&amp;gt;
      &amp;lt;a href="https://akashdeshpande.hashnode.dev/"&amp;gt;Blog&amp;lt;/a&amp;gt;
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
      &amp;lt;a href="https://www.twitter.com/akashd1995"&amp;gt;Contact&amp;lt;/a&amp;gt;
    &amp;lt;/li&amp;gt;
  &amp;lt;/ul&amp;gt;
&amp;lt;/header&amp;gt;

&amp;lt;main&amp;gt;
&amp;lt;/main&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  CSS Part:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* imports */
@import url('https://fonts.googleapis.com/css2?family=Lato:wght@400;900&amp;amp;display=swap');

body{
  padding: 0;
  margin: 0;
  font-family: "Lato", sans-serif;
}

header {
  /* general styling */
  height: 80px;
  background-color: #eee;

  /* display part */
  display: flex;
  align-items: center;
  padding: 0 32px;
  justify-content: space-between;
}

.logo {
  font-size: 32px;
  font-weight: 900;
  color: #333;
}

.nav-links li {
  list-style: none;
  display: inline;
  padding-left: 32px;
}

.nav-links li a {
  color: #333;
  text-decoration: none;
}

.nav-links li a:hover {
  color: #ccc;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So far, we've added Logo, nav-links and customized them, so that they are in one line and horizontal.&lt;br&gt;
Important part here is the CSS display part in &lt;code&gt;header&lt;/code&gt;. We've used &lt;code&gt;display: flex&lt;/code&gt; and &lt;code&gt;justify-content: space-between&lt;/code&gt;. This places the logo and nav-links at the start and end of the container (header in this case) respectively. If you have not learned CSS flexbox so far, no worries, but it sure is a good thing to have in your skill set.&lt;/p&gt;

&lt;p&gt;If we add more content to the page, the nav-bar will scroll with the page, we want it to stick to the top at all times. We will do this in the next part. The next part is important, it contains the magic ingredient that makes it all work.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. Add the magic ingredient
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Add this to header CSS */
position: fixed;
width: 100%;
box-sizing: border-box;
top: 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Adding &lt;code&gt;position: fixed&lt;/code&gt; to &lt;code&gt;header&lt;/code&gt;'s CSS makes the &lt;code&gt;header&lt;/code&gt; stick to the top. We are basically done at this point, but it doesn't look good. The &lt;code&gt;header&lt;/code&gt;'s width has shrunk, so we will add &lt;code&gt;width: 100%&lt;/code&gt;. With this code addition, comes another issue, the width of &lt;code&gt;header&lt;/code&gt; goes outside the page. We could technically fix this by adjusting the width %, but we have a better solution. Adding &lt;code&gt;box-sizing: border-box&lt;/code&gt; to the CSS limits the width of header to 100% of the viewport, so it spans the whole width of the screen. A precautionary measure we could take here, although it won't be necessary, would be to add &lt;code&gt;top: 0&lt;/code&gt;. This is just so that our nav-bar stays at the top of the page.&lt;/p&gt;
&lt;h3&gt;
  
  
  4. Fix code
&lt;/h3&gt;

&lt;p&gt;We've completed the main step, so what remains? It is the issues we brought with our code. Let's solve those.&lt;/p&gt;
&lt;h4&gt;
  
  
  Main tag
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;main&lt;/code&gt; tag contains the rest of the page. When we put &lt;code&gt;position: absolute&lt;/code&gt; to &lt;code&gt;header&lt;/code&gt;'s CSS, the &lt;code&gt;main&lt;/code&gt; now takes up the whole page. This means that &lt;code&gt;main&lt;/code&gt; will also include the area behind the nav-bar. Since that area won't be visible, we will add &lt;code&gt;padding-top: 80px&lt;/code&gt;, the same as the height of the header to &lt;code&gt;main&lt;/code&gt;'s CSS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;main {
  padding-top: 80px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Done!
&lt;/h3&gt;

&lt;p&gt;We have successfully completed our sticky top nav-bar! If you'd like to see it in action,  &lt;a href="https://codepen.io/akashdeshpande/pen/PoGrNGJ"&gt;see the codepen here.&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/akashdeshpande/embed/PoGrNGJ?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>html</category>
      <category>css</category>
      <category>navigation</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
