<?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>Forem: Mostafa Gaafar</title>
    <description>The latest articles on Forem by Mostafa Gaafar (@gafi).</description>
    <link>https://forem.com/gafi</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%2F201716%2Ffb5ed562-937c-4406-9e56-751801c8227a.jpg</url>
      <title>Forem: Mostafa Gaafar</title>
      <link>https://forem.com/gafi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/gafi"/>
    <language>en</language>
    <item>
      <title>Learnings That Landed Me Jobs @ Amazon &amp; Meta With No CS Degree</title>
      <dc:creator>Mostafa Gaafar</dc:creator>
      <pubDate>Tue, 31 May 2022 18:20:28 +0000</pubDate>
      <link>https://forem.com/gafi/how-i-cracked-coding-interviews-amazon-meta-with-no-cs-degree-25bj</link>
      <guid>https://forem.com/gafi/how-i-cracked-coding-interviews-amazon-meta-with-no-cs-degree-25bj</guid>
      <description>&lt;p&gt;As someone that has a non Computer Science college degree and did not have a strong background in algorithms, I always dreaded problem solving interviews and didn't think I'd be able to get a job at FAANG companies. I'm sure a lot of you are in the same position, so this article is a summary of my learnings in the past few years that enabled me to work at 2 of these companies.&lt;/p&gt;

&lt;p&gt;I will focus on problem solving questions here as this was the hardest part for me to master through trial and error. I may follow up with articles on system design and behavioral questions later.&lt;/p&gt;

&lt;p&gt;This will be a concise guide to help you pass the interview with minimal effort. I won't ask you to read lengthy books, and this article won't necessarily help you become a better engineer, but my aim is to help you do your best in algorithms interviews.&lt;/p&gt;

&lt;h3&gt;
  
  
  Disclaimers
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;This article is based entirely on my experience as candidate, not on any inside knowledge of interviewing processes. It's based on my experience interviewing at Amazon, Meta (Facebook), Google &amp;amp; Apple, but I imagine it's applicable to many large tech companies.&lt;/li&gt;
&lt;li&gt;Do your own research on the company and team before your interview. Some companies hold online sessions to help you prepare for interviews which are very useful to learn about the specifics of a certain company.&lt;/li&gt;
&lt;/ol&gt;




&lt;h1&gt;
  
  
  1 Before the interview
&lt;/h1&gt;

&lt;h3&gt;
  
  
  1.1 Get the interview, obviously
&lt;/h3&gt;

&lt;p&gt;When you're applying at big tech companies, you are more likely to get an interview round if an employee refers you for the job. I experienced applying online on my own and getting no reply at all, and then applying with the exact same CV through a referral and getting a call within days.&lt;/p&gt;

&lt;p&gt;If you know someone at the company, don't hesitate to ask for a referral. Most people will help if you ask, and they may get a bonus if you are hired. If you don't know someone personally, you can try to build more connections through online forums or meetups. You can also reach out to recruiters or managers who are hiring directly on Linkedin.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.2 Build algorithms foundation
&lt;/h3&gt;

&lt;p&gt;If you don't have a strong background in algorithms (like myself), you need to start with the basic concepts and common algorithms. Your goal here is to be familiar with topics like sorting, searching, array/string manipulation, stacks, queues, hash tables, trees, basic graph traversals.&lt;/p&gt;

&lt;p&gt;There's plenty of courses online for this. I personally refer to Princeton Algorithms Course Part 1 &amp;amp; Part 2&lt;/p&gt;

&lt;h4&gt;
  
  
  Part 1
&lt;/h4&gt;

&lt;p&gt;Coursera &lt;a href="https://www.coursera.org/learn/algorithms-part1"&gt;https://www.coursera.org/learn/algorithms-part1&lt;/a&gt;&lt;br&gt;
Youtube &lt;a href="https://www.youtube.com/watch?v=9diDWV-fOnE"&gt;https://www.youtube.com/watch?v=9diDWV-fOnE&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You need to know most of the algorithms in part 1, but focus on the more optimized solutions like quicksort &amp;amp; merge sort&lt;/p&gt;

&lt;h4&gt;
  
  
  Part 2
&lt;/h4&gt;

&lt;p&gt;Coursera &lt;a href="https://www.coursera.org/learn/algorithms-part2"&gt;https://www.coursera.org/learn/algorithms-part2&lt;/a&gt;&lt;br&gt;
Youtube &lt;a href="https://www.youtube.com/watch?v=0qF7tPSQdCg"&gt;https://www.youtube.com/watch?v=0qF7tPSQdCg&lt;/a&gt; and &lt;a href="https://www.youtube.com/watch?v=6TW3JSVEJQE"&gt;https://www.youtube.com/watch?v=6TW3JSVEJQE&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You only need a few topics from part 2, like graphs and tries&lt;/p&gt;

&lt;p&gt;There's also a cheat sheet from the course here &lt;a href="https://algs4.cs.princeton.edu/cheatsheet/"&gt;https://algs4.cs.princeton.edu/cheatsheet/&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Your goal is to understand the basic ideas from the course, then you will practice more coding when you start solving problems. Try not to take more than 1-2 months covering this course.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.3 Practice problem solving
&lt;/h3&gt;

&lt;p&gt;There are a lot of websites to practice coding problems, but what I found most useful is Leetcode which has a paid feature that shows you the exact questions that were asked at a specific company, and also can sort by most commonly asked questions, so you can practice with the most likely &amp;amp; realistic questions eg: &lt;a href="https://leetcode.com/company/amazon/"&gt;https://leetcode.com/company/amazon/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Leetcode premium plan is $35/months and you will only need it for 1-2 months when you are preparing. If this is too expensive in your local currency or you are a student, you can reach out to Leetcode and ask if they have discounts for your case. Lots of tech companies will happily offer a special discount even if they don't advertise it publicly.&lt;/p&gt;

&lt;p&gt;It’s important to cover as many problem ideas as you can, even if you think about a problem for a few minutes then jump to see the solutions. Don’t think too long about problems marked “hard” or dynamic programming problems as they aren’t likely to show up in the interview, just check the solution and move on. Try to code 1 problem every study session and think about a few other problems then read their solutions. Practice to think out loud and narrate the solution as you code.&lt;/p&gt;

&lt;p&gt;There's a good statistical likelihood that you won't pass the interviews on your first try, or that you will interview again in ~2 years if you get hired. So you should keep notes of all the problems you read with their links and their high level solution. This will save you a lot of time when you prepare for the next interview as you will start by going through these problems and refresh your memory instead of starting from scratch.&lt;/p&gt;

&lt;p&gt;Also, this nice site will send you a random algorithms problem every day &lt;a href="https://www.dailycodingproblem.com/"&gt;https://www.dailycodingproblem.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the final review days before the interview, review the code for the common algorithms like depth first search, breadth first search, tree traversals with different order &amp;amp; binary search&lt;/p&gt;

&lt;h3&gt;
  
  
  1.4 Debug your code WITHOUT RUNNING IT
&lt;/h3&gt;

&lt;p&gt;This is the single most important tip in this whole article. When you're practicing with something like Leetcode, it's very tempting to run your code immediately and see which cases fail and try to guess what went wrong. This is likely what you do when writing code daily, you write some code and run it to see if it works. This is the worst thing you can do when preparing for interviews.&lt;/p&gt;

&lt;p&gt;In a problem solving interview, you usually won't be able to run your code, and interviewers expect you to be able to review your code and spot any bugs. Think of it as reviewing a pull request and trying to find issues with it. You should try to find all bugs and edge cases BEFORE running the code, as you will do in the actual interview. Your goal is to pass all test cases on the first run. More details on how to do this below.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.5 Practice interviewing
&lt;/h3&gt;

&lt;p&gt;The best practice for interviews is interviews. Try to interview more often. Do not wait 4-5 months to prepare before an interview. You will learn more from failing an interview than any preparation, and most companies allow you to apply again after 6-12 months. &lt;/p&gt;

&lt;p&gt;If you have an important interview at a company you like, try to arrange 1-2 interviews at other companies (with a similar process) before it as practice. This way you will be less stressed about the important interview and if you end up getting multiple offers you will be in a better place to negotiate. &lt;/p&gt;

&lt;p&gt;Even if you aren't actively looking to move, it's useful to interview every 1-2 years. This will help keep your interviewing skills sharp, and if you get a better offer it can help you negotiate a raise or promotion if you decide to stay.&lt;/p&gt;

&lt;p&gt;If you can't easily get interviews at multiple companies, there are online services to set up mock interviews with experienced interviewers and get their feedback. I haven't tried these services so I can't recommend any of them. If you have a recommendation, please leave a comment with it.&lt;/p&gt;




&lt;h1&gt;
  
  
  2 In the interview
&lt;/h1&gt;

&lt;h3&gt;
  
  
  2.1 Before coding
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Most companies switched from whiteboards to a code editor for coding interviews. If you have a choice, I recommend using a text editor as it’s much easier and faster to modify your code than on a whiteboard.&lt;/li&gt;
&lt;li&gt;If you have a choice of which programming language to use, choose the one you’re most comfortable with. You want to focus on solving the problem, not remembering syntax. If you know multiple languages, choose one that’s faster to write. For example: manipulating lists and maps is easier in JavaScript or Python than Java.&lt;/li&gt;
&lt;li&gt;Ask questions to clarify the problem and confirm your understanding.&lt;/li&gt;
&lt;li&gt;Ask about the input &amp;amp; output formats, and valid ranges.&lt;/li&gt;
&lt;li&gt;Mention any assumptions you make (don’t assume trees are binary search trees or balanced).&lt;/li&gt;
&lt;li&gt;Draw/solve the problem verbally with some examples to validate your ideas.&lt;/li&gt;
&lt;li&gt;Mention non-optimum solutions briefly but try to come up with a more performant solution before starting to code.&lt;/li&gt;
&lt;li&gt;Mention the expected space &amp;amp; time complexity of your solution and if there are bounds on complexity, eg: if you must check all array items the complexity can't be lower than O(n). Make sure you include any steps like sorting or copying lists in the complexity.&lt;/li&gt;
&lt;li&gt;Think about possible preprocessing scenarios that can optimize the overall solution, like sorting an array before traversing.&lt;/li&gt;
&lt;li&gt;Some problems can be optimized by mutating the input, ask the interviewer such a solution is acceptable.&lt;/li&gt;
&lt;li&gt;Don’t start coding until you and the interviewer feel good about the solution.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2.2 During coding
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Outline the skeleton of the solution with comments, this prevents you from forgetting a step and helps the interviewer follow along.&lt;/li&gt;
&lt;li&gt;Write correct code, not pseudo-code. Minor syntax mistakes are usually not a problem.&lt;/li&gt;
&lt;li&gt;Apply all best practices of the language you use. Show the interviewer that you can write clean, maintainable code.&lt;/li&gt;
&lt;li&gt;If you’re using an uncommon language or one that the company does not use, explain any advanced language features you use that the interviewer may not be familiar with.&lt;/li&gt;
&lt;li&gt;Split your code to smaller functions with expressive names to clarify intent and make it easier to test. You may not need to implement some small functions if the interview says they aren’t important, which can save you some time.&lt;/li&gt;
&lt;li&gt;Explain your solution and decisions as you code. Think about which alternative provides a more accurate solution, is this solution opinionated or generic?&lt;/li&gt;
&lt;li&gt;Leave a &lt;code&gt;// TODO&lt;/code&gt; for input validation and other non-critical steps if you don’t want to spend time on them initially. The interviewer will notice your attention to these details.&lt;/li&gt;
&lt;li&gt;Use clear &amp;amp; expressive variable names to help the interviewer understand and help yourself avoid mistakes. Instead of names like &lt;code&gt;i&lt;/code&gt; or &lt;code&gt;j&lt;/code&gt;, use something like &lt;code&gt;currentBookIndex&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Use variables for array access to clarify intent, eg: &lt;code&gt;currentCell = grid[rowIndex][columnIndex]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Think about early return conditions and optimizations.&lt;/li&gt;
&lt;li&gt;For most problems, the solution should not be too long or too complicated. If your code exceeds ~30 lines and has several if/else branches, try to rethink it.&lt;/li&gt;
&lt;li&gt;When stuck, think out loud and try to draw your thoughts on paper. Listen to the interviewer if they provide hints, they won't trick you.&lt;/li&gt;
&lt;li&gt;Mention any limitations or potential optimizations. You can leave comments to revisit them if you have time later.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2.3 After coding
&lt;/h3&gt;

&lt;p&gt;This is the critical part. Most people think they are done after coding the solution and don't pay attention to these steps. I personally failed multiple interviews just because I was too tired or too hasty to follow them properly&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run through the code verbally. Read the actual code you wrote carefully, not what you think you wrote. Forget that you wrote the code and examine it like it's garbage code that some idiot posted on Stack Overflow. You are biased to think that your own code is bug free and it takes effort to train yourself to interrupt this bias. If you can't find 1-2 bugs, then you need to try harder.&lt;/li&gt;
&lt;li&gt;Look for specific parts of code that tend to cause bugs like: off by one errors in loops, math calculations, recursion exit conditions, what if a loop has no elements, what if a stack/queue becomes empty, what if input is null, empty input array, array of 0/1/2 items, input with duplicates, 0 or negative numbers, ...&lt;/li&gt;
&lt;li&gt;Verify that space &amp;amp; time complexity match what you expect.&lt;/li&gt;
&lt;li&gt;Ask the interviewer if you should come up with test cases for the code. List test cases with all different scenarios as if you are writing unit tests for your code.&lt;/li&gt;
&lt;li&gt;Ask the interviewer if you should trace the code with a test case. Choose a small but non-straightforward test case to trace and debug your code on paper/board. Go through the code line by line keeping track of variable values and look for any bugs and modify your code as needed.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Final remarks
&lt;/h3&gt;

&lt;p&gt;Problem solving interviews require some specific skills which may not always be relevant to your actual job. You need to train for these skills and understand that it might take a few tries to get it right.&lt;/p&gt;

&lt;p&gt;If you fail, remind yourself that these interviews are not a great measure of your skill as an engineer which is impossible to measure in an hour. It doesn't matter how many times you fail as you only need to make it once. Learn from your failure and document all questions you encounter in interviews or practice to help you prepare better next time.&lt;/p&gt;

</description>
      <category>career</category>
      <category>tutorial</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>I Built a Site for the Best Coding Fonts</title>
      <dc:creator>Mostafa Gaafar</dc:creator>
      <pubDate>Tue, 14 Apr 2020 15:11:26 +0000</pubDate>
      <link>https://forem.com/gafi/i-built-a-site-with-the-best-coding-fonts-295h</link>
      <guid>https://forem.com/gafi/i-built-a-site-with-the-best-coding-fonts-295h</guid>
      <description>&lt;p&gt;I'm always looking for a coding font that looks great with my favorite editor theme and language. Searching for coding fonts has been a pain since there is no way to see how a font will look until you download and install the font, then change your editor to use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enter Dev Fonts
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://devfonts.gafi.dev/"&gt;devfonts.gafi.dev&lt;/a&gt; is a list of the best coding fonts I could find around, along with an interactive preview where you can change the preview theme, code language, or the code sample for the preview.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d8vN0J0w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vdy2yb34po2pia9sginj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d8vN0J0w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vdy2yb34po2pia9sginj.png" alt="Dev Fonts screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My current favorite font is Cascadia Code. What is yours?&lt;/p&gt;

&lt;p&gt;The source code for the site is on &lt;a href="https://github.com/Gaafar/dev-fonts"&gt;github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PRs for missing fonts are welcome, check the &lt;a href="https://github.com/Gaafar/dev-fonts/blob/master/README.md#missing-a-font"&gt;notes here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>design</category>
      <category>productivity</category>
      <category>vscode</category>
    </item>
    <item>
      <title>7 Exciting New JavaScript Features You Need to Know</title>
      <dc:creator>Mostafa Gaafar</dc:creator>
      <pubDate>Mon, 05 Aug 2019 15:08:46 +0000</pubDate>
      <link>https://forem.com/gafi/7-new-exciting-javascript-features-you-need-to-know-1fkh</link>
      <guid>https://forem.com/gafi/7-new-exciting-javascript-features-you-need-to-know-1fkh</guid>
      <description>&lt;p&gt;This article has been translated to Japanese by &lt;a href="https://twitter.com/rana_kualu"&gt;@rana_kualu&lt;/a&gt; here &lt;a href="https://qiita.com/rana_kualu/items/ee7694aa1cd4ae7f4483"&gt;https://qiita.com/rana_kualu/items/ee7694aa1cd4ae7f4483&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JavaScript (or ECMA Script) is an evolving language with lots of proposals and ideas on how to move forward. TC39 (Technical Committee 39) is the committee responsible for defining JS standards and features, and they have been quite active this year. Here is a summary of some proposals that are currently in "Stage 3", which is the last stage before becoming "finished". This means that these features should be implemented in browsers and other engines pretty soon. In fact, some of them are available now.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Private fields &lt;code&gt;#&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Available in Chrome &amp;amp; NodeJS 12&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Yes, you read that right. Finally, JS is getting private fields in classes. No more &lt;code&gt;this._doPrivateStuff()&lt;/code&gt;, defining closures to store private values, or using &lt;code&gt;WeakMap&lt;/code&gt; to hack private props.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z93VbJxJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.meme-arsenal.com/memes/74402a52240be627fb62e298b1fe0897.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z93VbJxJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.meme-arsenal.com/memes/74402a52240be627fb62e298b1fe0897.jpg" alt="don't touch my garbage"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's how the syntax looks&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// private fields must start with '#'&lt;/span&gt;
&lt;span class="c1"&gt;// and they can't be accessed outside the class block&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;#&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;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="err"&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;x&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;onClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// works fine&lt;/span&gt;
&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Proposal: &lt;a href="https://github.com/tc39/proposal-class-fields"&gt;https://github.com/tc39/proposal-class-fields&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Optional Chaining &lt;code&gt;?.&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Ever had to access a property nested a few levels inside an object and got the infamous error &lt;code&gt;Cannot read property 'stop' of undefined&lt;/code&gt;. Then you change your code to handle every possible &lt;code&gt;undefined&lt;/code&gt; object in the chain, like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;stop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;please&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;please&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;make&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;please&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;make&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;it&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;please&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;make&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// or use a library like 'object-path'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;objectPath&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;please&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;make.it.stop&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;With optional chaining, soon you'll be able to get the same done writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;stop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;please&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;make&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Proposal: &lt;a href="https://github.com/tc39/proposal-optional-chaining"&gt;https://github.com/tc39/proposal-optional-chaining&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Nullish Coalescing &lt;code&gt;??&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;It's very common to have a variable with an optional value that can be missing, and to use a default value if it's missing&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;duration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;duration&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The problem with &lt;code&gt;||&lt;/code&gt; is that it will override all falsy values like (&lt;code&gt;0&lt;/code&gt;, &lt;code&gt;''&lt;/code&gt;, &lt;code&gt;false&lt;/code&gt;) which might be in some cases valid input.&lt;/p&gt;

&lt;p&gt;Enter the nullish coalescing operator, which only overrides &lt;code&gt;undefined&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;duration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;duration&lt;/span&gt; &lt;span class="p"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Proposal: &lt;a href="https://github.com/tc39/proposal-nullish-coalescing"&gt;https://github.com/tc39/proposal-nullish-coalescing&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. BigInt &lt;code&gt;1n&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Available in Chrome &amp;amp; NodeJS 12&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One of the reasons JS has always been terrible at Math is that we can't reliably store numbers larger than &lt;code&gt;2 ^ 53&lt;/code&gt;, which makes it pretty hard to deal with considerably large numbers. Fortunately, &lt;code&gt;BigInt&lt;/code&gt; is a proposal to solve this specific problem.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--URUEL7B2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgflip.com/p8blw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--URUEL7B2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgflip.com/p8blw.jpg" alt="Trump: gonna be HUUUUUGE"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Without further ado&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// can define BigInt by appending 'n' to a number literal&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;theBiggestInt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9007199254740991&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// using the constructor with a literal&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;alsoHuge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;BigInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9007199254740991&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// or with a string&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hugeButString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;BigInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;9007199254740991&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;You can also use the same operators on &lt;code&gt;BigInt&lt;/code&gt; as you would expect from regular numbers, eg: &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;%&lt;/code&gt;, ... There's a catch though, you can't mix &lt;code&gt;BigInt&lt;/code&gt; with numbers in most operations. Comparing &lt;code&gt;Number&lt;/code&gt; and &lt;code&gt;BigInt&lt;/code&gt; works, but not adding them&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; 
&lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="c1"&gt;// 🤷‍♀️ Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Proposal: &lt;a href="https://github.com/tc39/proposal-bigint"&gt;https://github.com/tc39/proposal-bigint&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. &lt;code&gt;static&lt;/code&gt; Fields
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Available in Chrome &amp;amp; NodeJS 12&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This one is pretty straightforward. It allows having static fields on classes, similar to most OOP languages. Static fields can be useful as a replacement for enums, and they also work with private fields.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Colors&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// public static fields&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;red&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#ff0000&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;green&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#00ff00&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// private static fields&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;secretColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#f0f0f0&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;font&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Colors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;font&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Colors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;secretColor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Proposal: &lt;a href="https://github.com/tc39/proposal-static-class-features"&gt;https://github.com/tc39/proposal-static-class-features&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Top Level &lt;code&gt;await&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Available in Chrome&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Allows you to use await at the top level of your code. This is super useful for debugging async stuff (like &lt;code&gt;fetch&lt;/code&gt;) in the browser console without wrapping it in an async function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wCIIk7Oa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/y5ur91fgud4pu7hh5ypv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wCIIk7Oa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/y5ur91fgud4pu7hh5ypv.png" alt="using await in browser console"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you need a refresher on async &amp;amp; await, &lt;a href="https://dev.to/gafi/7-reasons-to-always-use-async-await-over-plain-promises-tutorial-4ej9"&gt;check my article explaining it here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another killer use case is that it can be used at the top level of ES modules that initialize in an async manner (think about your database layer establishing a connection). When such an "async module" is imported, the module system will wait for it to resolve before executing the modules that depend on it. This will make dealing with async initialization much easier than the current workarounds of returning an initialization promise and waiting for it. A module will not know whether its dependency is async or not.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JDbkHgPB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://24.media.tumblr.com/tumblr_m3x648wxbj1ru99qvo1_500.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JDbkHgPB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://24.media.tumblr.com/tumblr_m3x648wxbj1ru99qvo1_500.png" alt="wait for it"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// db.mjs&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;connection&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;createConnection&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// server.mjs&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;connection&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./db.mjs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;start&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, nothing will execute in &lt;code&gt;server.mjs&lt;/code&gt; until the connection is complete in &lt;code&gt;db.mjs&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Proposal: &lt;a href="https://github.com/tc39/proposal-top-level-await"&gt;https://github.com/tc39/proposal-top-level-await&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  7. &lt;code&gt;WeakRef&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Available in Chrome &amp;amp; NodeJS 12&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A weak reference to an object is a reference that is not enough to keep an object alive. Whenever we create a variable with (&lt;code&gt;const&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;var&lt;/code&gt;) the garbage collector (GC) will never remove that variable from memory as long as its reference is still accessible. These are all strong references. An object referenced by a weak reference, however, may be removed by the GC at any time if there is no strong reference to it. A &lt;code&gt;WeakRef&lt;/code&gt; instance has a method &lt;code&gt;deref&lt;/code&gt; which returns the original object referenced, or &lt;code&gt;undefined&lt;/code&gt; if the original object has been collected.&lt;/p&gt;

&lt;p&gt;This might be useful for caching cheap objects, where you don't want to keep storing all of them in memory forever.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&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;setValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;obj&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;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;WeakRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&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;getValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&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;ref&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;ref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deref&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;// this will look for the value in the cache&lt;/span&gt;
&lt;span class="c1"&gt;// and recalculate if it's missing&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fibonacciCached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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;cached&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;cached&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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;calculateFibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sum&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;sum&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;This is probably not a good idea for caching remote data as it can be removed from memory unpredictably. It's better to use something like an LRU cache in that case.&lt;/p&gt;

&lt;p&gt;Proposal: &lt;a href="https://github.com/tc39/proposal-weakrefs"&gt;https://github.com/tc39/proposal-weakrefs&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;That's it. I hope you're as excited as I am to use these cool new features. For more details on these proposals and others that I didn't mention, &lt;a href="https://github.com/tc39/proposals"&gt;keep an eye on TC39 proposals on github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>7 Reasons Why JavaScript Async/Await Is Better Than Plain Promises (Tutorial)</title>
      <dc:creator>Mostafa Gaafar</dc:creator>
      <pubDate>Mon, 29 Jul 2019 00:26:45 +0000</pubDate>
      <link>https://forem.com/gafi/7-reasons-to-always-use-async-await-over-plain-promises-tutorial-4ej9</link>
      <guid>https://forem.com/gafi/7-reasons-to-always-use-async-await-over-plain-promises-tutorial-4ej9</guid>
      <description>&lt;p&gt;Async/await was introduced in NodeJS 7.6 and is currently supported in all modern browsers. I believe it has been the single greatest addition to JS since 2017. If you are not convinced, here are a bunch of reasons with examples why you should adopt it immediately and never look back.&lt;/p&gt;

&lt;h3&gt;
  
  
  Async/Await 101
&lt;/h3&gt;

&lt;p&gt;For those who have never heard of this topic before, here’s a quick intro&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Async/await is a new way to write asynchronous code. Previous alternatives for asynchronous code are callbacks and promises.&lt;/li&gt;
&lt;li&gt;Async/await is actually just syntax sugar built on top of promises. It cannot be used with plain callbacks or node callbacks.&lt;/li&gt;
&lt;li&gt;Async/await is, like promises, non-blocking.&lt;/li&gt;
&lt;li&gt;Async/await makes asynchronous code look and behave a little more like synchronous code. This is where all its power lies.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Syntax
&lt;/h3&gt;

&lt;p&gt;Assuming a function &lt;code&gt;getJSON&lt;/code&gt; that returns a promise, and that promise resolves with some JSON object. We just want to call it and log that JSON, then return &lt;code&gt;"done"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is how you would implement it using promises&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And this is how it looks with async/await&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;There are a few differences here&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Our function has the keyword &lt;code&gt;async&lt;/code&gt; before it. The &lt;code&gt;await&lt;/code&gt; keyword can only be used inside functions defined with &lt;code&gt;async&lt;/code&gt;. Any &lt;code&gt;async&lt;/code&gt; function returns a promise implicitly, and the resolve value of the promise will be whatever you &lt;code&gt;return&lt;/code&gt; from the function (which is the string &lt;code&gt;"done"&lt;/code&gt; in our case).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The above point implies that we can’t use &lt;code&gt;await&lt;/code&gt; at the top level of our code since that is not inside an &lt;code&gt;async&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;await getJSON()&lt;/code&gt; means that the &lt;code&gt;console.log&lt;/code&gt; call will wait until &lt;code&gt;getJSON()&lt;/code&gt; promise resolves and print its value.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Why Is It better?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Concise and clean&lt;br&gt;
Look at how much code we didn’t write! Even in the contrived example above, it’s clear we saved a decent amount of code. We didn’t have to write &lt;code&gt;.then&lt;/code&gt;, create an anonymous function to handle the response, or give a name &lt;code&gt;data&lt;/code&gt; to a variable that we don’t need to use. We also avoided nesting our code. These small advantages add up quickly, which will become more obvious in the following code examples.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Error handling&lt;br&gt;
Async/await makes it finally possible to handle both synchronous and asynchronous errors with the same construct, good old &lt;code&gt;try/catch&lt;/code&gt;. In the example below with promises, the &lt;code&gt;try/catch&lt;/code&gt; will not handle if &lt;code&gt;JSON.parse&lt;/code&gt; fails because it’s happening inside a promise. We need to call &lt;code&gt;.catch&lt;/code&gt; on the promise and duplicate our error handling code, which will (hopefully) be more sophisticated than &lt;code&gt;console.log&lt;/code&gt; in your production-ready code.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now look at the same code with async/await. The &lt;code&gt;catch&lt;/code&gt; block now will handle parsing errors.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Conditionals&lt;br&gt;
Imagine something like the code below which fetches some data and decides whether it should return that or get more details based on some value in the data.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Just looking at this gives you a headache. It’s easy to get lost in all that nesting (6 levels), braces, and return statements that are only needed to propagate the final result up to the main promise.&lt;/p&gt;

&lt;p&gt;This example becomes way more readable when rewritten with async/await.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Intermediate values&lt;br&gt;
You have probably found yourself in a situation where you call a &lt;code&gt;promise1&lt;/code&gt; and then use what it returns to call &lt;code&gt;promise2&lt;/code&gt;, then use the results of both promises to call a &lt;code&gt;promise3&lt;/code&gt;. Your code most likely looked like this&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If &lt;code&gt;promise3&lt;/code&gt; didn’t require &lt;code&gt;value1&lt;/code&gt; it would be easy to flatten the promise nesting a bit. If you are the kind of person who couldn’t live with this, you could wrap both values 1 &amp;amp; 2 in a &lt;code&gt;Promise.all&lt;/code&gt; and avoid deeper nesting, like this&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This approach sacrifices semantics for the sake of readability. There is no reason for &lt;code&gt;value1&lt;/code&gt; &amp;amp; &lt;code&gt;value2&lt;/code&gt; to belong in an array together, except to avoid nesting promises.&lt;br&gt;
This same logic becomes ridiculously simple and intuitive with async/await. It makes you wonder about all the things you could have done in the time that you spent struggling to make promises look less hideous.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Error stacks&lt;br&gt;
Imagine a piece of code that calls multiple promises in a chain, and somewhere down the chain, an error is thrown.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The error stack returned from a promise chain gives no clue of where the error happened. Even worse, it’s misleading; the only function name it contains is &lt;code&gt;callAPromise&lt;/code&gt; which is totally innocent of this error (the file and line number are still useful though).&lt;br&gt;
However, the error stack from async/await points to the function that contains the error&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This is not a huge plus when you’re developing on your local environment and have the file open in an editor, but it’s quite useful when you’re trying to make sense of error logs coming from your production server. In such cases, knowing the error happened in &lt;code&gt;makeRequest&lt;/code&gt; is better than knowing that the error came from a &lt;code&gt;then&lt;/code&gt; after a &lt;code&gt;then&lt;/code&gt; after a &lt;code&gt;then&lt;/code&gt; …&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Debugging&lt;br&gt;
A killer advantage when using async/await is that it’s much easier to debug. Debugging promises has always been such a pain for 2 reasons&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;You can’t set breakpoints in arrow functions that return expressions (no body).&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%2Fmiro.medium.com%2Fmax%2F1260%2F1%2An_V4LaVdBOFgGCbmTR_VKA.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%2Fmiro.medium.com%2Fmax%2F1260%2F1%2An_V4LaVdBOFgGCbmTR_VKA.png" alt="snippet with chained promises"&gt;&lt;/a&gt;&lt;em&gt;Try setting a breakpoint anywhere here&lt;/em&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If you set a breakpoint inside a &lt;code&gt;.then&lt;/code&gt; block and use debug shortcuts like step-over, the debugger will not move to the following &lt;code&gt;.then&lt;/code&gt; because it only “steps” through synchronous code.&lt;/p&gt;

&lt;p&gt;With async/await you don’t need arrow functions as much, and you can step through await calls exactly as if they were normal synchronous calls.&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%2Fmiro.medium.com%2Fmax%2F1260%2F1%2AGWYd4eLrs0U96MkNNVB56A.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%2Fmiro.medium.com%2Fmax%2F1260%2F1%2AGWYd4eLrs0U96MkNNVB56A.png" alt="snippet with consecutive awaits"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;You can &lt;code&gt;await&lt;/code&gt; anything&lt;br&gt;
Last but not least, &lt;code&gt;await&lt;/code&gt; can be used for both synchronous and asynchronous expressions. For example, you can write &lt;code&gt;await 5&lt;/code&gt;, which is equivalent to &lt;code&gt;Promise.resolve(5)&lt;/code&gt;. This might not seem very useful at first, but it's actually a great advantage when writing a library or a utility function where you don't know whether the input will be sync or async. &lt;/p&gt;

&lt;p&gt;Imagine you want to record the time taken to execute some API calls in your application, and you decide to create a generic function for this purpose. Here's how it would look with promises&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You know that all API calls are going to return promises, but what happens if you use the same function to record the time taken in a synchronous function? It will throw an error because the sync function does not return a promise. The usual way to avoid this is wrapping &lt;code&gt;makeRequest()&lt;/code&gt; in &lt;code&gt;Promise.resolve()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you use async/await, you won't have to worry about these cases because await allows you to work safely with any value, promise or not.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;/li&gt;

&lt;/ol&gt;

&lt;h3&gt;
  
  
  In Conclusion
&lt;/h3&gt;

&lt;p&gt;Async/await is one of the most revolutionary features that have been added to JavaScript in the past few years. It makes you realize what a syntactical mess promises are, and provides an intuitive replacement.&lt;/p&gt;

&lt;h3&gt;
  
  
  Concerns
&lt;/h3&gt;

&lt;p&gt;Some valid skepticism you might have about using async/await is that it makes asynchronous code less obvious: Our eyes learned to spot asynchronous code whenever we see a callback or a &lt;code&gt;.then&lt;/code&gt;, it will take a few weeks for your eyes to adjust to the new signs, but C# had this feature for years and people who are familiar with it know it’s worth this minor, temporary inconvenience.&lt;/p&gt;

&lt;p&gt;Follow me on twitter &lt;a href="https://twitter.com/imGaafar" rel="noopener noreferrer"&gt;@imgaafar&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article was originally &lt;a href="https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec10518dd9" rel="noopener noreferrer"&gt;published on Hackernoon&lt;/a&gt;&lt;/p&gt;

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