<?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: Reggie Peterson</title>
    <description>The latest articles on DEV Community by Reggie Peterson (@taorep).</description>
    <link>https://dev.to/taorep</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%2F222607%2F57986236-9c10-4bf0-befa-ea96c79e6b2a.jpeg</url>
      <title>DEV Community: Reggie Peterson</title>
      <link>https://dev.to/taorep</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/taorep"/>
    <language>en</language>
    <item>
      <title>ES6: Const &amp; Let</title>
      <dc:creator>Reggie Peterson</dc:creator>
      <pubDate>Sat, 14 Sep 2019 21:45:17 +0000</pubDate>
      <link>https://dev.to/taorep/es6-const-let-10gl</link>
      <guid>https://dev.to/taorep/es6-const-let-10gl</guid>
      <description>&lt;p&gt;&lt;a href="https://jayess.lifesandwich.co/blog/2019/08/javascript-es6-const-let/"&gt;~View the original post here at jayess.lifesandwich.co~&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Who uses var? Not you hopefully. ES6 (ES2015) killed it for the better. It's replaced by two new variable assignment keywords; Const &amp;amp; Let — they work a little bit differently than var.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Const:&lt;/strong&gt; Your go-to 99% of the time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let:&lt;/strong&gt; Use it only when needed.&lt;/p&gt;




&lt;p&gt;So what's the difference? Why are they so much better than var?&lt;/p&gt;

&lt;p&gt;The answer is that Const &amp;amp; Let are block scoped vs var which is function scoped.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Block Scoped:&lt;/strong&gt; only exist between a pair of curly braces { //code }&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function scoped:&lt;/strong&gt; only exists within the current function.&lt;/p&gt;




&lt;p&gt;The primary difference here is that naming a variable using const or let in an if statement, or a loop will basically hold that variable within it and will not allow it outside.&lt;/p&gt;

&lt;p&gt;so this:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;if(true) {&lt;br&gt;
var x = 2;&lt;br&gt;
}&lt;br&gt;
console.log(x)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;logs 2,&lt;br&gt;
but this:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;if(1 === 1) {&lt;br&gt;
const y = 2;&lt;br&gt;
}&lt;br&gt;
console.log(y)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;throws an error.&lt;/p&gt;

&lt;p&gt;This is because x, created with var, doesn't care about the if statement { }, but const (and let) does.&lt;/p&gt;

&lt;p&gt;This is a good thing, because it means if you accidentally name two variables the same, you won't get unexpected behavior, since a variable inside an if/ function/ loop will be completely different than one (named the exact same thing) outside of those { }. It helps avoid naming collisions.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Const vs Let&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;let is the same as var, aside from the block scope thing we just talked about.&lt;/p&gt;

&lt;p&gt;const, however, is short for constant, and means that you can't reassign it.&lt;/p&gt;

&lt;p&gt;so:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;let a = 1&lt;br&gt;
a = 2&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;is cool.&lt;br&gt;
but this:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;const b = 1&lt;br&gt;
b = 2&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;is a no go&lt;/p&gt;

&lt;p&gt;You also have to initialize const with a value:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;let c;&lt;br&gt;
c = 3&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;is fine&lt;br&gt;
but:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;const d;&lt;br&gt;
d = 4;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;is not. (it'll error on that first line)&lt;/p&gt;

&lt;p&gt;This is good because it means you can't accidentally reassign a value (although you can mutate arrays and objects via push, pop, and such).&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;//RECAP&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Var isn't great because it's function scoped and reassignable. Which is theoretically fine. but it opens the door for bad juju (namespace collision, unexpected values)&lt;/p&gt;

&lt;p&gt;const and let are block scoped, and const is also not reassignable, which is why const should be used all of the time, except when you for sure need to reassign (like when incrementing in a loop).&lt;/p&gt;

&lt;p&gt;Ditching var is a simple step to future proof your code and reduce bugs, and your users will thank you for it. &lt;/p&gt;




&lt;p&gt;&lt;a href="https://jayess.lifesandwich.co/blog/2019/08/javascript-es6-const-let/"&gt;~View the original post here at jayess.lifesandwich.co~&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>What Is the Event Loop? </title>
      <dc:creator>Reggie Peterson</dc:creator>
      <pubDate>Sat, 14 Sep 2019 20:02:50 +0000</pubDate>
      <link>https://dev.to/taorep/what-is-the-event-loop-9k6</link>
      <guid>https://dev.to/taorep/what-is-the-event-loop-9k6</guid>
      <description>&lt;p&gt;&lt;em&gt;check out the original post @&lt;/em&gt; &lt;a href="https://jayess.lifesandwich.co/blog/2019/08/javascript-es6-const-let/"&gt;&lt;em&gt;jayess.lifesandwich.co&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's mystical. Maybe you've never heard of it. If you're a developer who deals with browsers / http requests at all, chances are you've utilized it. It's what allows setTimeout to work, allow promises to be made, and how async/await is handled.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;But First; JavaScript Engines&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;1) Browsers work on the web.&lt;/p&gt;

&lt;p&gt;2) The web uses JavaScript.&lt;/p&gt;

&lt;p&gt;3) Therefore, Browsers have JavaScript engines (to run the javascript)&lt;/p&gt;

&lt;p&gt;Chrome's JavaScript engine is called V8. The same V8 Node.js is built on.&lt;/p&gt;

&lt;p&gt;V8 is a single threaded execution engine. Single threaded meaning one call stack. Execution engine means, well, it executes the code.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;V8&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;V8 has two main components: a memory heap &amp;amp; a call stack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Memory Heap&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This isn’t relevant enough to get into details, but the memory heap handles how &amp;amp; where the JavaScript gets stored. if you create a function called helloWorld and then want to use it later, the memory heap is what stores it and remembers where it is so when you want to use it, it works. That’s basically it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Call Stack&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A call stack is a data structure that allows functions to be added or removed from the top.&lt;/p&gt;

&lt;p&gt;Since JavaScript &amp;amp; V8 are single threaded, there is just one call stack in V8. Whenever we perform an action/function in JavaScript, that thing is pushed onto the call stack, where it will run to completion and then be popped (or removed) off the top.&lt;/p&gt;

&lt;p&gt;If the function called contains another function call, that will be pushed onto the call stack on top of the current call. This will continue until all code/calls have been executed and they will be removed one by one from the top.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Back to the Event Loop&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Since JavaScript can only do one thing at a time, anything that needs to or should be done asynchronously must rely on functionality provided by the browser itself. Most browsers have browser APIs to handle the actual request, and then use a task queue and event loop to determine when it should be done.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;A simple example&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;1) The b﻿rowser receives the Javascript source code you wrote &amp;amp; interprets it&lt;/p&gt;

&lt;p&gt;2) While the browser is executing the code, if it encounters a browser API such as a http request, it will push it onto the stack, begin a new, separate process of retrieving the response, and pop the call off of the stack&lt;/p&gt;

&lt;p&gt;3) The browser will continue executing the code as before&lt;/p&gt;

&lt;p&gt;4) Once the separate process is complete (ex. http request) it is sent to a task queue.&lt;/p&gt;

&lt;p&gt;5) The response will wait in the queue until the event loop determines the call stack in empty* and will push it to the call stack, where the callback / promise / whatever was waiting is executed&lt;/p&gt;

&lt;p&gt;6) After execution, it is popped off the stack and execution continues as normal or repeats these steps as needed.&lt;/p&gt;

&lt;p&gt;Now here is the part you came for&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;So What is the Event Loop?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;event loop&lt;/strong&gt; is a programming construct that checks the call stack and, when empty* will push the next waiting callback or promise in the queue to the call stack.&lt;/p&gt;

&lt;p&gt;That's it.&lt;/p&gt;

&lt;p&gt;It's a little program that directs the flow of traffic from asynchronous requests (promises, setTimout, http requests) back into the main flow of the one and only JS call stack.&lt;/p&gt;

&lt;h2&gt;
  
  
  ** Asterisk **
&lt;/h2&gt;

&lt;p&gt;There are not one, but &lt;strong&gt;two&lt;/strong&gt; queues:&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;microtask queue&lt;/strong&gt; for promises.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;task queue&lt;/strong&gt; for everything else.&lt;/p&gt;

&lt;p&gt;You should also know that the event loop &lt;strong&gt;prioritizes microtasks above regular tasks.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This means that a normal task response waiting in the task queue will be pushed onto the call stack after the call stack is empty. As in, no more things to fire.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://latentflip.com/loupe/?code=JC5vbignYnV0dG9uJywgJ2NsaWNrJywgZnVuY3Rpb24gb25DbGljaygpIHsKICAgIHNldFRpbWVvdXQoZnVuY3Rpb24gdGltZXIoKSB7CiAgICAgICAgY29uc29sZS5sb2coJ1lvdSBjbGlja2VkIHRoZSBidXR0b24hJyk7ICAgIAogICAgfSwgMjAwMCk7Cn0pOwoKY29uc29sZS5sb2coIkhpISIpOwoKc2V0VGltZW91dChmdW5jdGlvbiB0aW1lb3V0KCkgewogICAgY29uc29sZS5sb2coIkNsaWNrIHRoZSBidXR0b24hIik7Cn0sIDUwMDApOwoKY29uc29sZS5sb2coIldlbGNvbWUgdG8gbG91cGUuIik7!!!PGJ1dHRvbj5DbGljayBtZSE8L2J1dHRvbj4%3D"&gt;Here&lt;/a&gt; is a tool with a cool visual to show you how this works (sorry, no microtask queue)&lt;/p&gt;

&lt;p&gt;However, this isn't the case for microtasks. Microtasks will fire in between code finishing and more code starting.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;If you had three console.logs and a &lt;strong&gt;setTimeout&lt;/strong&gt; set to 0ms with an function call in it, you would get normal synchronous code execution all the way from top to bottom and then after the console.logs were completed, the setTimeout function call would run.&lt;/p&gt;

&lt;p&gt;But with &lt;strong&gt;microtasks&lt;/strong&gt; such as promises, assuming your promise is at the top of the file above the console.logs, it would fire as soon as it could. At the end of the file, after the first console.log, after the second console.log, etc. It doesn't care. as soon as something is popped off the stack, and as normal execution is about to continue, &lt;strong&gt;all the microtasks in the microtask queue slide into the spot on the call stack and they won't let code continue running until ALL OF THEM are complete.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is opposed to normal tasks in the task queue running one at a time, one per event loop cycle.&lt;/p&gt;

&lt;p&gt;Got it?&lt;/p&gt;

&lt;p&gt;Cool. That was easy.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;check out the original post @&lt;/em&gt; &lt;a href="https://jayess.lifesandwich.co/blog/2019/08/javascript-es6-const-let/"&gt;&lt;em&gt;jayess.lifesandwich.co&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>async</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
