<?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: Kelsy Mnk</title>
    <description>The latest articles on DEV Community by Kelsy Mnk (@klc).</description>
    <link>https://dev.to/klc</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%2F938560%2Ff4a1074c-3f90-4f1c-8a35-261df54232d2.jpeg</url>
      <title>DEV Community: Kelsy Mnk</title>
      <link>https://dev.to/klc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/klc"/>
    <language>en</language>
    <item>
      <title>Vacation Coding Amnesia</title>
      <dc:creator>Kelsy Mnk</dc:creator>
      <pubDate>Sun, 06 Aug 2023 23:01:06 +0000</pubDate>
      <link>https://dev.to/klc/vacation-coding-amnesia-d11</link>
      <guid>https://dev.to/klc/vacation-coding-amnesia-d11</guid>
      <description>&lt;p&gt;&lt;em&gt;As a programmer, taking a small vacation can prove useful for recharging your mind and body. However, stepping away from your keyboard for too long, can sometimes lead to experiencing a sense of disorientation and forgetfulness when you sit back at your desk and face lines of code that once felt familiar.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I made a mistake of taking a four-month vacation without planning a structured coding plan, and I returned to my desk feeling completely out of touch with the programming languages I had mastered. It was frustrating to see my skills had rusted, and it took me a while to regain my former programming prowess.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;To prevent you from making the same mistake, I’d like to share some additional tips to help you avoid vacation coding amnesia.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here is what I should've done 👇&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  1. Set Clear Objectives 🔍
&lt;/h3&gt;

&lt;p&gt;Define your coding goals for the vacation period. Are you aiming to explore a new programming language, deepen your understanding of a specific framework, or work on a personal project? Clearly outline your objectives to give your coding plan a sense of direction.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Prioritize Learning Materials 📚
&lt;/h3&gt;

&lt;p&gt;Identify learning resources that align with your objectives. This could include online tutorials, coding books, video courses, or coding challenge websites. Gather the necessary materials and save them offline if you'll have limited internet access during your vacation.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Allocate Time Slots ⏳
&lt;/h3&gt;

&lt;p&gt;Dedicate specific time slots for coding practice each day. Whether it's a short morning session or an evening deep dive, having a set schedule ensures that coding remains a consistent part of your daily routine.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Choose Realistic Tasks ☑
&lt;/h3&gt;

&lt;p&gt;Select coding tasks that match your skill level and the available time. Aim for a balance between challenging projects that push your boundaries and smaller tasks that allow for quick wins and a sense of accomplishment.&lt;br&gt;
(Platforms like LeetCode, HackerRank, and CodeWars offer a variety of challenges). You can also engage in personal projects that intrigue you to make coding an enjoyable activity during your vacation.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Adjust Your Plan 🗓️
&lt;/h3&gt;

&lt;p&gt;Be flexible and ready to adapt your plan based on the flow of your vacation. If you find that certain tasks or goals are unrealistic, don't hesitate to modify them to ensure a balanced and enjoyable experience.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Document Your Journey 📝
&lt;/h3&gt;

&lt;p&gt;Keep a coding journal or digital notes to document your coding experiences during the vacation. This can serve as a valuable reference when you return and need to refresh your memory.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Celebrate Milestones 🎊
&lt;/h3&gt;

&lt;p&gt;Acknowledge and celebrate your achievements, whether it's completing a challenging coding problem or successfully implementing a new feature in your personal project.&lt;/p&gt;




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

&lt;p&gt;A structured coding plan is the key to staying connected with programming even while you're away on vacation. By setting clear objectives, allocating time, and choosing appropriate tasks, you can ensure that your coding skills remain sharp and ready for action when you return to your desk. Remember, a well-crafted coding plan not only safeguards against vacation coding amnesia but also transforms your leisure time into an opportunity for growth and skill enhancement.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hope you enjoyed this article ❤️
&lt;/h2&gt;

&lt;p&gt;Let me know if you enjoyed it by leaving a &lt;strong&gt;like&lt;/strong&gt; or a &lt;strong&gt;comment&lt;/strong&gt; :) or if there is a topic that you'd like me to write about.&lt;br&gt;&lt;br&gt;
Feel free to bookmark this if you'd like to come back at it in the future. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for your time&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>codenewbie</category>
      <category>learning</category>
    </item>
    <item>
      <title>Getting started with JS loops</title>
      <dc:creator>Kelsy Mnk</dc:creator>
      <pubDate>Mon, 26 Dec 2022 13:00:00 +0000</pubDate>
      <link>https://dev.to/klc/getting-started-with-js-loops-20pf</link>
      <guid>https://dev.to/klc/getting-started-with-js-loops-20pf</guid>
      <description>&lt;p&gt;&lt;em&gt;Loops are an essential tool in JavaScript that can greatly improve the efficiency and simplicity of your code, and are a key construct to master to become proficient in the language.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In this article&lt;/strong&gt;, you are going to learn the &lt;strong&gt;three&lt;/strong&gt; most important types of loops. You'll learn how they work and how you can apply them in your code.&lt;/p&gt;

&lt;p&gt;Let's dive right into it, shall we? :) &lt;/p&gt;




&lt;p&gt;Before we get started, I'd like to quickly talk about &lt;strong&gt;Control Flow Statement&lt;/strong&gt; which is an important feature in the programming world. It will also help you understand loops more effectively. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What's a control flow statement ?&lt;/strong&gt;🤔&lt;br&gt;
As the name suggests, it allows you to control the flow of execution of your code. This will allow you to perform repetitive tasks such as &lt;code&gt;loops&lt;/code&gt; or write complex programs that can adapt to different situations.  &lt;/p&gt;


&lt;h2&gt;
  
  
  'For' loops 🔁
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;for&lt;/code&gt; loop is a control flow statement in JavaScript that allows you to repeat a block of code a certain number of times. It is often used to iterate over the elements of an array or to perform a certain task repeatedly and is best suited for situations where you know exactly how many times you want to execute a block of code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here is the basic syntax for a&lt;/strong&gt; &lt;code&gt;for&lt;/code&gt; &lt;strong&gt;loop in JS&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialization&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// code to be executed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`You have &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;&lt;span class="s2"&gt; seconds left!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Output: You have 4 seconds left!&lt;/span&gt;
&lt;span class="c1"&gt;//         You have 3 seconds left!&lt;/span&gt;
&lt;span class="c1"&gt;//         You have 2 seconds left!&lt;/span&gt;
&lt;span class="c1"&gt;//         You have 1 seconds left!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;As you can see in the example shown above, we are displaying how many seconds we have left.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The code is simply saying:&lt;/strong&gt;&lt;br&gt;
'As long as the &lt;code&gt;i&lt;/code&gt; variable is less than the number of elements that are inside of the &lt;code&gt;timer&lt;/code&gt; variable, keep displaying what's inside the &lt;code&gt;timer&lt;/code&gt;'.&lt;br&gt;
&lt;strong&gt;In short&lt;/strong&gt;, the &lt;code&gt;i&lt;/code&gt; var represents the number of times the code has to iterate through the &lt;code&gt;timer&lt;/code&gt; array.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Okay but..what if I don't know how many times I want to execute a block of code? Well, this is where the&lt;/em&gt; &lt;code&gt;while&lt;/code&gt; &lt;em&gt;loop comes in clutch! :)&lt;/em&gt; &lt;/p&gt;


&lt;h2&gt;
  
  
  'While' loops 🔁
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;while&lt;/code&gt; loop is also a control flow statement that will let you repeat a block of code but an unknown number of times.&lt;br&gt;
It has an indeterminate number of iterations and is best suited for situations where you don't know how many times you want to execute a block of code. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here is the syntax for a&lt;/strong&gt; &lt;code&gt;while&lt;/code&gt; &lt;strong&gt;loop&lt;/strong&gt; 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// code to be executed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;condition&lt;/code&gt; is a boolean expression that is evaluated before each iteration of the loop. &lt;br&gt;
If the condition is &lt;code&gt;true&lt;/code&gt;, the loop will continue to execute. &lt;br&gt;
Else, if the condition is &lt;code&gt;false&lt;/code&gt;, the loop will terminate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let me give you an example :)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;stop&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Enter a word (type 'stop' to exit):&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;while&lt;/code&gt; loop will continue to ask the user for input until they enter the word "stop". The &lt;code&gt;input&lt;/code&gt; variable is initialized to an empty string before the loop starts, and the loop condition is checked before each iteration of the loop. &lt;br&gt;
If the value of &lt;code&gt;input&lt;/code&gt; is equal to "stop", the loop will terminate, else the loop will continue  to execute.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Great! We've learned that the &lt;code&gt;while&lt;/code&gt; loop checks for the condition first before running. &lt;strong&gt;But one more thing..&lt;/strong&gt;What if I'd like my code to run at least once, before checking for the condition? That's where the &lt;code&gt;do..while&lt;/code&gt; loop comes in.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;But, why would I need to run the code before checking the condition; you may ask. Well, let me give you a real-world scenario. &lt;/p&gt;


&lt;h3&gt;
  
  
  Real-world scenario
&lt;/h3&gt;

&lt;p&gt;Suppose, you want to get a free trial of a music app first, then pay a month later. &lt;br&gt;
By using the &lt;code&gt;do..while&lt;/code&gt; loop, it would simply go like that: "Run the music app first, then check for the payment."&lt;br&gt;
But if we use the &lt;code&gt;while&lt;/code&gt; loop, the code will need to first check for the condition (in this case for the payment) meaning you can't get a free trial since you need to pay first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You got it? :)&lt;/strong&gt; If not, feel free to check this simplified &lt;a href="https://www.youtube.com/watch?v=v-K-4KuA8mQ" rel="noopener noreferrer"&gt;youtube video&lt;/a&gt; 👈 &lt;br&gt;
Alright, now let's transform this example into code for a better understanding. &lt;/p&gt;
&lt;h2&gt;
  
  
  'Do..While' loops 🔁
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;do...while&lt;/code&gt; loop is similar to a while loop, except that the loop body is always executed at least once, even if the condition is &lt;code&gt;false&lt;/code&gt;. It is useful for situations where you want to ensure that the loop body is executed at least once before the condition is checked.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt; 💻&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;musicAppPayment&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Here is your free trial&amp;lt;3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;musicAppPayment&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//Ouput: Here is your free trial&amp;lt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the code will run the 'free trial' first and then only after it has been run, will it check for the condition. &lt;br&gt;
If the payment is less than 0, then terminate the program. &lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Loops are an essential tool in JavaScript that allows you to repeat a block of code multiple times, which can be very useful in a variety of situations. They can be used to iterate over the elements of an array, perform a task a certain number of times, repeat a task until a certain condition is met, or simplify repetitive code.&lt;/p&gt;




&lt;h2&gt;
  
  
  Hope you enjoyed this article ❤️
&lt;/h2&gt;

&lt;p&gt;That's it! This was an introduction to JavaScript loops. Feel free to bookmark this if you'd like to come back at it in the future.&lt;/p&gt;

&lt;p&gt;A new article on other types of loops is &lt;strong&gt;coming soon&lt;/strong&gt;. If you are interested, you can follow me to get a notification when it's out :)&lt;/p&gt;

&lt;p&gt;Let me know if you enjoyed it by leaving a &lt;strong&gt;like&lt;/strong&gt; or a &lt;strong&gt;comment&lt;/strong&gt; :) or if there is a topic that you'd like me to write about.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for your time,&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding and Merry Christmas&lt;/strong&gt;✨🎄🎅&lt;/p&gt;

</description>
      <category>cloud</category>
      <category>networking</category>
      <category>beginners</category>
      <category>mentorship</category>
    </item>
    <item>
      <title>Understanding JavaScript Promises👩‍💻👨‍💻</title>
      <dc:creator>Kelsy Mnk</dc:creator>
      <pubDate>Mon, 19 Dec 2022 13:00:00 +0000</pubDate>
      <link>https://dev.to/klc/understanding-javascript-promises-1hp1</link>
      <guid>https://dev.to/klc/understanding-javascript-promises-1hp1</guid>
      <description>&lt;p&gt;&lt;em&gt;Promises are one of the most important features of JavaScript, they allow us to write cleaner code by reducing callback functions and give us the ability to perform asynchronous operations in a more linear fashion.  Today, you are going to learn how promises work and how you can apply them in your code.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;Let’s start, shall we? :) &lt;/p&gt;




&lt;h2&gt;
  
  
  What exactly is a  promise ?🧐
&lt;/h2&gt;

&lt;p&gt;A promise is an object that represents an asynchronous operation that hasn’t been completed yet, but is expected to provide a value sometime in the future. The promise object supports two properties: &lt;code&gt;State&lt;/code&gt; and &lt;code&gt;Result&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  State &amp;amp; Result
&lt;/h3&gt;

&lt;p&gt;A promise object has three possible states: &lt;code&gt;Pending&lt;/code&gt;, &lt;code&gt;Fulfilled&lt;/code&gt; and &lt;code&gt;Rejected&lt;/code&gt;. When an asynchronous operation starts, a promise is created and then goes in a &lt;code&gt;pending&lt;/code&gt; (working) state.  While in a pending state, the result has no value; therefore is “&lt;strong&gt;undefined&lt;/strong&gt;”.&lt;br&gt;
 If the async operation is completed successfully, the promise switches to a &lt;code&gt;fulfilled&lt;/code&gt; state with a value as result. If the operation fails, the state switches to &lt;code&gt;rejected&lt;/code&gt; with a reason. &lt;/p&gt;
&lt;h4&gt;
  
  
  Good to know 📌
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;A &lt;strong&gt;fulfilled&lt;/strong&gt; value is passed on what we call an&lt;/em&gt; &lt;code&gt;onfulfilled&lt;/code&gt; &lt;em&gt;callback and a &lt;strong&gt;rejected&lt;/strong&gt; reason is passed on an&lt;/em&gt; &lt;code&gt;onrejected&lt;/code&gt; &lt;em&gt;callback.&lt;/em&gt;&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%2F05yln5uovj9x776xvqwb.gif" 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%2F05yln5uovj9x776xvqwb.gif" alt="Illustration of how promise works"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Are promises that useful? 🤔
&lt;/h2&gt;

&lt;p&gt;Yes! Promises are handy in complex logic applications, they can easily handle multiple async operations and provide better error handling than callbacks. In short, they are the perfect choice for handling multiple callbacks at once.&lt;/p&gt;
&lt;h4&gt;
  
  
  Good to know 📌
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;If you ever find yourself having an unwieldy number of nested functions, so nested that it's mentally draining you, then you are probably in what we call the &lt;strong&gt;Pyramid of Doom&lt;/strong&gt; also known as callback hell. At this point, you should probably think of using promises instead&lt;/em&gt; ;) &lt;/p&gt;

&lt;p&gt;Here is what the pyramid of doom looks like 👇&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%2Fapuk03xq7hcploglf8nz.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%2Fapuk03xq7hcploglf8nz.png" alt="Image of the pyramid of doom"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alriight enough with technical terms, let's take a real-world example to understand promises more precisely and then jump to the coding part right after :)&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Real-world example&lt;/strong&gt; 🐤&lt;br&gt;
Suppose, you place an online order for a rubber duck. The order will be created and will start processing. (In this case, a promise is created and is on &lt;code&gt;pending&lt;/code&gt;)  &lt;/p&gt;

&lt;p&gt;If the product is available, your order will get confirmed. (That's when the promise is resolved, in other words; &lt;code&gt;fulfilled&lt;/code&gt;) &lt;/p&gt;

&lt;p&gt;If the product is out of stock, your order gets cancelled. (In this case, as you might've guessed, the promise is unfulfilled and as a result, it's &lt;code&gt;rejected&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;Are you still following? Great, now let's code this for a better perception ;) &lt;/p&gt;


&lt;h3&gt;
  
  
  Coding session 💻
&lt;/h3&gt;

&lt;p&gt;Before we dive in, let's just take a look at how you can create a promise. There are a few ways in creating one but the most common way is using the &lt;code&gt;promise&lt;/code&gt; constructor. Also known as the &lt;strong&gt;executor function&lt;/strong&gt;, the &lt;code&gt;promise&lt;/code&gt; will take a function as an argument. &lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// Here is where the asynchronous operation starts.&lt;/span&gt;
      &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we did here was, create a promise that resolves with a value. In short, it is &lt;code&gt;fulfilled&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Can you guess on how we can create a promise that rejects with a reason? Common give it a shot ;)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here it is&lt;/strong&gt; 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Start of the async operation &lt;/span&gt;
    &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;reason&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Good to know 📌
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;You can also create a promise that is already resolved or rejected using the&lt;/em&gt; &lt;code&gt;Promise.resolve()&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; &lt;code&gt;Promise.reject()&lt;/code&gt; &lt;em&gt;methods&lt;/em&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;reason&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, now that you have an idea of how to create a promise, let's jump back to our real-world example :) &lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Back to our real-world example but with codes&lt;/strong&gt; 🐤&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//We are going to place an online order for our Rubber duck&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;placeOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rubber Duck&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Confirmed order&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sorry your order has been cancelled&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your order is accepted, it will display the status of the order. If it's rejected, then that's probably the end of your code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Let's assume your order has been accepted and display the status :) */&lt;/span&gt; 

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;orderStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Order is being processed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Your &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; has been delivered :)`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="p"&gt;})&lt;/span&gt;
   &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Promise Chaining ⛓
&lt;/h3&gt;

&lt;p&gt;One of the advantages of promises is that they can be tied together. This will let you create a chain of async operations where the output of one operation is passed as the input to the next operation.&lt;br&gt;
You can chain promises using the &lt;code&gt;Promise.then()&lt;/code&gt; method.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Scenario with Chain promises&lt;/span&gt;

&lt;span class="nf"&gt;placeOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rubber Duck&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orderFromCustomer&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Request received&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;orderProcessed&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;orderStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orderFromCustomer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;orderProcessed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orderProcessed&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orderProcessed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Here is the whole code&lt;/strong&gt; 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//We are going to place an online order for our Rubber duck&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;placeOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rubber Duck&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Confirmed order&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sorry your order has been cancelled&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="cm"&gt;/* Let's assume your order has been accepted and display the status :) */&lt;/span&gt; 

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;orderStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Order is being processed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Your &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; has been delivered :)`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="p"&gt;})&lt;/span&gt;
   &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Scenario with Chain promises&lt;/span&gt;

&lt;span class="nf"&gt;placeOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rubber Duck&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orderFromCustomer&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Request received&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;orderProcessed&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;orderStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orderFromCustomer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;orderProcessed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orderProcessed&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orderProcessed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="c1"&gt;// Output: Request received&lt;/span&gt;
&lt;span class="c1"&gt;//         Order is being processed&lt;/span&gt;
&lt;span class="c1"&gt;//         Your Confirmed order has been delivered :)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Error Handling
&lt;/h2&gt;

&lt;p&gt;Error handling helps in managing errors and helps the program to resume without being interrupted. In JavaScript, we use &lt;code&gt;Promise.catch()&lt;/code&gt; to catch errors. It only handles errors that may happen between &lt;code&gt;.then()&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Perhaps an example might help :)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;It's workiiing!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
       &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;There is an error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;promise&lt;/span&gt; 
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result2&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;   &lt;span class="c1"&gt;// line 12&lt;/span&gt;
         &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; 
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;erroooor&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;//last line&lt;/span&gt;

&lt;span class="c1"&gt;// Output: It's workiiing! &lt;/span&gt;
&lt;span class="c1"&gt;//         erroooor&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see here, we've thrown an error on line 12 and normally, the program should've been interrupted. But the &lt;code&gt;.catch()&lt;/code&gt; method on the last line was able to capture the error and log it to the console to make sure the program keeps running smoothly. (You can try to remove the last line and check if the program works)&lt;/p&gt;




&lt;h2&gt;
  
  
  Async / Await
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Async/await&lt;/strong&gt; is a syntactic feature that allows us to write &lt;strong&gt;asynchronous&lt;/strong&gt; code in a more similar way to a synchronous function.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Async&lt;/code&gt; keyword is what lets the JavaScript engine know that we are declaring an async function.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;await&lt;/code&gt; keyword makes the function pause the execution and wait for a resolved promise before moving on to the next line of code.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// The code on this line will only be executed after the promise is resolved. &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great, now one more thing☝️.&lt;/p&gt;

&lt;p&gt;Async functions use &lt;code&gt;try/catch&lt;/code&gt; statements for error handling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How does it work?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Well, the code which we want to execute goes inside a try block and when performing that execution, if there is an error, it will go through the catch() block.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's take a look :)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
       &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
&lt;span class="c1"&gt;// The code on this line is executed if the promise is rejected. &lt;/span&gt;
&lt;span class="p"&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;&lt;strong&gt;Promises&lt;/strong&gt; are amazing! They are a core feature of JS that allows us to write async code more synchronously. Before promises, our code was full of confusing callback tricks and async.&lt;/p&gt;

&lt;p&gt;They are ideal for managing multiple callbacks at once, error handling, executing async functions and many more.&lt;/p&gt;




&lt;h3&gt;
  
  
  Hope you enjoyed this article ❤️
&lt;/h3&gt;

&lt;p&gt;Greaat, that was quite a lot of codes, congrats to you if you made it so far! Feel free to bookmark this if you'd like to come back at it in the future.&lt;/p&gt;

&lt;p&gt;Let me know if you enjoyed it by leaving a &lt;strong&gt;like&lt;/strong&gt; or a &lt;strong&gt;comment&lt;/strong&gt; :) or if there is a topic that you'd like me to write about in the future.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for your time,&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding and Merry Christmas✨🎄🎅&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Thank you all✨</title>
      <dc:creator>Kelsy Mnk</dc:creator>
      <pubDate>Sat, 17 Dec 2022 20:11:51 +0000</pubDate>
      <link>https://dev.to/klc/thank-you-all-1i7g</link>
      <guid>https://dev.to/klc/thank-you-all-1i7g</guid>
      <description>&lt;p&gt;&lt;strong&gt;Wow a 100 followers already :o&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;I was hesitant to start writing blogs for about two months but after giving it a lot of thought, I realized that it was a win-win situation. Not only I’m gaining more knowledge by writing and documenting but I’m also sharing it with the community. &lt;/p&gt;

&lt;p&gt;I started 10 days ago and it seems to be going pretty well so far:) &lt;br&gt;
(I publish every Monday)&lt;/p&gt;

&lt;p&gt;For those who don’t know me, I’m Kelsy, just your average 21y/o guy who’s also a self-taught front-end dev. &lt;/p&gt;

&lt;p&gt;Anyway, I just wanted to give a huge thanks to all of my new followers:) You are all awesome!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for your time and Merry Christmas&lt;/strong&gt; ✨🎄🎅&lt;/p&gt;

</description>
      <category>blog</category>
      <category>gratitude</category>
      <category>proud</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Understanding Map, Filter &amp; Reduce functions.</title>
      <dc:creator>Kelsy Mnk</dc:creator>
      <pubDate>Mon, 12 Dec 2022 13:00:00 +0000</pubDate>
      <link>https://dev.to/klc/understanding-map-filter-reduce-functions-22b</link>
      <guid>https://dev.to/klc/understanding-map-filter-reduce-functions-22b</guid>
      <description>&lt;p&gt;&lt;em&gt;If you still don't feel comfortable with these functions or simply need a refresher, well you are at the right place :)&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;Before we jump in, I'd like to talk about what these functions have in common. Can you take a guess? They are both referred to as &lt;strong&gt;Higher-Order Functions&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's a higher-order function?
&lt;/h3&gt;

&lt;p&gt;A higher-order function (HOF) is a function that can take another function as an argument or that can return a function as its result. All other functions are simply &lt;strong&gt;First-Order Functions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why use a HOF? 🤔&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It allows us to keep our code DRY and more flexible. &lt;em&gt;(Let me know if you are interested to know &lt;strong&gt;HOF&lt;/strong&gt; more in-depth :-)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fhn85iby9m2t2o0frf89l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fhn85iby9m2t2o0frf89l.png" alt="Higher-order functions" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alright, now that we have some background let's start, shall we?&lt;/p&gt;




&lt;h2&gt;
  
  
  .Map()
&lt;/h2&gt;

&lt;p&gt;The map() method performs a callback function on each element in an array. It will return a new array from the returned values so that the initial array can stay unmodified.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's take a look :)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newTest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newTest&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// output: [1,9,16,25,81,4]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what we did here was, multiply each number by itself inside the &lt;code&gt;test array&lt;/code&gt; then return the value inside a new array.&lt;/p&gt;

&lt;p&gt;Alright, now I'm sure we can write this function on a single line to make it a little bit cleaner :)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cleaner code&lt;/strong&gt; 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newTest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newTest&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output: [1,9,16,25,81,4]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  .Filter()
&lt;/h2&gt;

&lt;p&gt;As the name suggests 'filter', it will allow you to filter a range of data based on the given conditions.&lt;/p&gt;

&lt;p&gt;Similarly to the map function, the .filter() will also perform a callback function on each element in an array. Then, using a true or false condition, it will return a new array filled with elements that are true to that condition.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newScore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newScore&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output: [2,6,-8]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we wanted to filter out all the odd numbers and then return only even numbers.&lt;br&gt;
Is 1 &lt;code&gt;true&lt;/code&gt; to the condition? No, so don't return it.&lt;br&gt;
Is 2 &lt;code&gt;true&lt;/code&gt; to the condition? Yes, then return it.&lt;br&gt;
And so on...&lt;/p&gt;

&lt;p&gt;This function can also be written on a single line. Can you give it a shot? :)&lt;/p&gt;


&lt;h2&gt;
  
  
  .Reduce()
&lt;/h2&gt;

&lt;p&gt;The .reduce() method iterates through an array and returns a single value.&lt;/p&gt;

&lt;p&gt;It takes a callback function with two parameters (&lt;strong&gt;accumulator&lt;/strong&gt;, &lt;strong&gt;currentValue&lt;/strong&gt;) as arguments. The &lt;code&gt;accumulator&lt;/code&gt; is the value returned from the previous iteration and the &lt;code&gt;currentValue&lt;/code&gt; is the current element being processed in the array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's take an example.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;totalNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currVal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;currVal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//initializing the accumulator to 0&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;totalNumbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output: [20]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So here, we summed all elements of the array and then returned a single value (where the output is 20). &lt;strong&gt;In other words&lt;/strong&gt;, reducing the array to a single value.&lt;/p&gt;

&lt;p&gt;Great! Now that we've learned these functions, let's take a quick look at the summary :-)&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;h3&gt;
  
  
  .Map()
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Performs a callback function on each element in an array.&lt;/li&gt;
&lt;li&gt;Creates a new array from the callback function.&lt;/li&gt;
&lt;li&gt;Does not change the original array.&lt;/li&gt;
&lt;li&gt;Does not execute the function for empty elements.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  .Filter()
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Performs a callback function on each element in an array.&lt;/li&gt;
&lt;li&gt;Creates a new array filled with elements that passed the true/false condition based on the given instructions.&lt;/li&gt;
&lt;li&gt;Does not change the original array.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  .Reduce()
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Returns a single value (using the accumulator).&lt;/li&gt;
&lt;li&gt;Executes a reducer function for an array element.&lt;/li&gt;
&lt;li&gt;Does not change the original array.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;code&gt;Before we wrap this up, feel free to check in the comment for some additional information written by some awesome users from this community :) You can add some details too if you think I might've missed something ;)&lt;/code&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Hope you enjoyed this article ❤️
&lt;/h2&gt;

&lt;p&gt;That's it! This was a quick refresher for the &lt;code&gt;map, filter &amp;amp; reduce functions.&lt;/code&gt; Feel free to bookmark this if you'd like to come back at it in the future.&lt;/p&gt;

&lt;p&gt;Let me know if you enjoyed it by leaving a &lt;strong&gt;like&lt;/strong&gt; or a &lt;strong&gt;comment&lt;/strong&gt; :) or if there is a topic that you'd like me to write about.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for your time,&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding :)&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>How does JavaScript work?🧐</title>
      <dc:creator>Kelsy Mnk</dc:creator>
      <pubDate>Mon, 05 Dec 2022 18:18:39 +0000</pubDate>
      <link>https://dev.to/klc/how-does-javascript-work-3fpl</link>
      <guid>https://dev.to/klc/how-does-javascript-work-3fpl</guid>
      <description>&lt;p&gt;There is nothing more exciting than learning to code for the first time. We get so passionate that we want to start building projects right away. However, we tend to miss the one thing that can bring more meaning in the way we write codes which is... &lt;strong&gt;the fundamentals&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's get started, shall we? Don't forget to write notes to enhance your understanding:)&lt;/p&gt;




&lt;h2&gt;
  
  
  First of all, what is JavaScript?
&lt;/h2&gt;

&lt;p&gt;JS is a &lt;strong&gt;single-threaded&lt;/strong&gt; language that can be &lt;strong&gt;non-blocking&lt;/strong&gt;. &lt;em&gt;'Hmm okay..what does that mean?'&lt;/em&gt;  Well, being single-threaded means that it can only run one set of instructions at a time. Think of it as reading a food recipe, you have to follow instructions one by one from top to bottom right? It's not doing multiple things, and that's because it has only one &lt;strong&gt;Call Stack&lt;/strong&gt;. &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%2Fwko5xd47nyykovyj56pi.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%2Fwko5xd47nyykovyj56pi.png" alt="Call Stack"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Call Stack &amp;amp; Memory Heap
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;A Call Stack&lt;/strong&gt; is what let JavaScript Engine keep track of function calls but, most importantly it is what allows us to run code one at a time. In short, when you call a function, the interpreter will add it to the call stack first then start carrying out the function and once it has been run, it will be removed from the &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Call_stack" rel="noopener noreferrer"&gt;call stack&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To call functions, they have to be stored somewhere..and that somewhere is in the &lt;strong&gt;Memory Heap&lt;/strong&gt;. It's a form of resource management applied to computer memory. It is used to provide ways to dynamically allocate portions of memory to programs at their request, and free it for reuse when no longer needed. &lt;/p&gt;&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%2Fg1gvhzqsztsmaowxnhoy.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%2Fg1gvhzqsztsmaowxnhoy.png" alt="Stack and Memory Heap"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Memory Leak
&lt;/h2&gt;

&lt;p&gt;That's what happens when a computer program incorrectly manages memory allocations and this is when the memory is not released when it's no longer needed. &lt;/p&gt;

&lt;p&gt;The good news, this issue was fixed when JS Engine introduced the &lt;strong&gt;Garbage Collector&lt;/strong&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Garbage Collector (GC)
&lt;/h2&gt;

&lt;p&gt;Garbage Collection is a form of automatic memory management. It will attempt to reclaim memory which was issued by the program but is no longer needed. &lt;/p&gt;

&lt;h2&gt;
  
  
  Stack Overflow
&lt;/h2&gt;

&lt;p&gt;You might have ever heard of it? Well, since we know that there is one call stack that means we are also quite limited. A stack overflow happens when the call stack space is exceeded.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But how?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;The most common cause is infinite recursion, where a function calls itself so many times that the space needed to store the variables and information connected with each call is way more than we can fit on the stack.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Perhaps an example might help :)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Helloo, you are awesome!!!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here, we just created an infinite loop that will run indefinitely. If we try to run it inside the browser, the function will run until the stack space is exceeded as you can see in the picture shown below 👇.&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%2F1zn1pfg8svy17sx6sm6f.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%2F1zn1pfg8svy17sx6sm6f.png" alt="Stack Overflow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Are you still following? Great, let's continue!&lt;/p&gt;

&lt;p&gt;Now that we know what JavaScript is, we need to know &lt;strong&gt;how it works&lt;/strong&gt;. &lt;/p&gt;




&lt;h2&gt;
  
  
  The JavaScript Engine
&lt;/h2&gt;

&lt;p&gt;As you might know, computers only understand binary codes which are 0s and 1s, meaning that they can't simply execute our JavaScript files since they can't understand what's written inside. That's where the &lt;strong&gt;JS Engine&lt;/strong&gt; plays its most important role. &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%2Fqdactvzh8a5mh51l0bmd.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%2Fqdactvzh8a5mh51l0bmd.png" alt="JavaScript engine process"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Instead of sending our JS file straight to the CPU, we first of all, need a translator to make the computer understand what we want it to do. As in the picture shown above, the engine takes our file, executes the compilation to binary instructions then sends it to the CPU to finally run it. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Did you know?&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;The first JavaScript Engine was created in 1995 by &lt;a href="https://en.wikipedia.org/wiki/Brendan_Eich" rel="noopener noreferrer"&gt;Brendan Eich&lt;/a&gt;. Before that, browsers were barely interactive since they were only run in HTML &amp;amp; CSS.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Inside the JavaScript Engine
&lt;/h2&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%2F9rjhs4h675wtq1qo4k7r.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%2F9rjhs4h675wtq1qo4k7r.png" alt="Inside the Javascript engine"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we run a JS file, first it goes through the Parser which will perform a lexical analysis, which means it will break the code into tokens to identify their meaning or to know what the code is trying to do. &lt;/p&gt;

&lt;p&gt;Secondly, the tokens will get formed into an Abstract Syntax Tree (AST). An &lt;a href="https://deepsource.io/glossary/ast/" rel="noopener noreferrer"&gt;AST&lt;/a&gt; is a tree representation of the source code of a computer program.   &lt;/p&gt;

&lt;p&gt;Now, before continuing I'd like to talk about two important things in a JS Engine that quite often confuse people, and that is..an &lt;strong&gt;interpreter&lt;/strong&gt; vs a &lt;strong&gt;compiler&lt;/strong&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Interpreter Vs Compiler
&lt;/h2&gt;

&lt;p&gt;In programming, there are generally two ways of translating to machine language, let's start with the first one.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;An &lt;strong&gt;interpreter&lt;/strong&gt;, what it does is read and translates the files line by line. This can be a slow process however, the program runs right away and you will be able to catch any error on the spot. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;With a &lt;strong&gt;compiler&lt;/strong&gt;, unlike an interpreter, it doesn't start to translate immediately. What it does is, it works ahead of time to create a translation. In short, instead of having to wait for the program to run instructions one at a time, a compiler will simply read the whole file at once and then execute. Yes, it does take some preparation time since the compiler needs to first read all the instructions but then after that, it will run very quickly. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Did you know?&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;JavaScript originally came as an interpreted language but now we can use a compiler to optimize the code.&lt;/em&gt;&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%2Fj9o5t1huethqd2ihoy57.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%2Fj9o5t1huethqd2ihoy57.png" alt="Difference between an interpreter and a compiler"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you might be wondering which one to use between a compiler and an interpreter but guess what, there is the best of both worlds! Google came up with the V8 Engine using both interpreter and compiler also known as &lt;strong&gt;JIT&lt;/strong&gt; (Just In Time) compiler to make the engine runs faster. &lt;/p&gt;

&lt;p&gt;Perfect! Now that we know what JavaScript is and how it works, there is still one small issue. If JavaScript has one call stack and runs one instruction at a time, wouldn't it take an eternity to run a large program? Well, yes but this is where the &lt;strong&gt;JavaScript Runtime&lt;/strong&gt; comes in!&lt;/p&gt;




&lt;h2&gt;
  
  
  What's a JavaScript Runtime?
&lt;/h2&gt;

&lt;p&gt;Runtime is an environment in which a programming language executes. This will make the browser keep working in the background while the synchronous javascript code is running, this uses the &lt;strong&gt;Web API&lt;/strong&gt; to communicate.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keep in mind 💭&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;It's good to be mindful that the runtime environment can have different forms based on the context. For example, a browser runtime environment can be very different from a &lt;code&gt;Node.JS&lt;/code&gt;. However, these differences are mostly on the implementation level, meaning that the following concepts are still relevant.&lt;/em&gt;&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%2Fkdmxo4ws83tkvi3uo1bt.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%2Fkdmxo4ws83tkvi3uo1bt.png" alt="JavaScript Runtime"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Web API
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;First of all, what's an API?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;An API is an Application Programming Interface. In short, it's a kind of interface that has a set of functions that allow programmers to build software/applications or get access to some specific features or data of an application.&lt;/p&gt;

&lt;p&gt;Now, a &lt;strong&gt;Web API&lt;/strong&gt; which is not part of the JS engine but is instead provided by the browser, will simply extend the browser functionality by doing multiple tasks such as sending HTTP requests, listening to DOM events, delaying executions using a callback, etc...  &lt;/p&gt;

&lt;p&gt;These Web APIs are called &lt;strong&gt;Asynchronous&lt;/strong&gt;. Why? Because they can run instructions in a different order compared to JS which can only run from top to bottom. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How does it work?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Using &lt;strong&gt;setTimeout&lt;/strong&gt;, you can delay the first instruction to be able to run the second instruction first.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's take a look :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;First&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Second&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Third&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cm"&gt;/*
OutPut: First,
        Third,
        Second
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see in the example shown above, &lt;strong&gt;console.log("Second")&lt;/strong&gt; was written second but came as last in the output. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But how?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Alright, so what happened here is, we ran the &lt;strong&gt;console.log("First")&lt;/strong&gt; and then set a delay on the &lt;strong&gt;console.log("Second")&lt;/strong&gt; for 1-second.&lt;/li&gt;
&lt;li&gt;Since &lt;strong&gt;setTimeout&lt;/strong&gt; is a function from the Web API, the call stack cannot understand it so it will send the function to the web API to handle it. 
&lt;em&gt;(By the time it sent the setTimeout function, it will proceed with the **console.log("Third")&lt;/em&gt;&lt;em&gt;)&lt;/em&gt; &lt;/li&gt;
&lt;li&gt;Finally, the Web API will read the setTimeout function then wait for the &lt;strong&gt;1-second delay&lt;/strong&gt; that was defined by us, goes through the &lt;strong&gt;Callback Queue&lt;/strong&gt; then finally back to the call stack. &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Callback Queue
&lt;/h2&gt;

&lt;p&gt;The Callback queue is a data structure that uses the &lt;strong&gt;FIFO&lt;/strong&gt; (first-in-first-out) principle. That's where all the asynchronous callback functions need to go first before being sent to the call stack, it will simply try to maintain the order in which the message or method was added to the queue.&lt;/p&gt;

&lt;h2&gt;
  
  
  Event Loop
&lt;/h2&gt;

&lt;p&gt;The event loop will keep checking consistently if the call stack is empty and if there is any message left in the event queue. Its purpose is to move the message from the callback queue to the call stack but only when the call stack is empty.&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%2Frkw8cnllufcnwd6wxa2m.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%2Frkw8cnllufcnwd6wxa2m.png" alt="Event Loop"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Hope you enjoyed this article ❤️
&lt;/h3&gt;

&lt;p&gt;Alriight, that was quite a lot of new information, congrats to you if you made it so far! Feel free to bookmark this if you'd like to come back at it in the future. &lt;/p&gt;

&lt;p&gt;Let me know if you enjoyed it by leaving a &lt;strong&gt;like&lt;/strong&gt; or a &lt;strong&gt;comment&lt;/strong&gt; :) or if there is a topic that you'd like me to write about.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for your time,&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding :)&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
