<?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: Satvik</title>
    <description>The latest articles on DEV Community by Satvik (@satvik1203).</description>
    <link>https://dev.to/satvik1203</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%2F644888%2F15739202-46c4-4652-b5aa-2c924310e4a4.png</url>
      <title>DEV Community: Satvik</title>
      <link>https://dev.to/satvik1203</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/satvik1203"/>
    <language>en</language>
    <item>
      <title>Event loop and queue</title>
      <dc:creator>Satvik</dc:creator>
      <pubDate>Fri, 25 Jun 2021 04:59:18 +0000</pubDate>
      <link>https://dev.to/satvik1203/event-loop-and-queue-k4b</link>
      <guid>https://dev.to/satvik1203/event-loop-and-queue-k4b</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;JavaScript is a single threaded language so how does it handle processes that require multi threads?&lt;br&gt;
 &lt;strong&gt;LETS FIND OUT&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h2&gt; Why do people say JS isn't a stand alone language? 

&lt;/h2&gt;
&lt;p&gt;The first thing that comes to our mind is JS needs an environment to run (node or browser), so we call it dependent language.&lt;/p&gt;

&lt;p&gt;So basically JS is a single threaded language so how can it perform api or any processes that require another thread? &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IT CAN'T&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;But when its run on browser or node environment, it takes help of something called event loop. &lt;/p&gt;

&lt;h3&gt; What is event loop?  &lt;/h3&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%2Fuploads%2Farticles%2Fpvttj4j1k2h79yo28d9n.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%2Fuploads%2Farticles%2Fpvttj4j1k2h79yo28d9n.png" alt="Screen Shot 2021-06-25 at 9.17.00 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before we get into the understanding of event loop. It's ideal to understand how call stack works. &lt;em&gt;(will make another blog on call stack or please refer to another blog on call stack)&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Any process that is async goes into the call stack. It is directly thrown into the event loop and new call stack is formed to call the next process. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What happens in the event loop?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The event loop is something that is at core done by c++ and c to do the non-blocking I/O operations that couldn't be done with vanilla JS. &lt;/p&gt;

&lt;p&gt;It would be easier to understand doing it with an example. &lt;/p&gt;

&lt;p&gt;Let's take the most basic function of asynchronous call which is setTimeout().&lt;/p&gt;

&lt;p&gt;Step 1: &lt;br&gt;
       setTimeout() first goes into the call stack.&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%2Fuploads%2Farticles%2Firgl80sng1ko22smsd6j.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%2Fuploads%2Farticles%2Firgl80sng1ko22smsd6j.png" alt="Screen Shot 2021-06-25 at 9.40.00 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step 2: Since setTimeout is an async call it gets put in the event loop immediately. &lt;br&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%2Fuploads%2Farticles%2Fy8b0qj587jw7wl8yo5b9.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%2Fuploads%2Farticles%2Fy8b0qj587jw7wl8yo5b9.png" alt="Screen Shot 2021-06-25 at 9.41.47 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step 3: Now the call stack looks for the next process while the setTimeout() process is happening in the event loop. This is what they call async programming. Doing multiple tasks at the same time. &lt;/p&gt;

&lt;p&gt;Once the process in the event loop is done. It goes to the event queue so that the call stack can pick it up once there are no more processes left for the call stack to read. &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%2Fuploads%2Farticles%2Fg9xtgw40sfi32osiotpa.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%2Fuploads%2Farticles%2Fg9xtgw40sfi32osiotpa.png" alt="Screen Shot 2021-06-25 at 10.16.25 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once no more processes are left for the call stack, the event queue then pushes the call backs to the call stack so that the script can finally end. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>ES6 Array Helper methods</title>
      <dc:creator>Satvik</dc:creator>
      <pubDate>Fri, 11 Jun 2021 01:46:34 +0000</pubDate>
      <link>https://dev.to/satvik1203/es6-array-helper-methods-1am8</link>
      <guid>https://dev.to/satvik1203/es6-array-helper-methods-1am8</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Array methods can be the most confusing thing one can think of in the language of JS. They are so useful that almost every framework is based on array methods, eg: REACT.JS. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt; Why are they so useful? &lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The main reason why they are used is the clean syntax they can provide while using it in a complex code. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We will be covering forEach, map, filter, find, findIndex, reduce.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;I'll be giving an example with arrow function, plain function and also with for loop.&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt; forEach &lt;/h3&gt;

&lt;p&gt;&lt;em&gt;This is probably the first method you can think of when you hear the word array methods.&lt;/em&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Suppose you want to print each element in a array, generally you would want to make a for loop from zero to length-1 and print right? But in case of forEach thats not how it works. Let's see some examples.&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%2Fuploads%2Farticles%2Farh8s7wnlw8lb6478cad.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%2Fuploads%2Farticles%2Farh8s7wnlw8lb6478cad.png" alt="Screen Shot 2021-06-07 at 5.33.05 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You might say this doesn't look so shabby but well imagine writing the same statement millions of times for iterating? 

It gets real boring lmaoo.
 
Let's see an example using forEach.
&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%2Fuploads%2Farticles%2F7xzrbv2avvwj9ukbn25k.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%2Fuploads%2Farticles%2F7xzrbv2avvwj9ukbn25k.png" alt="Screen Shot 2021-06-07 at 5.40.32 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thats an example using plain function, let's see how it looks after changing it into arrow function.&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%2Fuploads%2Farticles%2Frsv99yg5uj1d9qvzzk32.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%2Fuploads%2Farticles%2Frsv99yg5uj1d9qvzzk32.png" alt="Screen Shot 2021-06-07 at 5.47.31 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Surprised? How small you can make your code?&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Note: if you statement is more than one line you would want to use {}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt; map &lt;/h3&gt;

&lt;p&gt; If you have tried understanding what maps are previously you would question yourself what is the difference between a map and forEach method. But by base both map and forEach method are similar, they both iterate through every element in the array but the biggest difference is, maps return a brand new array while forEach doesn't. Lets look at some examples. &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%2Fuploads%2Farticles%2Ft2y56o9u5uxmw6w5kxqh.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%2Fuploads%2Farticles%2Ft2y56o9u5uxmw6w5kxqh.png" alt="Screen Shot 2021-06-07 at 5.58.52 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt; Why is it giving us undefined?  &lt;/h4&gt;


&lt;p&gt; What is the definition of undefined? not defined? since the variable isn't initialized to something it's giving us undefined. By this we can conclude that forEach doesn't return anything to us &lt;/p&gt;

&lt;h4&gt; Let's look at maps &lt;/h4&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%2Fuploads%2Farticles%2Fxz1xk52nv1ycjfoi0ah3.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%2Fuploads%2Farticles%2Fxz1xk52nv1ycjfoi0ah3.png" alt="Screen Shot 2021-06-07 at 6.08.23 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; The same code I changed it to maps, look what the output is, we got an array since map returns an array. &lt;/p&gt;

&lt;h3&gt;
  
  
  AMAZING FACTS
&lt;/h3&gt;

&lt;p&gt; When accessing the forEach and map method, there is actually a second parameter which is index, it not quite used but it's good to know&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%2Fuploads%2Farticles%2Fjehho0fiepkn4fsj6b8l.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%2Fuploads%2Farticles%2Fjehho0fiepkn4fsj6b8l.png" alt="Screen Shot 2021-06-08 at 11.11.53 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; The screenshot can be confusing since I had to show both arrow function and also normal function, but observe how I was able to access the index value and print it. You can name it anything not only index &lt;/p&gt;

&lt;p&gt; I think this should conclude the description about forEach and map. Let me remind you again, though forEach and map are similar, use the right one depending on the situation. forEach doesn't return a brand new array while map returns an array &lt;/p&gt;

&lt;h3&gt; Filter &lt;/h3&gt;

&lt;p&gt; Taking any framework in JS, you might see filter occurring 
as much as map or forEach. 
 &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Suppose you have an array and you only want a fresh array that has only the values that are 0 or over 0, how do we do that with the old school for loop? lets see.&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%2Fuploads%2Farticles%2Fo9j11abbbb8b2qajb7i1.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%2Fuploads%2Farticles%2Fo9j11abbbb8b2qajb7i1.png" alt="Screen Shot 2021-06-08 at 11.29.49 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Wanna be amazed by the filter method now?&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%2Fuploads%2Farticles%2Fr35bpofeszi0ydh7y7cv.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%2Fuploads%2Farticles%2Fr35bpofeszi0ydh7y7cv.png" alt="Screen Shot 2021-06-08 at 11.34.05 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Using arrow function or no is completely your choice, but I would say using arrow function would be better thanks to the clear syntax. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can also do this filter methods on objects&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Suppose you have an array of objects (books) and you only want the books that cost less than 10$, lets see how we can implement that.&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%2Fuploads%2Farticles%2Faxupdn1nlodthwgu00j8.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%2Fuploads%2Farticles%2Faxupdn1nlodthwgu00j8.png" alt="Screen Shot 2021-06-08 at 11.43.51 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Well seriously its that simple.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt; find &amp;amp; findIndex &lt;/h3&gt;

&lt;p&gt; Find and FindIndex can be very similar, also similar to how you would normally use them with primitive data types. 

Lets find out how it works with objects.

 &lt;/p&gt; 
 

&lt;p&gt; Suppose you have a array of books(objects), You want to find a book with particular id, how do we do that? You can do them iterating through the entire array and compare each object's id? Yepp thats about it, so lets see how we do that using array methods and for loop&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%2Fuploads%2Farticles%2Fbgcb1hmyltiyrdbcwsol.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%2Fuploads%2Farticles%2Fbgcb1hmyltiyrdbcwsol.png" alt="Screen Shot 2021-06-12 at 9.37.44 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; So that how we do it using for loop, we iterate through the entire loop, when  we find object we return it back. Same goes for findIndex as well
 &lt;/p&gt;

&lt;p&gt; Let's see how we do it using array methods &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%2Fuploads%2Farticles%2Ffrn87oyazsouedsxbp9s.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%2Fuploads%2Farticles%2Ffrn87oyazsouedsxbp9s.png" alt="Screen Shot 2021-06-12 at 9.41.29 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Not you can't do something like &lt;strong&gt;Array.find(object)&lt;/strong&gt;&lt;br&gt;
since objects are compared with hash code and not how the structure and values look like.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With this I think we can wrap up the topic:) . &lt;br&gt;
I hope you got a better understanding about this topic.&lt;/p&gt;

&lt;p&gt;Let me know what other article you want me to write in the comments below. Peace.&lt;/p&gt;

</description>
      <category>es6</category>
      <category>javascript</category>
      <category>array</category>
    </item>
  </channel>
</rss>
