<?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: sanjeev</title>
    <description>The latest articles on DEV Community by sanjeev (@sanjeevpanday).</description>
    <link>https://dev.to/sanjeevpanday</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%2F249852%2F9bac74ea-cb56-4d35-80a7-8c313f600f43.jpeg</url>
      <title>DEV Community: sanjeev</title>
      <link>https://dev.to/sanjeevpanday</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sanjeevpanday"/>
    <language>en</language>
    <item>
      <title>javascript variable declaration: var vs let vs const </title>
      <dc:creator>sanjeev</dc:creator>
      <pubDate>Fri, 18 Oct 2019 11:07:49 +0000</pubDate>
      <link>https://dev.to/sanjeevpanday/javascript-variable-declaration-var-vs-let-vs-const-3h5f</link>
      <guid>https://dev.to/sanjeevpanday/javascript-variable-declaration-var-vs-let-vs-const-3h5f</guid>
      <description>&lt;p&gt;We have 3 different ways to declare variables in java script&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;b&gt;var&lt;/b&gt; - Variables declared with var keyword are &lt;strong&gt;function scoped&lt;/strong&gt;. What do I mean by that ? let's take an example -

&lt;pre class="highlight plaintext"&gt;
&lt;code&gt;
function sayHello(){
  for(var i=0;i&amp;lt;4;i++){
    console.log(i);
  }
  // As var is function scoped, i is available outside for loop.
  console.log(i); // This prints 4.
}
sayHello();
&lt;/code&gt;
&lt;/pre&gt;


Here, i is declared with var keyword in a for loop ( block ) and it is incremented util i &amp;lt; 4. When i becomes 4, the for loop ends and the last console.log(i) is executed.
&lt;strong&gt;Output&lt;/strong&gt;

&lt;pre class="highlight plaintext"&gt;
&lt;code&gt;
0
1
2
3
4
&lt;/code&gt;
&lt;/pre&gt;


Because of the var keyword, i is function scoped hence i is available outside the for loop as well and last console.log() prints 4.
&lt;/li&gt;

&lt;li&gt;
&lt;b&gt;let&lt;/b&gt; -  This is part of ES6 and addresses the problem related to var keyword. Variables declared with &lt;b&gt;let&lt;/b&gt; keyword are &lt;strong&gt;block scoped&lt;/strong&gt;. 
Let's consider the previous code with let 

&lt;pre class="highlight plaintext"&gt;
&lt;code&gt;
function sayHello(){
  for(let i=0;i&amp;lt;4;i++){
    console.log(i);
  }
  // let is block scoped hence i is only visible inside for block.
  console.log(i); // This line will throw an error.
}
sayHello();
&lt;/code&gt;
&lt;/pre&gt;


Because i is declared with let, i is visible inside the for loop only and the last console.log() will throw an error &lt;b&gt;ReferenceError: i is not defined&lt;/b&gt;

&lt;b&gt;Output&lt;/b&gt;

&lt;pre class="highlight plaintext"&gt;
&lt;code&gt;
0
1
2
3
Uncaught ReferenceError: i is not defined
    at sayHello (:6:15)
    at :8:1
&lt;/code&gt;
&lt;/pre&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;b&gt;const&lt;/b&gt; - const keyword is used to define constants in java scripts and are block scoped. e.g.

&lt;pre class="highlight plaintext"&gt;
&lt;code&gt;
function sayHello(){
    const i = 1;
    console.log(i);// This prints 1
    i = 2;  // i is declared as constant hence this line will throw an error.
}
sayHello();
&lt;/code&gt;
&lt;/pre&gt;

if we try to reassign i with a different value within the same block then java script engine will throw an error &lt;b&gt;TypeError: Assignment to constant variable&lt;/b&gt;

&lt;b&gt;Output&lt;/b&gt;

&lt;pre class="highlight plaintext"&gt;
&lt;code&gt;
1
Uncaught TypeError: Assignment to constant variable.
    at sayHello (:4:4)
    at :6:1
&lt;/code&gt;
&lt;/pre&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;b&gt;Conclusion:&lt;/b&gt; With var keyword we may get unexpected output, so if the value of a variable is not expected to change then &lt;b&gt;const&lt;/b&gt; keyword should be preferred else use &lt;b&gt;let&lt;/b&gt; keyword to define variables. &lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>interview</category>
    </item>
    <item>
      <title>javascript question - Hoisting</title>
      <dc:creator>sanjeev</dc:creator>
      <pubDate>Tue, 15 Oct 2019 11:15:48 +0000</pubDate>
      <link>https://dev.to/sanjeevpanday/javascript-question-hoisting-2dde</link>
      <guid>https://dev.to/sanjeevpanday/javascript-question-hoisting-2dde</guid>
      <description>&lt;p&gt;What you think, will be the output of the following code ? &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
hello();&lt;br&gt;
console.log(a);&lt;br&gt;
function hello(){&lt;br&gt;
    console.log("Hello World!");&lt;br&gt;
}&lt;br&gt;
var a = 5;&lt;br&gt;
console.log(a);&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A small hint, the output of the above code snippet depends on : - &lt;br&gt;
&lt;strong&gt;Hoisting&lt;/strong&gt; -  An interesting javascript concepts where &lt;i&gt;variable&lt;/i&gt; and &lt;i&gt;function&lt;/i&gt; declarations are allocated memory during context creation phase (phase 1) before execution&lt;/p&gt;

&lt;p&gt;Javascript engine will start executing code line by line in the execution phase (phase 2) &lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>interview</category>
    </item>
    <item>
      <title>Data Structures and Algorithms Visual Algo</title>
      <dc:creator>sanjeev</dc:creator>
      <pubDate>Tue, 15 Oct 2019 04:18:52 +0000</pubDate>
      <link>https://dev.to/sanjeevpanday/data-structures-and-algorithms-visual-algo-3n09</link>
      <guid>https://dev.to/sanjeevpanday/data-structures-and-algorithms-visual-algo-3n09</guid>
      <description>&lt;p&gt;Data structures and Algorithms are one of the most important topics in computer science. &lt;/p&gt;

&lt;p&gt;One of main challenges I personally have faced while learning Data structures and Angorithms is to visualize the steps while performing operations like insert, lookup, remove etc. &lt;/p&gt;

&lt;p&gt;Recently I came across a wonderful website which has helped me to understand DSA by providing animated visuals&lt;/p&gt;

&lt;p&gt;Please find below url -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://visualgo.net/en"&gt;https://visualgo.net/en&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Please take a look and I hope you will also find this helpful.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>datastructures</category>
      <category>visual</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
