<?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: Shikhar Saxena</title>
    <description>The latest articles on DEV Community by Shikhar Saxena (@shikharsaxena98).</description>
    <link>https://dev.to/shikharsaxena98</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%2F413846%2Fc2825da9-91bf-4a3e-9eaa-f444cc45cf7a.jpeg</url>
      <title>DEV Community: Shikhar Saxena</title>
      <link>https://dev.to/shikharsaxena98</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shikharsaxena98"/>
    <language>en</language>
    <item>
      <title>Scopes in Javascript</title>
      <dc:creator>Shikhar Saxena</dc:creator>
      <pubDate>Mon, 26 Apr 2021 15:11:06 +0000</pubDate>
      <link>https://dev.to/shikharsaxena98/scopes-in-javascript-kh1</link>
      <guid>https://dev.to/shikharsaxena98/scopes-in-javascript-kh1</guid>
      <description>&lt;p&gt;Scope in Javascript basically means the area of code where a particular variable or function is available.&lt;br&gt;
There are 2 types of scopes in Javascript - &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Function Scope&lt;/li&gt;
&lt;li&gt;Block scope&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This article will cover both of them in detail and we will try to understand what they mean. If you have prior experience in other languages like Java, Python, etc., you might find it a bit weird, but I hope that everyone will have a clear understanding of how scopes work in Javascript by the end of this article.&lt;/p&gt;

&lt;p&gt;P.S. - You can check out the TL;DR at the bottom of the article&lt;/p&gt;
&lt;h1&gt;
  
  
  Function Scope
&lt;/h1&gt;

&lt;p&gt;Let us begin by guessing the output of this simple example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function foo() {
    if(true) {
        var x = 10;
    }
    console.log(x);
}
foo(); // ??
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you answered undefined or some error, then you'd be surprised to see that this prints &lt;code&gt;10&lt;/code&gt; to the console.&lt;/p&gt;

&lt;p&gt;Let's dive deeper to understand what is going on under the hood.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the example
&lt;/h2&gt;

&lt;p&gt;When the function &lt;code&gt;foo&lt;/code&gt; is called, javascript creates a new execution context for &lt;code&gt;foo&lt;/code&gt; and puts it on top of the call stack.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Execution context&lt;/code&gt; contains all the variables and functions that are defined inside the function scope. So before the execution of the function starts, it gets all the variables defined in the &lt;code&gt;foo&lt;/code&gt; function(i.e. x) and puts a value of &lt;code&gt;undefined&lt;/code&gt; to them.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Another point to note here is that what we see is &lt;br&gt;
&lt;code&gt;var x = 10;&lt;/code&gt;, whereas the JS engine breaks it into two lines&lt;br&gt;
&lt;code&gt;var x;&lt;/code&gt; and &lt;code&gt;x=10&lt;/code&gt; i.e the variable declaration and function assignment. &lt;br&gt;
This is broken up and put into tp be put into the Abstract Syntax Tree which is outside the scope of this article.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now as the function continues its execution, it assigns the value 10 to x and then prints it out to the console.&lt;/p&gt;

&lt;p&gt;What we encountered here is called &lt;code&gt;Hoisting&lt;/code&gt;, you may have also read about hoisting where people say that while compilation the declarations are moved to the top whereas, in reality, the execution scope just parses the context and keeps the value of all defined variables as &lt;code&gt;undefined&lt;/code&gt; before execution.&lt;/p&gt;

&lt;p&gt;In function scope, all the variables declared inside the function can be used at any place inside the function even before they are declared although it is considered a bad practice to use any variable or function before declaring them.&lt;/p&gt;

&lt;h1&gt;
  
  
  Block Scope
&lt;/h1&gt;

&lt;p&gt;Let's look at a slightly modified version of our last example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function foo() {
    if(true) {
        let x = 10;
    }
    console.log(x); // ??
}

foo();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time we got a &lt;code&gt;Reference Error&lt;/code&gt; saying &lt;code&gt;x is not defined&lt;/code&gt;. This should have been the expected behavior all along.&lt;/p&gt;

&lt;p&gt;Originally JS did not have &lt;code&gt;block scope&lt;/code&gt; and it was introduced to Javascript in ES6.&lt;/p&gt;

&lt;p&gt;To understand block scope first, we need to understand what a block is.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{ ... }&lt;/code&gt; -&amp;gt; This is what a block is. A pair of curly braces and everything inside them is a block. Maybe now you understand why we got a reference error while trying to use x in the second example.&lt;/p&gt;

&lt;p&gt;But unlike function scope, you are not allowed to use the variables before declaring them. (Because &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are not hoisted)&lt;/p&gt;

&lt;p&gt;Note - Both function and block scope have a reference to their lexical environment which is used when a certain variable is not found in the same environment.&lt;/p&gt;

&lt;h1&gt;
  
  
  TL;DR
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;Function scope&lt;/code&gt; is when variables defined in an execution context are available throughout the execution context.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Block scope&lt;/code&gt; is when variables defined in a scope are available only in the current scope and not outside the scope even though it is still in the same execution context.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;In this article, we discussed the two different kinds of scopes in Javascript and discussed them in detail.&lt;/p&gt;

&lt;p&gt;You can learn more about Scopes in this famous book &lt;a href="https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/scope%20&amp;amp;%20closures/README.md#you-dont-know-js-scope--closures"&gt;Scopes and Closures&lt;/a&gt; by Kyle Simpson.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Javascript - forEach vs map method in Arrays</title>
      <dc:creator>Shikhar Saxena</dc:creator>
      <pubDate>Thu, 01 Apr 2021 18:20:55 +0000</pubDate>
      <link>https://dev.to/shikharsaxena98/javascript-foreach-vs-map-method-in-arrays-15fj</link>
      <guid>https://dev.to/shikharsaxena98/javascript-foreach-vs-map-method-in-arrays-15fj</guid>
      <description>&lt;p&gt;Javascript provides us with different functions to make our development life easy but if you have worked with Javascript for a little while, you understand how weird bugs can pop up if we don't understand the complete need of a function. Arrays in javascript provide two different functions to iterate through an array, &lt;code&gt;Array.prototype.forEach&lt;/code&gt; and &lt;code&gt;Array.prototype.map&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Definition
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;map&lt;/code&gt; takes one mandatory parameter as an argument which is a function (callback function) that defines what is required to be done to the element. It expects a value to be returned in the callback function and the map() returns a &lt;code&gt;new array&lt;/code&gt; with the specified modifications on each value of the original value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1,2,3]
let result = arr.map(val=&amp;gt;{
    return val*2
})
console.log(result) // [2,4,6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;forEach&lt;/code&gt; also takes one mandatory parameter which is the callback function that defines what needs to done to the element. Although it does not expect any value to be returned and the complete .forEach() function returns &lt;code&gt;undefined&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1,2,3]
let result = arr.forEach(val=&amp;gt;{
    console.log(val*2) // 2 4 6
})
console.log(result) // undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Differences
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Return Value
&lt;/h3&gt;

&lt;p&gt;The main difference between &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;forEach&lt;/code&gt; is that &lt;code&gt;map&lt;/code&gt; returns a new array whereas &lt;code&gt;forEach&lt;/code&gt; returns undefined.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Mutability
&lt;/h3&gt;

&lt;p&gt;MDN Docs of &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach"&gt;forEach&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map"&gt;map&lt;/a&gt; say the following for both of these.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;forEach&lt;/code&gt; does not mutate the array on which it is called. (However, callback may do so).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;map&lt;/code&gt; does not mutate the array on which it is called (although callback, if invoked, may do so).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What this basically means is that both of them do not change the original array but the logic inside the callback function may do so.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Performance
&lt;/h3&gt;

&lt;p&gt;Performance is arguably one of the most important part of the codebase and hence it makes sense to cover the performance of these two functions.&lt;/p&gt;

&lt;p&gt;Although since both these functions have different usecases, there is no universal way to compare both of them equally. I have used the performance APi to cover the performance of both of these functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [2,3,5,6,8,0]

let startTime = performance.now()
arr.forEach(num=&amp;gt;num*2)
let endTime = performance.now()
console.log("TIme taken by forEach is " + (endTime - startTime) +" milliseconds")
&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;let arr = [2,3,5,6,8,0]

let startTime = performance.now()
arr.map(num=&amp;gt;num*2)
let endTime = performance.now()
console.log("TIme taken by map is" + (endTime - startTime) +" milliseconds")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My observations were that map was faster when array size was small but as array size started crossing 10^5, forEach showed better performance. &lt;br&gt;
But feel free to comment down your observations for the same as well.&lt;/p&gt;

&lt;p&gt;Bonus - Another thing I noticed is that the use of for loop gave better performance in all cases but it affects the readability of code.&lt;/p&gt;
&lt;h2&gt;
  
  
  When to use map and forEach
&lt;/h2&gt;

&lt;p&gt;So, we dived deep into how the functions are working and saw how we use them in different cases but how do you figure out which one to use in what situations.&lt;/p&gt;

&lt;p&gt;As a rule of thumb, we want to use forEach in cases when we do not want to store the result of the modifications and only want to access the values and perform operations using the value.&lt;/p&gt;

&lt;p&gt;So we should use map when we require the resultant array, another advantage of using map is the ability of function chaining.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1,2,3,4,5];
const result  = arr.map(x=&amp;gt;x*2).filter(x=&amp;gt;x&amp;gt;=5)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the filter function is chained to the map function as it returns an array. This provides easily readable code and keeps the codebase clean.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Both &lt;code&gt;forEach&lt;/code&gt; and &lt;code&gt;map&lt;/code&gt; are powerful functions with different use cases but can be used to do almost everything that the other one does.&lt;/li&gt;
&lt;li&gt;Using a for loop gives better performance than both the inbuilt functions.&lt;/li&gt;
&lt;li&gt;map returns the resultant array whereas forEach returns undefined.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>node</category>
    </item>
    <item>
      <title>Getting Started with SVN</title>
      <dc:creator>Shikhar Saxena</dc:creator>
      <pubDate>Sun, 29 Nov 2020 18:42:18 +0000</pubDate>
      <link>https://dev.to/shikharsaxena98/getting-started-with-svn-5fdc</link>
      <guid>https://dev.to/shikharsaxena98/getting-started-with-svn-5fdc</guid>
      <description>&lt;p&gt;SVN is an SCM(Source Code Management) tool developed by the Apache Software Foundation. It is one of the less popular but still widely used Source Code Management tool.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;To check the version of Subversion run the following command&lt;br&gt;
&lt;code&gt;svn --version&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If SVN client is not installed on your machine, you will get back an error, otherwise, the command will return the version number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ svn --version
bash: svn: command not found
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you get back an error, SVN is not installed on your system and you need to run the following commands to install Subversion.&lt;/p&gt;

&lt;p&gt;If you are using RPM-based GNU/Linux, the default package manager is yum package manager. So you will have to run the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo yum install subversion
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the case of Debian-based GNU/Linux, the default package manager is apt package manager. So to install Subversion, run the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get install subversion
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Workflow
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Checking out a repository
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;svn checkout &amp;lt;svn-repo-url&amp;gt; &amp;lt;folder-name&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now you can cd into the svn directory and copy your project files and folders inside the directory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding files or folders to SVN
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;svn add filename&lt;/code&gt;&lt;br&gt;
&lt;code&gt;svn add foldername&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Checking in code to the server
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;svn commit -m "Commit Message"&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Now you are ready to get started with SVN and use it as an SCM in your projects.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>git</category>
      <category>java</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
