<?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: Pippa Thompson</title>
    <description>The latest articles on DEV Community by Pippa Thompson (@ppiippaa).</description>
    <link>https://dev.to/ppiippaa</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%2F1025808%2F8f3f49a3-d5a3-453c-8d91-9b58836b75d7.jpeg</url>
      <title>DEV Community: Pippa Thompson</title>
      <link>https://dev.to/ppiippaa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ppiippaa"/>
    <language>en</language>
    <item>
      <title>Web Dev Related Acronyms You Should Know</title>
      <dc:creator>Pippa Thompson</dc:creator>
      <pubDate>Mon, 20 Mar 2023 09:42:23 +0000</pubDate>
      <link>https://dev.to/ppiippaa/web-dev-related-acronyms-you-should-know-35ao</link>
      <guid>https://dev.to/ppiippaa/web-dev-related-acronyms-you-should-know-35ao</guid>
      <description>&lt;p&gt;Web dev acronyms - ftw or wtf? &lt;/p&gt;

&lt;p&gt;This article is a reference and a brief, high-level overview of an assortment of acronyms that you may come across in the web development wild. If, like me, you have the memory of a sieve then I hope that this article might come in handy for reminding you what they really mean. This is by no means an exhaustive list and I’m sure there are a many more out there, so if you have any suggestions please let me know ☺️.&lt;/p&gt;

&lt;p&gt;Happy reading!&lt;/p&gt;

&lt;h2&gt;
  
  
  CRUD
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Create Read Update Delete
&lt;/h3&gt;

&lt;p&gt;CRUD refers to four common operations of persistent storage, i.e. operations available to perform on data in some sort of database. &lt;br&gt;
A super creative example is that of a to-do list. Each to-do should have methods available for the creation, reading (essentially fetching), updating and deletion of said to-do.&lt;/p&gt;

&lt;h2&gt;
  
  
  OS
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Operating System
&lt;/h3&gt;

&lt;p&gt;The OS of a machine refers to the software installed which enables the user to communicate with the device - it is the layer between the user and the device’s hardware components. Examples include Apple’s macOS, Microsoft’s Windows and Linux which is an open-source OS. &lt;/p&gt;

&lt;h2&gt;
  
  
  GUI
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Graphical User Interface
&lt;/h3&gt;

&lt;p&gt;A GUI enables a user to communicate with a device visually. Elements of a GUI include icons, menus and a mouse to click/drag/scroll on screen. Generally speaking they are more intuitive and easier to work with than a CLI (coming up next), although less powerful in their operating power. &lt;/p&gt;

&lt;h2&gt;
  
  
  CLI
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Command Line Interface
&lt;/h3&gt;

&lt;p&gt;A CLI is a type of program which accepts text inputs in order to execute functions on an operating system. Nowadays a CLI is commonly used to install software, configure computers or access functionality not accessible via a GUI. Back in the early days of computers, users could only interact using a keyboard so a CLI was their only option. There are different variations depending on the OS, so for a deeper dive plus a bit more background information I recommend reading this &lt;a href="https://www.hostinger.com/tutorials/what-is-cli"&gt;article&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  NPM &amp;amp; NPX
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Node Package Manager &amp;amp; Node Package eXecute
&lt;/h3&gt;

&lt;p&gt;NPM is an online repository of open source Node projects. It is also a CLI tool with a variety of commands available which enable developers to install these packages in order to use them in their own projects.&lt;/p&gt;

&lt;p&gt;As well as also being a CLI tool, NPX is a Node package runner; it allows developers to execute a package from the NPM registry without installing the package, so is useful for a single time use package. As of version 5.2.0, it comes pre-bundled with NPM.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTML
&lt;/h2&gt;

&lt;h3&gt;
  
  
  HyperText Markup Language
&lt;/h3&gt;

&lt;p&gt;HTML is the standard markup language used on the web. It requires its own &lt;code&gt;.html&lt;/code&gt; file type. It is made up of a collection of elements which are also referred to as tags. HTML was designed to be very descriptive, so examples of elements include &lt;code&gt;&amp;lt;h1&amp;gt;&amp;lt;/h1&amp;gt;&lt;/code&gt; which represents a heading 1 and &lt;code&gt;&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt; which represents a divider element. &lt;/p&gt;

&lt;p&gt;HTML not only describes the structure of a web page, it also instructs the browser how to display the data on the page with basic styling. Different browsers might have slightly different styling implementation. Since the inception of the web, there have been many different versions of HTML, with the latest standard being HTML5.&lt;/p&gt;

&lt;h2&gt;
  
  
  XML
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Extensible Markup Language
&lt;/h3&gt;

&lt;p&gt;XML is, like HTML, both a type of markup language and a file type. Its format is text based, and it is predominantly used for storing information (such as documents, data, invoices and more) and sharing said information between programs, people and computers. It is software and hardware independent. One main difference between HTML and XML is that the main purpose of XML is simply to store/transport data and does not focus on the visual displaying of data as much. &lt;/p&gt;

&lt;h2&gt;
  
  
  JSX
&lt;/h2&gt;

&lt;h3&gt;
  
  
  JavaScript XML
&lt;/h3&gt;

&lt;p&gt;JSX is a syntax extension of JavaScript which allows you to write HTML-looking code whilst also harnessing the power of JavaScript. &lt;/p&gt;

&lt;p&gt;If you’ve ever worked with React, you will most likely have come across JSX. Although it is possible to use React without JSX, more often than not you will see it in React projects.&lt;/p&gt;

&lt;p&gt;Given React’s focus on the separation of concerns as opposed to separation of technologies, rather than writing markup and logic in separate files JSX enables the developer to combine logic and markup into separate components.&lt;/p&gt;

&lt;h2&gt;
  
  
  CMS
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Content Management System
&lt;/h3&gt;

&lt;p&gt;Exactly as it sounds, a CMS is some sort of system developed to manage content. The content is typically stored in a database and the presentational layer then displays the stored data. This enables multiple users to edit data and publish any changes in a relatively pain-free process. Common examples include software such as WordPress, Wix and Drupal.&lt;/p&gt;

&lt;h2&gt;
  
  
  IAM
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Identity Access Management
&lt;/h3&gt;

&lt;p&gt;IAM is a collection of systems, policies and technologies which allow you to control who has access to digital resources and thus reduce the risk of data breaches. I recently read a really great analogy for IAM in this &lt;a href="https://dev.to/jareechang/understanding-identity-access-management-an-analogy-2png"&gt;article&lt;/a&gt; - I recommend reading it if you need some more clarification!&lt;/p&gt;

&lt;h2&gt;
  
  
  CI/CD
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Continuous Integration, Continuous Delivery and Deployment
&lt;/h3&gt;

&lt;p&gt;CI/CD refers to a set of tools/a method used for the development of software. It prioritises automation and monitoring of each stage of the software lifecycle in order to make the deployment process as pain-free as possible. Specifically, this could include practices like automated testing once a developer has merged their code to a shared branch and then automatically deployment of the changes once all tests have been passed. The complete process of all the different practices is referred to as a CI/CD pipeline. &lt;/p&gt;

&lt;h2&gt;
  
  
  COVE &amp;amp; PLSRD
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Closed Over Variable Environment &amp;amp; Persistent Lexically Scoped Referenced Data
&lt;/h3&gt;

&lt;p&gt;Both of these acronyms are synonyms for closure. Closure is a behavioural feature of JavaScript which allows a function to store a reference to the data that was live in its own local memory as well as its parent function’s memory when said function was created, if said function was returned from its parent function. &lt;/p&gt;

&lt;p&gt;In simple terms, the returned function “remembers” the data available in the environment in which it was created after it has been returned by its parent. &lt;/p&gt;

&lt;p&gt;There are countless resources out there which explain this concept in detail, but here’s one I found especially helpful: &lt;a href="https://dmitripavlutin.com/javascript-closure/"&gt;read it here&lt;/a&gt;. Frontend Masters also has some great video courses on the topic, in particular JS Hard Parts by Will Sentance, so if that is available to you it’s a great option to gain a deeper understanding.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Application Programming Interface
&lt;/h3&gt;

&lt;p&gt;An API acts as a sort of middleman which allows for communication between different software.&lt;/p&gt;

&lt;p&gt;A well-known analogy to describe APIs is that of a restaurant. You have a customer (representing the frontend) who wants to order a meal. In order to do so, this customer needs to relay their order to the chefs (representing the backend) in the kitchen. In comes the waiting staff (representing the API) who relay the customer’s order to the chefs and then bring the meal to the customer.   &lt;/p&gt;

&lt;p&gt;A common API that you might have come across while using JavaScript is .fetch(), which allows you to fetch data (known as resources) from the web via the browser. Without this communication functionality, the browser and the server wouldn’t know the other one existed, so .fetch() is acting as the link between the two. &lt;/p&gt;

&lt;p&gt;For more in depth information along with some interesting public APIs available out there, have a read of &lt;a href="https://www.freecodecamp.org/news/public-apis-for-developers/"&gt;this&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  AJAX
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Asynchronous JavaScript and XML
&lt;/h3&gt;

&lt;p&gt;AJAX is a terms that refers to a collection of techniques and technologies which allow the developer to create interactive websites and applications that can request data from a server without blocking other functionality on the web page. It relies on XML/JSON, JavaScript and XMLHttpRequest to retrieve data from a server. &lt;/p&gt;

&lt;h2&gt;
  
  
  REST
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Representational State Transfer
&lt;/h3&gt;

&lt;p&gt;REST is an architectural paradigm for communication between web-based systems and/or software (such as APIs). In order to be considered RESTful systems must comply with certain &lt;a href="https://restfulapi.net/"&gt;principals&lt;/a&gt;, with a particular focus on separation between the client and the server. RESTful communication is relatively easy to use and implements HTTP requests and endpoints/paths to manage the transfer of data. &lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP(S)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  HyperText Transfer Protocol (Secure)
&lt;/h3&gt;

&lt;p&gt;As the name suggests, HTTP is an application-layer protocol (i.e. a set of rules defining optimal conduct and procedures) for transferring hypermedia documents (such as HTML documents) over the web. Originally it was designed for transferring resources between browsers and servers, but it is possible to use it for other purposes. &lt;/p&gt;

&lt;p&gt;When a browser sends a request to a server for some data, it sends along an HTTP header which contains information about the request, such as an HTTP verb (GET, PUT, POST, DELETE are common but others exist) to describe the operation type. The server sends back a response header containing information about the response, such as the response data type, size and data itself amongst other things. &lt;/p&gt;

&lt;p&gt;HTTPS is an extension of HTTP and utilises encryption to protect data being sent over the web.&lt;/p&gt;

&lt;h2&gt;
  
  
  OOP
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Object Oriented Programming
&lt;/h3&gt;

&lt;p&gt;OOP is a programming paradigm, meaning an architectural style for writing code. It focuses on separating code into - you guessed it - objects in order to encapsulate data and functionality. This can make it easier to identify where issues are coming from during any debugging.&lt;/p&gt;

&lt;h2&gt;
  
  
  TCP
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Transmission Control Protocol
&lt;/h3&gt;

&lt;p&gt;Yet another protocol, TCP defines how data should be formatted in order to be sent between a server and a client. To be TCP compliant, before any data is transferred a connection between the data-requesting and the data-serving parties must be established and the requested data must be sent in a data packet (if the data requested is large, it may need to be broken down into multiple, smaller packets). &lt;/p&gt;

&lt;h2&gt;
  
  
  IP (address)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Internet Protocol (address)
&lt;/h3&gt;

&lt;p&gt;IP refers to the set of rules which govern how data is sent over the internet/local network. &lt;/p&gt;

&lt;p&gt;An IP address is a unique number given to every device that connects to any network which uses IP in order for these devices to be able to communicate with each other (i.e. send and receive data). In simple terms they describe where the data request is coming from or where to send requested data to, much like a normal home address. IP addresses are assigned to devices by ICCAN (&lt;a href="https://www.icann.org/"&gt;Internet Corporation for Assigned Names and Numbers&lt;/a&gt; - two acronyms in one!).&lt;/p&gt;

&lt;h2&gt;
  
  
  DOM
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Document Object Model
&lt;/h3&gt;

&lt;p&gt;The DOM is a cross-platform interface (API) that breaks down an HTML or XML document into a tree structure, with each node representing an element of the document. It does this in order to allow programmes to access, interact with and manipulate parts of the page. &lt;/p&gt;

&lt;h2&gt;
  
  
  XHR
&lt;/h2&gt;

&lt;h3&gt;
  
  
  XML HTTP Request
&lt;/h3&gt;

&lt;p&gt;Another API, XHR is an object provided by the browser and consists of methods which allow for data transfer between the browser and a web server. It falls under the umbrella of AJAX.   &lt;/p&gt;

&lt;h2&gt;
  
  
  ASCII
&lt;/h2&gt;

&lt;h3&gt;
  
  
  American Standard Code for Information Interchange
&lt;/h3&gt;

&lt;p&gt;ASCII was developed to enable different makes and models of electronic devices (including computers) to communicate with each other by standardising the representation of characters (letters, numbers, punctuation, etc). &lt;/p&gt;

&lt;p&gt;It is a character-encoding system that assigns a unique 8-bit (byte) number to each different character. &lt;/p&gt;

&lt;p&gt;One major drawback is the fact that ASCII only supports certain characters (numbers 0-9, upper case and lower case letters A-Z as well as some punctuation characters). This meant that languages not based on the Latin alphabet were not included in the system. In order to provide standardisation to a wider and more inclusive character set, Unicode was introduced in 1991 which is a more expansive character-encoding system supporting different alphabets.&lt;/p&gt;

&lt;h2&gt;
  
  
  JSON
&lt;/h2&gt;

&lt;h3&gt;
  
  
  JavaScript Object Notation
&lt;/h3&gt;

&lt;p&gt;JSON refers to a specific format for storing data. It is syntactically very similar to JavaScript objects as it stores data using key-value pairs, curly braces and commas for separation. It’s often used when sending/receiving data over the web.&lt;/p&gt;

&lt;h2&gt;
  
  
  SSL &amp;amp; TSL
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Secure Socket Layer &amp;amp; Transfer Socket Layer
&lt;/h3&gt;

&lt;p&gt;Both of these acronyms refer to a technology which is responsible for encrypting data sent over the internet. They utilise encryption algorithms to achieve this. TSL is just a more updated and secure version of SSL.  &lt;/p&gt;

&lt;h2&gt;
  
  
  CSS / SASS / SCSS
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Cascading Style Sheets / Syntactically Awesome Style Sheets / Sassy Cascading Style Sheets
&lt;/h3&gt;

&lt;p&gt;No doubt you’ll have come across this before if you’ve ever stepped foot in the frontend world. CSS is a style sheet language (which is written and stored in a &lt;code&gt;.css&lt;/code&gt; file) that specifies how a markup document should be displayed. It encompasses everything from the basics such as colours, size and font to more complex styling options such as animations. &lt;/p&gt;

&lt;p&gt;SASS and SCSS can be thought of as more “advanced” CSS. They are both CSS pre-processer languages which combine all the features of CSS whilst adding some extra behaviour, such as the ability to use variables and nesting.  SASS/SCSS files are then compiled in normal CSS that the browser can understand.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>frontend</category>
      <category>codenewbie</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>var, let or const: what's the real difference?</title>
      <dc:creator>Pippa Thompson</dc:creator>
      <pubDate>Wed, 01 Mar 2023 13:31:11 +0000</pubDate>
      <link>https://dev.to/ppiippaa/var-let-or-const-whats-the-real-difference-37l9</link>
      <guid>https://dev.to/ppiippaa/var-let-or-const-whats-the-real-difference-37l9</guid>
      <description>&lt;h2&gt;
  
  
  The &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; keywords
&lt;/h2&gt;

&lt;p&gt;At this point, the &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; keywords are pretty much ubiquitous. Before their existence, we could only use the &lt;code&gt;var&lt;/code&gt; keyword to declare variables which store our data. Although you will still encounter &lt;code&gt;var&lt;/code&gt; in JavaScript code today, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; bring with them some features which help address certain issues you might encounter when using &lt;code&gt;var&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scope
&lt;/h3&gt;

&lt;p&gt;One of the differences between &lt;code&gt;let&lt;/code&gt;/ &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;var&lt;/code&gt; relates to how they handle &lt;strong&gt;scope&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;Scope refers to the accessibility of variables and functions in different parts of a program. Being a lexically scoped language, the accessibility of variables and functions in JavaScript depends on where they are &lt;strong&gt;declared&lt;/strong&gt; in the program. Both &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are &lt;strong&gt;block scoped&lt;/strong&gt; whereas &lt;code&gt;var&lt;/code&gt; is not. This means that variables declared with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; within if/switch statements, for/while loops and curly braces &lt;code&gt;{ }&lt;/code&gt; &lt;strong&gt;are not accessible outside of this block&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let’s see how this block-scoped behaviour affects our 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="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="mi"&gt;3&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="nx"&gt;i&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;`var i = &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;`&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;let&lt;/span&gt; &lt;span class="nx"&gt;j&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;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&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="nx"&gt;j&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;`var j = &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// (first for loop - using var)&lt;/span&gt;
&lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="c1"&gt;// "var i = 3"&lt;/span&gt;
&lt;span class="c1"&gt;// (second for loop - using let)&lt;/span&gt;
&lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="c1"&gt;// "error"&lt;/span&gt;
&lt;span class="c1"&gt;// "ReferenceError: j is not defined"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the above example we have two for loops. In the first loop, we use the &lt;code&gt;var&lt;/code&gt; keyword to declare variable &lt;code&gt;i&lt;/code&gt;. Despite the fact that &lt;code&gt;i&lt;/code&gt; is within a for loop, we are still able to access it outside the for loop. &lt;br&gt;
In the second loop we use the &lt;code&gt;let&lt;/code&gt; keyword to declare variable &lt;code&gt;j&lt;/code&gt; . We try to access &lt;code&gt;j&lt;/code&gt; outside of the for loop but we receive the &lt;code&gt;ReferenceError: j is not defined&lt;/code&gt;. As it was declared using let, it is block scoped and therefore only accessible within the loop. &lt;/p&gt;

&lt;p&gt;This same behaviour occurs when using the &lt;code&gt;const&lt;/code&gt; keyword to declare variables within block scopes:&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="kd"&gt;let&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="mi"&gt;3&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pippa&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;myName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; -&amp;gt; &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;`&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;myName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// (console.logging within the for loop)&lt;/span&gt;
&lt;span class="c1"&gt;// "Pippa -&amp;gt; 0"&lt;/span&gt;
&lt;span class="c1"&gt;// "Pippa -&amp;gt; 1"&lt;/span&gt;
&lt;span class="c1"&gt;// "Pippa -&amp;gt; 2"&lt;/span&gt;
&lt;span class="c1"&gt;// (console.logging outside the for loop)&lt;/span&gt;
&lt;span class="c1"&gt;// "error"&lt;/span&gt;
&lt;span class="c1"&gt;// "ReferenceError: myName is not defined"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here we have declared the variable &lt;code&gt;myName&lt;/code&gt; using &lt;code&gt;const&lt;/code&gt; inside a for loop. Although we can access &lt;code&gt;myName&lt;/code&gt; inside the for loop as demonstrated by the &lt;code&gt;console.log(${myName} -&amp;gt; ${i})&lt;/code&gt;, we cannot access it from outside the loop and we receive a &lt;code&gt;ReferenceError&lt;/code&gt; when we try. &lt;/p&gt;

&lt;h3&gt;
  
  
  Redeclaration and Reassignment
&lt;/h3&gt;

&lt;p&gt;Another way in which &lt;code&gt;let&lt;/code&gt; / &lt;code&gt;const&lt;/code&gt; differ from &lt;code&gt;var&lt;/code&gt; relates to whether or not they can be redeclared or their values reassigned. Variables declared with &lt;code&gt;var&lt;/code&gt; can be both redeclared and reassigned.&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;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pippa&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;myName&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;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Philippa&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;myName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pippa Again&lt;/span&gt;&lt;span class="dl"&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;myName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt; &lt;/span&gt;
&lt;span class="c1"&gt;// "Pippa"&lt;/span&gt;
&lt;span class="c1"&gt;// "Philippa"&lt;/span&gt;
&lt;span class="c1"&gt;// "Pippa Again"&lt;/span&gt;

&lt;span class="c1"&gt;// No errors, we can declare myName, redeclare it and finally reassign the value.&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This can sometimes cause issues, especially when working with global variables. It’s one of the reasons that &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; were introduced because when using &lt;code&gt;var&lt;/code&gt; you could end up accidentally redeclaring variables. &lt;/p&gt;

&lt;p&gt;Let’s try the same using the &lt;code&gt;let&lt;/code&gt; keyword:&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;let&lt;/span&gt; &lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pippa&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;myName&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;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Philippa&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;myName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// "SyntaxError: Identifier 'myName' has already been declared"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here, we receive a syntax error informing us that the &lt;code&gt;myName&lt;/code&gt; variable has already been declared. So with &lt;code&gt;let&lt;/code&gt;, &lt;strong&gt;we cannot redeclare a variable&lt;/strong&gt;. We can, however, reassign values:&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;let&lt;/span&gt; &lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pippa&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;myName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Philippa&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;myName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// "Pippa"&lt;/span&gt;
&lt;span class="c1"&gt;// "Philippa"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;No errors! Rather than redeclaring &lt;code&gt;myName&lt;/code&gt; we just reassign its value, which is fine by &lt;code&gt;let&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;So how does &lt;code&gt;const&lt;/code&gt; deal with redeclaration of variables? &lt;code&gt;const&lt;/code&gt;, short for constant, displays the same behaviour as &lt;code&gt;let&lt;/code&gt; when it comes to redeclaring variable, i.e. &lt;strong&gt;it does not allow variable redeclaration&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;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pippa&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;myName&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;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Philippa&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;myName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// "Pippa"&lt;/span&gt;
&lt;span class="c1"&gt;// "SyntaxError: Identifier 'myName' has already been declared&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The same as with &lt;code&gt;let&lt;/code&gt;, we receive a &lt;code&gt;SyntaxError&lt;/code&gt; when trying to redeclare a &lt;code&gt;const&lt;/code&gt; variable. But what about reassigning the value of a &lt;code&gt;const&lt;/code&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;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pippa&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;myName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Philippa&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;myName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// "Pippa"&lt;/span&gt;
&lt;span class="c1"&gt;// "error"&lt;/span&gt;
&lt;span class="c1"&gt;// "TypeError: Assignment to constant variable.&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;That’s also a big no-no. This time we receive a &lt;code&gt;TypeError&lt;/code&gt; telling us we have assigned a constant variable, which cannot be changed. As such, &lt;code&gt;const&lt;/code&gt; is a good option to use for variables which you know won’t change. &lt;/p&gt;

&lt;p&gt;While useful in some cases, doesn’t this behaviour seem a bit inflexible? Let me expand a bit more on the details of this. As we just saw,  when using &lt;code&gt;const&lt;/code&gt; to store primitive data types (&lt;code&gt;Number&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;null&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;BigInt&lt;/code&gt; and &lt;code&gt;Symbol&lt;/code&gt;) we cannot redeclare or reassign the value. However, this is not the case when using &lt;code&gt;const&lt;/code&gt; to store non-primitive data types (objects, which include arrays and functions). &lt;/p&gt;

&lt;p&gt;When storing non-primitive data types, although we cannot redeclare them, we can mutate them. An example of this would be using &lt;code&gt;const&lt;/code&gt; to store an object and then later adding on a new property or method. &lt;/p&gt;

&lt;p&gt;The reason for this relates to how JavaScript stores different types of data. When we store an object (or any non-primitive data type) in a variable, for example &lt;code&gt;const myObject = {...}&lt;/code&gt;, we are saving a reference to the object (essentially an “address” to a point in memory in the JavaScript memory heap) rather than storing the object itself. Primitive types in JavaScript are stored in a different way; the actual value is stored as opposed to a reference to a point in memory.&lt;/p&gt;

&lt;p&gt;This means that we can use &lt;code&gt;const&lt;/code&gt; to store an object, and later mutate this object as it is a non-primitive data type:&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;carObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;make&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ford&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;wheels&lt;/span&gt;&lt;span class="dl"&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="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;carObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;carObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;colour&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&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;carObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="cm"&gt;/*
FIRST CONSOLE.LOG()
[object Object] {
  make: "Ford",
  wheels: 4
}
SECOND CONSOLE.LOG()
[object Object] {
  colour: "red",
  make: "Ford",
  wheels: 4
}
*/&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;As you can see, we declared &lt;code&gt;carObject&lt;/code&gt; using &lt;code&gt;const&lt;/code&gt; and were able to add the colour property on later as the &lt;strong&gt;reference to the object&lt;/strong&gt; which was saved in &lt;code&gt;carObject&lt;/code&gt; &lt;strong&gt;has not been changed&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hoisting
&lt;/h3&gt;

&lt;p&gt;Another major difference between &lt;code&gt;let&lt;/code&gt;/ &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;var&lt;/code&gt; is their behaviour relating to hoisting.&lt;/p&gt;

&lt;p&gt;Before we dive into these differences, let’s first discuss what hoisting actually is. To do so we must take a look at the 2 phases of the JavaScript execution context.&lt;/p&gt;

&lt;p&gt;When a program is passed to a JavaScript engine there are 2 phases which will occur. JavaScript will first parse the file in what it known as the &lt;strong&gt;creation phase&lt;/strong&gt;. During this phase, JavaScript will allocate memory for all variables and functions, amongst other processes such as creating the execution context and the scope chain.  &lt;/p&gt;

&lt;p&gt;Next comes the &lt;strong&gt;execution phase&lt;/strong&gt; during which, as the name suggests, the JavaScript engine actually executes the code in the file. &lt;/p&gt;

&lt;p&gt;Hoisting happens during the &lt;strong&gt;creation phase&lt;/strong&gt;, and it is the process of JavaScript identifying any variables and functions in a file and “hoisting” them to the top of their respective scope so that they are available for use during the execution phase. However, as we have already discovered, not all variables are created equal, so how does the behaviour of &lt;code&gt;let&lt;/code&gt;/&lt;code&gt;const&lt;/code&gt; differ from &lt;code&gt;var&lt;/code&gt;? Let’s take a look.&lt;/p&gt;

&lt;h4&gt;
  
  
  Var
&lt;/h4&gt;

&lt;p&gt;Any variable that has been declared with &lt;code&gt;var&lt;/code&gt; will be partially hoisted. The identifier name will be stored in memory but the value will be set to &lt;code&gt;undefined&lt;/code&gt; until JavaScript reaches the assignment of the value during the execution phase.&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="nx"&gt;myVar&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;myVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I used to be undefined&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;myVar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// undefined | first console.log()&lt;/span&gt;
&lt;span class="c1"&gt;// "I used to be undefined" | second console.log()&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;No errors were thrown, but the value of &lt;code&gt;myVar&lt;/code&gt; was set to &lt;code&gt;undefined&lt;/code&gt; until the execution phase during which JavaScript assigns the value as the string ‘I used to be undefined’. &lt;/p&gt;

&lt;h4&gt;
  
  
  Let and Const
&lt;/h4&gt;

&lt;p&gt;Variables declared with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; will be hoisted, but their values will be set to &lt;code&gt;uninitialised&lt;/code&gt;. This is different from &lt;code&gt;var&lt;/code&gt; as any values declared by var will initially be stored as &lt;code&gt;undefined&lt;/code&gt; until the execution phase. &lt;/p&gt;

&lt;p&gt;Let’s take a closer look at using let:&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="nx"&gt;myLet&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;myLet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I used to be uninitialised&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;myLet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt; &lt;/span&gt;
&lt;span class="c1"&gt;// "error" &lt;/span&gt;
&lt;span class="c1"&gt;// "ReferenceError: Cannot access 'myLet' before initialization&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The reference error message we received confirms this; &lt;code&gt;Cannot access 'myLet' before intialization&lt;/code&gt;. So, JavaScript is aware of its existence, but it has not yet been initialised with any value.  Practically speaking, what this means is that you should &lt;strong&gt;declare and assign&lt;/strong&gt; the value when using &lt;code&gt;let&lt;/code&gt; &lt;strong&gt;before&lt;/strong&gt; you try and access it in your code - unless you want to receive an error that is!&lt;/p&gt;

&lt;p&gt;That being said, some potentially unexpected behaviour occurs when we: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;declare a variable with &lt;code&gt;let&lt;/code&gt; but don’t assign it a value&lt;/li&gt;
&lt;li&gt;access that variable&lt;/li&gt;
&lt;li&gt;then assign it a value&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That sounds a bit confusing, so let me show you what I mean with an 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;let&lt;/span&gt; &lt;span class="nx"&gt;partiallyHoisted&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// declare a variable using let but don't manually assign it a value&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;partiallyHoisted&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// access the variable &lt;/span&gt;

&lt;span class="nx"&gt;partiallyHoisted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I used to be undefined&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// assign a value to the variable&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;partiallyHoisted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// undefined | first console.log()&lt;/span&gt;
&lt;span class="c1"&gt;// "I used to be undefined" | second console.log() after assigning the variable a value&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;No errors! In this case, although we tried to access &lt;code&gt;partiallyHoisted&lt;/code&gt; before we assigned it a value, as we had declared it (and therefore JavaScript “knew” it existed) it is set to &lt;code&gt;undefined&lt;/code&gt; by default, until we manually assign it a value.&lt;/p&gt;

&lt;p&gt;This behaviour differs between &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;. When using &lt;code&gt;const&lt;/code&gt; we cannot declare a variable without assigning it a value, and if you try you will receive a syntax error:&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;myConst&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;myConst&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;myConst&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This will cause an error!&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;myConst&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt; &lt;/span&gt;
&lt;span class="c1"&gt;// "error"&lt;/span&gt;
&lt;span class="c1"&gt;// "SyntaxError: Missing initializer in const declaration"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Aside from the example we just looked at, &lt;code&gt;const&lt;/code&gt; behaves the same way as &lt;code&gt;let&lt;/code&gt; when it comes to hoisting in JavaScript. The identifier name will be hoisted to the top of its scope, but its value will be uninitialised and therefore inaccessible until JavaScript reaches the line of code which assigns it a value during the execution phase.&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="nx"&gt;myConst&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;myConst&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I will be uninitialised &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;myConst&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// "error"&lt;/span&gt;
&lt;span class="c1"&gt;// "ReferenceError: Cannot access 'myConst' before initialization"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;If we take a look at the error message we receive, we see that it’s the same error we got when trying to access a variable declared with &lt;code&gt;let&lt;/code&gt; before assigning it a value. It cannot be accessed as it’s not yet been initialised. There’s a term for this specific period during which &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variables have been hoisted (so JavaScript “knows” they exist) but not initialised so are therefore inaccessible; the &lt;strong&gt;Temporal Dead Zone&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As a final note, although you will continue to see the use of the &lt;code&gt;var&lt;/code&gt; keyword in JavaScript code, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are nowadays considered standard, with &lt;code&gt;const&lt;/code&gt; usually being used to store objects and arrays even if you plan on mutating them later on in the program. &lt;/p&gt;

&lt;p&gt;If you made it this far, thanks for reading! &lt;/p&gt;

</description>
      <category>ci</category>
      <category>cicd</category>
      <category>java</category>
      <category>docker</category>
    </item>
    <item>
      <title>The Coolest JavaScript Features from the Last 5 Years</title>
      <dc:creator>Pippa Thompson</dc:creator>
      <pubDate>Mon, 27 Feb 2023 21:28:31 +0000</pubDate>
      <link>https://dev.to/ppiippaa/some-cool-javascript-features-from-the-last-5-years-4alp</link>
      <guid>https://dev.to/ppiippaa/some-cool-javascript-features-from-the-last-5-years-4alp</guid>
      <description>&lt;p&gt;According to Erik Qualman, language is always evolving. Although he was referring to natural language, the same applies to programming languages too. JavaScript has changed a lot since it’s conception in 1995, and since then many new features have been added. This article discusses some super useful (and possibly less well known) features added to JavaScript in the last 5 years. It in by no means an exhaustive list, and major revisions such as features surrounding classes will be discussed in a separate article. &lt;/p&gt;

&lt;h4&gt;
  
  
  A quick note on ECMAScript
&lt;/h4&gt;

&lt;p&gt;ECMA (European Computer Manufacturers Association) is an organisation tasked with providing specifications and standards for programming languages, hardware and communications. ECMAScript is a part of this organisation which focuses specifically on scripting languages. In other words, it provides a “blueprint” for how a scripting language, such as JavaScript, should behave. JavaScript implements these specifications, and as ECMAScript evolves so too does JavaScript. In order for a new feature to be implemented there is a 4 step process presided over by the TC39 committee. &lt;/p&gt;

&lt;p&gt;Due to the modernity of the features discussed in this article, some of them may not be supported by all browsers. To check browser compatibility, take a look at &lt;a href="https://caniuse.com/" rel="noopener noreferrer"&gt;this link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now with all that out of the way, let's dive into the features.&lt;/p&gt;

&lt;h3&gt;
  
  
  String.padStart() and String.padEnd()
&lt;/h3&gt;

&lt;p&gt;These string methods are a quick and easy way to attach strings onto other strings. As the names suggest, &lt;code&gt;String.padStart()&lt;/code&gt; adds a new string onto the start of a given string and &lt;code&gt;String.padEnd()&lt;/code&gt; appends a string onto the end of a given string. These methods do not mutate the original string.&lt;/p&gt;

&lt;h4&gt;
  
  
  String.padStart(desiredStringLength, stringToAdd)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;desiredStringLength&lt;/strong&gt;: How long you want the new string length to be as a number.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;stringToAdd&lt;/strong&gt;: this is the string you want to add to the beginning of the original string.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's take a look at an 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;let&lt;/span&gt; &lt;span class="nx"&gt;originalString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Script&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;paddedString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;originalString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Java&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;paddedString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// 'JavaScript'&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;What happens if the &lt;strong&gt;desiredStringLength&lt;/strong&gt; argument is &lt;strong&gt;shorter&lt;/strong&gt; than the length of the &lt;strong&gt;original string + the stringToAdd&lt;/strong&gt;? In this case, the stringToAdd is truncated &lt;strong&gt;before&lt;/strong&gt; it's added to the original string:&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;let&lt;/span&gt; &lt;span class="nx"&gt;originalString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Script&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;paddedString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;originalString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;padStart&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Java&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;paddedString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// 'JScript'&lt;/span&gt;
&lt;span class="c1"&gt;// truncates the stringToAdd from 'Java' to 'J'&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;What about it the &lt;strong&gt;desiredStringLength&lt;/strong&gt; argument is &lt;strong&gt;longer&lt;/strong&gt; than the length of the &lt;strong&gt;original string + stringToAdd&lt;/strong&gt;? This can lead to some weird looking results! The stringToAdd argument will be &lt;strong&gt;repeated&lt;/strong&gt; until it is equal to the desiredStringLength argument:&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;let&lt;/span&gt; &lt;span class="nx"&gt;originalString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Script&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;paddedString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;originalString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Java&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;paddedString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// 'JavaJavaJScript'&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;And if &lt;strong&gt;no stringToAdd argument is provided&lt;/strong&gt;? Empty spaces will be added to the front of the original string until the string length is equal to desiredStringLength:&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;let&lt;/span&gt; &lt;span class="nx"&gt;originalString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Script&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;paddedString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;originalString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&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;paddedString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// "         Script"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;And finally, what about it &lt;strong&gt;no desiredStringLength argument is provided&lt;/strong&gt;? A copy of the original string is returned unchanged:&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;let&lt;/span&gt; &lt;span class="nx"&gt;originalString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Script&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;paddedString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;originalString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Java&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;paddedString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt; &lt;/span&gt;
&lt;span class="c1"&gt;// 'Script'&lt;/span&gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  String.padEnd(desiredStringLength, stringToAppend)
&lt;/h4&gt;

&lt;p&gt;This string method works in the same way as &lt;code&gt;String.padStart()&lt;/code&gt;, but appends a string to the end of a given string.&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;let&lt;/span&gt; &lt;span class="nx"&gt;originalString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Web&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;paddedString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;originalString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;padEnd&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dev&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;paddedString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// 'WebDev&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The same rules apply regarding argument usage:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;desiredStringLength &amp;lt; original string + stringToAppend?&lt;/strong&gt; The stringToAppend appended to the end of the original string will be truncated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;desiredStringLength &amp;gt; original string + stringToAppend?&lt;/strong&gt; The stringToAppend appended to the end of the original string will be repeated until the desiredStringLength is reached.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No stringToAppend argument passed?&lt;/strong&gt; Empty spaces will be appended to the original string until the desiredStringLength is reached.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No desiredStringLength argument passed?&lt;/strong&gt; A copy of the original string is returned unchanged.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  String.replaceAll(pattern, replacement)
&lt;/h3&gt;

&lt;p&gt;You may have come across &lt;code&gt;String.replace()&lt;/code&gt; before, which takes a pattern argument and a replacement argument and replaces the first instance of the matching pattern in a string. The pattern argument can be a string literal or a RegEx. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;String.replaceAll()&lt;/code&gt; takes it one step further and, as the name suggests, allows us to replace all instances of a specified pattern with a replacement string rather than just the first instance.&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;// Using String.replace() &lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;aString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;My name is Pippa. Pippa is my name.&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;replaceString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pippa&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Philippa&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;replaceString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// "My name is Philippa. Pippa is my name"&lt;/span&gt;
&lt;span class="c1"&gt;// only the first instance of 'Pippa' is replaced with 'Philippa'&lt;/span&gt;

&lt;span class="c1"&gt;// Using String.replaceAll() with regex&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt;  &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/Pippa/ig&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;anotherString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;My name is Pippa. Pippa is my name.&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;replaceAllString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;anotherString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replaceAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Philippa&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;replaceAllString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// "My name is Philippa. Philippa is my name."&lt;/span&gt;
&lt;span class="c1"&gt;// both instances of 'Pippa' and replaced by 'Philippa'&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Object.entries(), Object.keys(), Object.values() and Object.fromEntries()
&lt;/h3&gt;

&lt;p&gt;This set of methods are useful for transforming certain data  structures. Let's go through them starting with...&lt;/p&gt;

&lt;h4&gt;
  
  
  Object.entries(originalObject)
&lt;/h4&gt;

&lt;p&gt;This object method &lt;strong&gt;takes an object&lt;/strong&gt; and returns a new &lt;strong&gt;2-dimensional array&lt;/strong&gt; with each nested array containing the original object’s key and value as elements. Let me show you what I mean:&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;fruitObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;banana&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;yellow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;strawberry&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tangerine&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;orange&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;fruitArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fruitObject&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;fruitArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// [["banana", "yellow"], ["strawberry", "red"], ["tangerine", "orange"]]&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This can be a super helpful method to use when transforming data. Another use case would be to access a specific key-value pair in an object:&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;fruitObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;banana&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;yellow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;strawberry&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tangerine&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;orange&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;firstFruit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fruitObject&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="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;firstFruit&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// ['banana', 'yellow']&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In case you hadn’t heard, a lot of things in JavaScript are objects. So, we can even pass arrays and strings as arguments into the &lt;code&gt;Object.entries()&lt;/code&gt; method which will coerce them into objects. Let’s see what happens when we pass a string as an argument:&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;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stringAsArgument&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&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;stringAsArgument&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt; &lt;/span&gt;
&lt;span class="c1"&gt;// [["0", "H"], ["1", "e"], ["2", "l"], ["3", "l"], ["4", "o"]]&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Each character in the string is inserted into a separate array, and its index is set as the first element of the array. This behaviour also happens when you pass an array as an argument:&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;array&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;3&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;formattedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&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;formattedArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt; &lt;/span&gt;
&lt;span class="c1"&gt;// [["0", 1], ["1", 2], ["2", 3]]&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Note that for both of these cases the first element (the index) is a string. &lt;/p&gt;

&lt;h4&gt;
  
  
  Object.keys(anObject)
&lt;/h4&gt;

&lt;p&gt;This object method accepts an object as an argument and returns an array containing the object’s keys as elements.&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;programmingLangs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Brendan Eich&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;C&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dennis Ritchie&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Python&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Guido van Rossum&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;langs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;programmingLangs&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;langs&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// ["JavaScript", "C", "Python"]&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;What about if we try and pass a string as the argument? Let’s take a look:&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;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hallo&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;stringArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&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;stringArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// ["0", "1", "2", "3", "4"]&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this case the string is also coerced into an object. Each letter represents the value and its index represents the key, so we are left with an array containing the indices of each letter in the string. &lt;/p&gt;

&lt;h4&gt;
  
  
  Object.values(anObject)
&lt;/h4&gt;

&lt;p&gt;As you might expect, the &lt;code&gt;Object.values()&lt;/code&gt; method works similarly to the method we just discussed, but instead of returning out an object’s keys in an array it returns out an object’s values in an array. Let’s use the programmingLangs example we saw previously:&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;programmingLangs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Brendan Eich&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;C&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dennis Ritchie&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Python&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Guido van Rossum&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;creators&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;programmingLangs&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;creators&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// ["Brendan Eich", "Dennis Ritchie", "Guido van Rossum"]&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;As we saw in the previous case of &lt;code&gt;Object.entries()&lt;/code&gt; and &lt;code&gt;Object.keys()&lt;/code&gt;, we can pass in other data types such as a string.&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;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bonjour&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stringArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&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;stringArray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// ["B", "o", "n", "j", "o", "u", "r"]&lt;/span&gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Object.fromEntries(anIterable)
&lt;/h4&gt;

&lt;p&gt;Another super useful method for transforming data. You remember the &lt;code&gt;Object.entries()&lt;/code&gt; method we saw earlier that turns an object into a 2-dimensional array? Well, &lt;code&gt;Object.fromEntries()&lt;/code&gt; essentially does the opposite. It accepts an iterable as an argument, such as an array or a map, and returns an object. Let’s take a look:&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;arrayTranslations&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;french&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bonjour&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;spanish&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;buenos dias&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;czech&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dobry den&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;objectTranslations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromEntries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrayTranslations&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;objectTranslations&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt; &lt;/span&gt;
&lt;span class="cm"&gt;/* [object Object] {
  czech: "dobry den",
  french: "bonjour",
  spanish: "buenos dias"
} */&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;So our iterable, in this case the nested array stored as &lt;code&gt;translations&lt;/code&gt;, is iterated through and each subarray transformed into an object with element at index 0 as the key and element at index 1 as the value. Handy!&lt;/p&gt;

&lt;h3&gt;
  
  
  Array.flat(optionalDepthArgument)
&lt;/h3&gt;

&lt;p&gt;Useful when it comes to dealing with multi-dimensional arrays, the array method &lt;code&gt;.flat()&lt;/code&gt; takes a given array and returns a flat array (1-dimensional by default) or an array of a specified depth (when the optionalDepthArgument is provided). &lt;/p&gt;

&lt;p&gt;When no optionalDepthArgument is provided, the default depth is 1:&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;numArray&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="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;4&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;flatArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flat&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;flatArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// [1, 2, 3, 4]&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The following is an example where an optionalDepthArgument has been passed into the method:&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;numArray&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="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;4&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;depthOf2Array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flat&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="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;depthOf2Array&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// [1, 2, [3, 4]];&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the above snippet, we specified an optionalDepthArgument of 2 so our new array &lt;code&gt;depthOf2Array&lt;/code&gt; is array with 2 ‘levels’. If you have a heavily nested array and want to return a one-dimensional array but you aren’t sure of the depth of the array, you can pass in the argument &lt;code&gt;Infinity&lt;/code&gt; like so:&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;nestedArray&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="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;4&lt;/span&gt;&lt;span class="p"&gt;,&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="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;7&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;oneDimensionalArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nestedArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;Infinity&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;oneDimensionalArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// [1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Object Spread Operator &lt;code&gt;...&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;You may have seen the spread syntax (…) in JavaScript before, however up until recently we could only use it with arrays. It’s a great method for cloning and merging arrays. As of ES2018, we can now harness this power to use with objects. The use cases of the object spread operator are the same as with the array spread operator; cloning and merging. &lt;/p&gt;

&lt;p&gt;Let’s first take a look at how we’d use this operator to clone an object and add extra key-value pairs:&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;bookAndAuthor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Gabriel Garcia Marquez&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;100 Years of Solitude&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;moreBooksAndAuthors&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;bookAndAuthor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Paolo Coelho&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;The Alchemist&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;moreBooksAndAuthors&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="cm"&gt;/* [object Object] {
  Gabriel Garcia Marquez: "100 Years of Solitude",
  Paolo Coelho: "The Alchemist"
}
*/&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here we’ve been able to very easily clone the original object and add more properties. &lt;/p&gt;

&lt;p&gt;Importantly, using the object spread operator to copy an &lt;strong&gt;object with no nested data&lt;/strong&gt; will create a &lt;strong&gt;new object in memory&lt;/strong&gt;. This means that we can clone an object, change or add properties to the newly created object, but &lt;strong&gt;the original object will not be mutated&lt;/strong&gt;. However, if we clone an object that &lt;strong&gt;contains nested data&lt;/strong&gt;, the clone will be stored in a new place in memory, but &lt;strong&gt;the nested data will be passed by reference&lt;/strong&gt;. This means that if we were to make changes to any nested data in one object, &lt;strong&gt;the same nested data in the second object would also change&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now, let's see what happens when we use the object spread operator to merge objects:&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;book1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Milan Kundera&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;The Unbearable Lightness of Being&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;book2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bohumil Hrabal&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I Served the King of England&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;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;book1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;book2&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;books&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt; &lt;/span&gt;
&lt;span class="cm"&gt;/*
  [object Object] {
  Bohumil Hrabal: "I Served the King of England",
  Milan Kundera: "The Unbearable Lightness of Being"
}
*/&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here we have merged two object to create a new object. How easy was that?&lt;/p&gt;

&lt;h3&gt;
  
  
  Promise.finally() and Promise.allSettled()
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Promise.finally(callbackFunction)
&lt;/h4&gt;

&lt;p&gt;Promises in JavaScript are nothing new and have been around since ES6, but the &lt;code&gt;Promise.finally()&lt;/code&gt; method is a newer addition to the async JavaScript toolbox. This method takes a callback function which is executed once the promise is settled, i.e. resolved or rejected. It is a good place to run code that relates to any clean up tasks.&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&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;num&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.5&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;promise resolved&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;promise rejected&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;value&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;value&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="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;error&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;finally&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;promise has been settled&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// 'promise resolved' / 'promise rejected' (depending on value of num)&lt;/span&gt;
&lt;span class="c1"&gt;// 'promise has been settled'&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Regardless of whether the promise is resolved or rejected, the callback function in the &lt;code&gt;Promise.finally()&lt;/code&gt; method will always be executed.&lt;/p&gt;

&lt;h4&gt;
  
  
  Promise.allSettled([promises])
&lt;/h4&gt;

&lt;p&gt;An ES2020 addition to JavaScript, the Promise.allSettled() method accepts an array of promises and returns a new promise which only resolves once all promises in the array have been settled (resolved or rejected). Once resolved, the return value will be an array of objects, with each object describing the outcomes of the promises passed in the array.&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;promise1&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="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;I have been resolved&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise2&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="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;I have been rejected&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;allSettled&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;promise1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;promise2&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;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="c1"&gt;// OUTPUT --&amp;gt; &lt;/span&gt;
&lt;span class="cm"&gt;/*
[[object Object] {
  status: "fulfilled",
  value: "I have been resolved"
}, [object Object] {
  reason: "I have been rejected",
  status: "rejected"
}]
*/&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this example, on line 9 we declare &lt;code&gt;Promise.allSettled()&lt;/code&gt; and we pass this method an array containing &lt;code&gt;promise1&lt;/code&gt; and &lt;code&gt;promise2&lt;/code&gt;. On line 10, we chain a &lt;code&gt;.then()&lt;/code&gt; method to &lt;code&gt;Promise.allSettled()&lt;/code&gt; which instructs JavaScript to print out the resolved value of &lt;code&gt;Promise.allSettled()&lt;/code&gt;. The output shows an array of objects has been returned. Each object represents the outcome of the promises passed as arguments to &lt;code&gt;Promise.resolve()&lt;/code&gt; (in our case, &lt;code&gt;promise1&lt;/code&gt; and &lt;code&gt;promise2&lt;/code&gt;). These objects have 2 properties; status, which evaluates to either fulfilled or rejected, and value, which evaluates to either rejected if the promise has been rejected, or the resolved value if the promise has been resolved. &lt;/p&gt;

&lt;h3&gt;
  
  
  BigInt
&lt;/h3&gt;

&lt;p&gt;This handy data type is used to store integer values in a variable which are too large to be stored as a Number data type. You may or may not know that the JavaScript Number data type has limits on the size of integers it can store - the safe range is from -9007199254740991 -(253-1) up to 9007199254740991 +(253-1), so 15 digits. The introduction of this data type (which has a typeof value of “bigint”) allows us to work more easily with integers outside this range. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A few notes about using BigInt:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arithmetic operators which work with the Number data type in JavaScript can also be used with BigInt, such as +, *, / etc. &lt;/li&gt;
&lt;li&gt;BigInt &lt;strong&gt;cannot&lt;/strong&gt; be used with decimals.&lt;/li&gt;
&lt;li&gt;You cannot perform arithmetic operations between a BigInt data type and a Number data type. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are 2 ways to declare a variable as a BigInt data type, using &lt;code&gt;BigInt(number)&lt;/code&gt; or appending &lt;code&gt;n&lt;/code&gt; to a number:&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;let&lt;/span&gt; &lt;span class="nx"&gt;hugeNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BigInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9999999999999999&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;anotherHugeNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7777777777777777&lt;/span&gt;&lt;span class="nx"&gt;n&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="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;hugeNumber&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="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;anotherHugeNumber&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// 'bigint'&lt;/span&gt;
&lt;span class="c1"&gt;// 'bigint'&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Nullish Coalescing Operator and Optional Chaining
&lt;/h3&gt;

&lt;p&gt;Arguably my most used features from this list. &lt;/p&gt;

&lt;h4&gt;
  
  
  Nullish Coalescing (&lt;code&gt;??&lt;/code&gt;)
&lt;/h4&gt;

&lt;p&gt;This logical operator takes two operands. If the left-hand side operand is &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt; it will return the right-hand side operand. On the flip side, if the left-hand side operand is not &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;, it will return the left-hand side operand. &lt;/p&gt;

&lt;p&gt;It’s similar to the logical Or operator (&lt;code&gt;||&lt;/code&gt;), except the &lt;code&gt;||&lt;/code&gt; operator returns the right-hand side based on whether the left-hand side is a falsy value (as opposed to just &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;). There will be some behavioural overlap between &lt;code&gt;??&lt;/code&gt; and &lt;code&gt;||&lt;/code&gt; as falsy values in JavaScript include &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt;, but also &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;“”&lt;/code&gt; (empty string), &lt;code&gt;Nan&lt;/code&gt; and, of course, &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's see this in action:&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;usingOr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Return me because undefined is a falsy 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;usingOrAgain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Return me because I am NOT falsy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I will not be returned&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;


&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;usingNullishCoalescing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Return me!&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;usingNullishCoalescingAgain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I will return because I am NOT null/undefined &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I will not be returned&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;usingOr&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;usingOrAgain&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;usingNullishCoalescing&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;usingNullishCoalescingAgain&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// "Return me because undefined is a falsy value"&lt;/span&gt;
&lt;span class="c1"&gt;// "Return me because I am NOT falsy"&lt;/span&gt;
&lt;span class="c1"&gt;// "Return me!"&lt;/span&gt;
&lt;span class="c1"&gt;// "I will return because I am NOT null/undefined " &lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In terms of operator precedence, the Nullish Coalescing operator has the 5th lowest precedence, so bear this in  mind when combing multiple operators. For more in-depth information about operator precedence you can &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence" rel="noopener noreferrer"&gt;check out this page&lt;/a&gt;. &lt;/p&gt;

&lt;h4&gt;
  
  
  Optional Chaining (&lt;code&gt;?.&lt;/code&gt;)
&lt;/h4&gt;

&lt;p&gt;This operator is used when accessing properties or methods of an object. It helps us avoid throwing errors if a property/method does not exist. Rather than an error, we receive &lt;code&gt;undefined&lt;/code&gt; if no corresponding property/method is found. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For use with object properties we access the property with &lt;code&gt;Object?.property&lt;/code&gt; rather than just &lt;code&gt;Object.property&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For use with object methods we invoke the method with &lt;code&gt;Object.method?.()&lt;/code&gt; rather than just &lt;code&gt;Object.method()&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&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;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pippa&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;favouriteColour&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;green&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

  &lt;span class="nf"&gt;sayHello&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="s2"&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="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; says hello`&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;// ?. with object properties &lt;/span&gt;
&lt;span class="kd"&gt;const&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;person&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;favouriteColour&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;age&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;color&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;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// ?. with object methods&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;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayHello&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;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayGoodby&lt;/span&gt;&lt;span class="p"&gt;?.());&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt; &lt;/span&gt;
&lt;span class="c1"&gt;// "green"&lt;/span&gt;
&lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="c1"&gt;// "Pippa says hello"&lt;/span&gt;
&lt;span class="c1"&gt;// undefined&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;As you can see, both &lt;code&gt;person.age&lt;/code&gt; and &lt;code&gt;person.sayGoodbye()&lt;/code&gt; do not exist on the person object, but instead of receiving an error we get undefined returned to us. &lt;/p&gt;

&lt;h3&gt;
  
  
  Numeric Separator(&lt;code&gt;_&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;Let’s finish this list with an easy one. Numeric separators were introduced into JavaScript for readability when working with larger numbers. They allow you to ‘break down’ numbers into more easily digestible chunks, exactly like you would when using a comma (,) or point (.). We can separate larger numbers using the &lt;code&gt;_&lt;/code&gt; character (underscore).&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;harderToReadNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100000000&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;easierToReadNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="nx"&gt;_000_000&lt;/span&gt;

&lt;span class="c1"&gt;// how much nicer is that to read?!&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;If you made it this far, thanks for reading! I hope that you’ve learnt something which will serve you well in future JavaScript projects.  &lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>All you need to know about asynchronous JavaScript</title>
      <dc:creator>Pippa Thompson</dc:creator>
      <pubDate>Thu, 23 Feb 2023 17:10:19 +0000</pubDate>
      <link>https://dev.to/ppiippaa/all-you-need-to-know-about-asynchronous-javascript-40ma</link>
      <guid>https://dev.to/ppiippaa/all-you-need-to-know-about-asynchronous-javascript-40ma</guid>
      <description>&lt;p&gt;If you have ever spent time in the JavaScript world you will probably have heard the phrase "JavaScript is a single threaded language". But what does that mean exactly? Put simply, it means that JavaScript executes code line by line in sequential order, and after each line has been executed there's not going back to it.&lt;/p&gt;

&lt;h3&gt;
  
  
  So what's the problem?
&lt;/h3&gt;

&lt;p&gt;Imagine a situation where a user wants to display a list of comments on a website. They click a button to view the comments. However, while the comments are being fetched from a server (and let's say that the fetching takes a while), the user cannot interact with anything else on the website because JavaScript is busy executing the show comments functionality. In other words, all other code is &lt;strong&gt;blocked&lt;/strong&gt; until JavaScript has completed this comment fetching task. Not very user friendly.&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Pippa&lt;/span&gt;&lt;span class="err"&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;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="nx"&gt;_000_000&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;let&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;number&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="nx"&gt;i&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;`Hi, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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 trivial example, a time consuming command (in this case, iterating through a large for loop) would block the final &lt;code&gt;console.log()&lt;/code&gt; command for an undetermined amount of time. Nothing else can happen in the program while the for loop is running, and who knows how long that could take to complete.&lt;/p&gt;

&lt;h4&gt;
  
  
  The solution: enter asynchronous JavaScript.
&lt;/h4&gt;

&lt;p&gt;Async JS is a technique (or rather, group of techniques) which enables your program to continue executing code while time consuming tasks are deferred. No, it's not magic, it is just a set of features of both JavaScript and the browser which when combined allow us to write &lt;strong&gt;non-blocking code&lt;/strong&gt;. Let's start with the first, and probably most simple, method.&lt;/p&gt;

&lt;h3&gt;
  
  
  Callbacks: using the &lt;code&gt;setTimeout()&lt;/code&gt; API
&lt;/h3&gt;

&lt;p&gt;If you have read up on the JavaScript runtime environment (and if you've haven't you can &lt;a href="https://dev.to/ppiippaa/javascript-runtime-explained-all-you-need-to-know-about-client-side-js-code-execution-5g8e"&gt;do so here&lt;/a&gt;) this one should come as no surprise. The browser provides the event loop, the callback queue and the microtask queue. These features allow certain functionality to be deferred until all global code has been executed. &lt;code&gt;setTimeout()&lt;/code&gt; (a browser API) takes a callback function and a time argument (&lt;strong&gt;minimum time&lt;/strong&gt; in milliseconds to pass before invoking the callback) and pushes the callback function to the callback queue. The callback queue must wait for all other global code to run before anything in it can be pushed to the call stack (this is handled by the event loop), thus we can essentially call this function knowing that our global code will continue executing in the meantime.&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="s1"&gt;first hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&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="c1"&gt;// functionality to make XHR request to fetch data from a server and log it to the console&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="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;fetchData&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="s1"&gt;second hello &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt; &lt;/span&gt;
&lt;span class="c1"&gt;// 'first hello '&lt;/span&gt;
&lt;span class="c1"&gt;// 'second hello'&lt;/span&gt;
&lt;span class="c1"&gt;// '[ object Response ] {...}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example we start with &lt;code&gt;console.log('first hello')&lt;/code&gt;. We then declare the function &lt;code&gt;fetchData()&lt;/code&gt;. This function emulates making an XML/HTTP request, which is wrapped in the &lt;code&gt;setTimeout()&lt;/code&gt; browser API. Once we call the &lt;code&gt;fetchData()&lt;/code&gt; function, the &lt;code&gt;setTimeout()&lt;/code&gt; starts a timer up in the browser with a time of 1000ms. The callback function wrapped inside the &lt;code&gt;setTimeout()&lt;/code&gt; is sent to the callback queue where it will be waiting for &lt;em&gt;at least 1000ms&lt;/em&gt;. Meanwhile JavaScript continues on to the second &lt;code&gt;console.log('second hello')&lt;/code&gt;. Once the second &lt;code&gt;console.log()&lt;/code&gt; is logged to the console and the callstack is clear of all global code, the &lt;code&gt;fetchData()&lt;/code&gt; callback function is able to give us the response it received from the server. No code has been blocked and our data has been fetched! &lt;/p&gt;

&lt;h3&gt;
  
  
  Promises and the &lt;code&gt;fetch()&lt;/code&gt; API
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;In this section we will look at promises in the context of using the &lt;code&gt;fetch()&lt;/code&gt; API. It’s worth noting, however, this is not the only use case for promises. There's loads of great resources on promises out there such as &lt;a href="https://javascript.info/promise-basics" rel="noopener noreferrer"&gt;this article&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;With ES6 came the introduction of promises. In order to understand how they facilitate asynchronous code we need to take a closer look at their features and behaviour. Promises are just special objects that return a placeholder object until some asynchronous code has been resolved. Every promise has several properties including state, value, onFulfilled and onRejected. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;State:&lt;/strong&gt; represents current status of the promise object which can be one of three options: &lt;strong&gt;pending&lt;/strong&gt; (the default), &lt;strong&gt;resolved&lt;/strong&gt; or &lt;strong&gt;rejected.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Value:&lt;/strong&gt; this is undefined by default, but will eventually evaluate to the response of the asynchronous task.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;onFulfilled:&lt;/strong&gt; an array containing functions to be run once the value property has been successfully resolved. We push functions into this array by using the &lt;code&gt;.then()&lt;/code&gt; method. The callbacks in this array receive an argument which is the response of the &lt;code&gt;fetch()&lt;/code&gt; call they are chained on to.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;onRejected:&lt;/strong&gt; an array containing functions to run if the promise is rejected, so essentially error handling functions. We push functions into this array by using the &lt;code&gt;.catch()&lt;/code&gt; method. The callbacks in this array receive an argument which is the error received in the &lt;code&gt;fetch()&lt;/code&gt; call they are chained on to.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Promises allowed for the creation of the modern XHR browser function known as &lt;code&gt;fetch()&lt;/code&gt;. I would like to stress that that &lt;code&gt;fetch()&lt;/code&gt; is a &lt;strong&gt;a feature of the browser&lt;/strong&gt;, so it is not native to JavaScript even though it looks like a regular function. Once invoked, fetch has two main tasks; one is to return a placeholder object (a promise) and the other is to send an XML/HTTP request from the browser to a specified url.&lt;/p&gt;

&lt;p&gt;Here’s the big reveal - any promise deferred functionality is sent to the microtask queue (separate from the callback queue, which has lower priority). The functionality in the microtask queue must wait until all global code has been executed until it can be pushed to the call stack. Thus, we can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run an asynchronous operation, i.e. fetching data from a server.&lt;/li&gt;
&lt;li&gt;Execute all other global code meanwhile.&lt;/li&gt;
&lt;li&gt;Trigger response related functionality from the onFulfilled array once all global code has been run and we’ve received a response from an asynchronous operation. Alternatively, if the promise rejects we can trigger error handling functionality from the onRejected array.
&lt;/li&gt;
&lt;/ul&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="s1"&gt;me first&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://dog.ceo/api/breeds/image/random&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;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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;data&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;data&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="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;error&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;fetchData&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="s1"&gt;me second&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt; &lt;/span&gt;
&lt;span class="c1"&gt;// 'me first'&lt;/span&gt;
&lt;span class="c1"&gt;// 'me second'&lt;/span&gt;
&lt;span class="c1"&gt;// '[ object Object ] {...}'&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;And there you have it, asynchronous code which allows a more time intensive command to be run (in this case, the XHR functionality) without blocking the rest of the JavaScript code in the file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Side note&lt;/strong&gt;: there is also another promise array, similar to onFulfilled/onRejected, which allows us to run callbacks once the promise has been settled (either resolved or rejected). We push callbacks into this array using the &lt;code&gt;.finally()&lt;/code&gt; method and it's useful for performing any clean up tasks.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Generator Functions and Async/Await
&lt;/h3&gt;

&lt;p&gt;Generator functions are an ES6 feature of JavaScript. Up until now, we understand that once a line of JavaScript code has been executed it is gone for good - there’s no going back up to that line. However, the introduction of generator functions has changed this model. Generators are special types of functions marked with an asterisk. They use a very powerful keyword, &lt;code&gt;yield&lt;/code&gt;. I like to think of the &lt;code&gt;yield&lt;/code&gt; keyword as the &lt;code&gt;return&lt;/code&gt; keyword with superpowers. &lt;code&gt;Yield&lt;/code&gt; allows us to pause a generator function’s execution context, return out a value, but later re-enter that same function execution context! We re-enter the generator funtion's execution context by accessing the &lt;code&gt;.next()&lt;/code&gt; method on the generator function. This can be quite tricky to wrap your head around, so let me show you an 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;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;myNumberGenerator&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;3&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;returnNextNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;myNumberGenerator&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;number1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;returnNextNumber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;value&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;number2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;returnNextNumber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;value&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;number3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;returnNextNumber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;value&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;number1&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;number2&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;number3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="c1"&gt;// 3&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;On line 9 we are assigning the value of &lt;code&gt;number1&lt;/code&gt; to &lt;code&gt;returnNextNumber.next().value&lt;/code&gt;. We established that &lt;code&gt;.next()&lt;/code&gt; allows us to re-enter the function execution context, but we have not yet seen the &lt;code&gt;.value&lt;/code&gt; syntax. &lt;br&gt;
When we are kicked out of the generator function by the &lt;code&gt;yield&lt;/code&gt; keyword, we know that we also receive a return value. However, what we actually get is an object with two properties; done (a boolean) and the value itself (in this case a number). In order to access just the value itself, we must use &lt;code&gt;.value&lt;/code&gt;. &lt;br&gt;
On line 10, we assign the variable &lt;code&gt;number2&lt;/code&gt; to the result of calling &lt;code&gt;returnNextNumber.next().value&lt;/code&gt;, so &lt;code&gt;number2&lt;/code&gt; evaluates to 2. This is because we re-entered the generator function where we left off and once more encounter the &lt;code&gt;yield&lt;/code&gt; keyword, this time with the value 2. The same pattern continues on line 11.&lt;/p&gt;

&lt;p&gt;We know that once a normal function has been invoked, its execution context is automatically garbage collected, but here we are able to go back into the function and continue running it later on in the code. Cool, right? Let’s take a look at another 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;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;aGeneratorFunc&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;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;newNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;yield&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;yield&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;newNumber&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;20&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;returnNextElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;aGeneratorFunc&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;element1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;returnNextElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 10&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;returnNextElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 12&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt; &lt;/span&gt;
&lt;span class="c1"&gt;// 10&lt;/span&gt;
&lt;span class="c1"&gt;// 12&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Wait, what… the number 12 was returned and not 20? Why? Because not only can we re-enter a function, we can re-enter the function and bring some new data with us. &lt;/p&gt;

&lt;p&gt;The first time we call &lt;code&gt;returnNextElement.next().value&lt;/code&gt; on line 9, &lt;code&gt;yield number&lt;/code&gt; (line 3) threw us out of the function &lt;strong&gt;before&lt;/strong&gt; it had time to assign the value of &lt;code&gt;number&lt;/code&gt; to the variable &lt;code&gt;newNumber&lt;/code&gt;. So, at this point &lt;code&gt;newNumber&lt;/code&gt; is undefined. The next time we call &lt;code&gt;returnNextElement.next(2).value&lt;/code&gt; (line 10) with 2 as an argument, we re-enter the function where we left off last time, i.e. on line 3 where we were just about to assign the value of &lt;code&gt;newNumber&lt;/code&gt;. The argument we passed in, the number 2, is inserted where we left off, so &lt;code&gt;newNumber&lt;/code&gt; evaluates to 2. We move to the next line &lt;code&gt;yield 10 + newNumber&lt;/code&gt;, and we are thrown out of the function execution context along with the evaluation of &lt;code&gt;10 + newNumber&lt;/code&gt;, which as we now know is 2. Hence, we return out 12. If we were to re-enter the function once more and save &lt;code&gt;returnNextElement.next().value&lt;/code&gt; to the variable element3, the value of element3 would be 20. &lt;/p&gt;

&lt;p&gt;So now we understand how it’s possible to re-enter an execution context of a function, with additional data if we wish, &lt;strong&gt;but how does this link to async JS?&lt;/strong&gt; &lt;/p&gt;

&lt;h4&gt;
  
  
  Introducing the ES7 feature async/await.
&lt;/h4&gt;

&lt;p&gt;Async/Await is made possible due to generators (and promises). With this ability to re-enter a function later on in our code, we can start executing a more time consuming command (such as fetching data), continue on with the global code, and then re-enter the data-fetching function once the requested data has been returned and all global code has been run. &lt;/p&gt;

&lt;p&gt;Async/await has a clean and simple syntax which requires you to place the async keyword before the function definition. The await keyword is used within the function scope, and commands JavaScript to pause the function execution until the result of the promise has been returned and continue on with the rest of the program code. After the global code has been run, JavaScript will return back into the function where it left off, by this time with a received response, and continue with the rest of the logic. Again, let’s take a look at an 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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getData&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://jsonplaceholder.typicode.com/todos/1&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;data&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;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;getData&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="s1"&gt;me first!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT →&lt;/span&gt;
&lt;span class="c1"&gt;// 'me first!'&lt;/span&gt;
&lt;span class="c1"&gt;// [ object Promise ] {...}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Let’s break it down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We declare an async function using the async keyword.&lt;/li&gt;
&lt;li&gt;We use the await keyword to signal that a promise will be returned from the fetch call which enables us to break out of the function and return to it once the promise has been settled and global code has been run.&lt;/li&gt;
&lt;li&gt;We call the &lt;code&gt;getData()&lt;/code&gt; function, but the &lt;code&gt;console.log()&lt;/code&gt; runs first because the fetching of API data has been deferred to the microtask queue. &lt;/li&gt;
&lt;li&gt;JavaScript continues running the global code, then re-enters the &lt;code&gt;getData()&lt;/code&gt; function once all global code has run and the response is available in &lt;code&gt;getData()&lt;/code&gt;. It then continues executing the rest of the &lt;code&gt;getData()&lt;/code&gt; function. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;A note on error handling in async/await.&lt;/strong&gt; In order to facilitate error handling in an async function, you can wrap the functionality in a try/catch statement. This gives you the option to run some code upon promise failure, essentially the same as using the &lt;code&gt;.catch()&lt;/code&gt; method with &lt;code&gt;fetch()&lt;/code&gt;. Using the same example we just saw, let’s see what it looks like using a try/catch statement:&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;getData&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://jsonplaceholder.typicode.com/todos/1&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;data&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;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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;data&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="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="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;getData&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="s1"&gt;me first&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;span class="c1"&gt;// OUTPUT→&lt;/span&gt;
&lt;span class="c1"&gt;// 'me first!'&lt;/span&gt;
&lt;span class="c1"&gt;// [ object Promise ] {...}&lt;/span&gt;

&lt;span class="c1"&gt;// if an error occurs when fetching the data, the output would be:&lt;/span&gt;
&lt;span class="c1"&gt;// ‘me first!’&lt;/span&gt;
&lt;span class="c1"&gt;// [ object Error ] {...}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;And there you have it, asynchronous JavaScript. If you made it this far, thanks for reading! If you want to dive deeper into the topic there are loads of great resources out there, but I thoroughly recommend watching Will Sentence’s Hard Parts video courses available on Front End Masters. &lt;/p&gt;

</description>
      <category>web3</category>
      <category>blockchain</category>
      <category>privacy</category>
      <category>dataviz</category>
    </item>
    <item>
      <title>JavaScript Runtime Explained: All you need to know about client-side JS code execution</title>
      <dc:creator>Pippa Thompson</dc:creator>
      <pubDate>Mon, 13 Feb 2023 10:08:45 +0000</pubDate>
      <link>https://dev.to/ppiippaa/javascript-runtime-explained-all-you-need-to-know-about-client-side-js-code-execution-5g8e</link>
      <guid>https://dev.to/ppiippaa/javascript-runtime-explained-all-you-need-to-know-about-client-side-js-code-execution-5g8e</guid>
      <description>&lt;p&gt;If, like me, you struggled to understand the concept of the JavaScript runtime, this article may help shed some light on this particular topic. Put simply, the JavaScript runtime is the environment that executes your JavaScript code. Although JavaScript can now be run on the server side (Node.js/Deno), today we will focus on the client side - running JavaScript in the browser.&lt;/p&gt;

&lt;p&gt;The browser does not have the ability to understand JavaScript files. Instead, they must be parsed into executable commands. In order to do this, the browser uses a JavaScript engine, for instance Chrome uses the Google V8 engine. The specific engine depends on which browser you’re using. &lt;a href="https://www.geeksforgeeks.org/introduction-to-javascript-engines/" rel="noopener noreferrer"&gt;Here's a list of browser specific engines&lt;/a&gt;. &lt;br&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%2Fsrd2nnv3zryvlr8qblkp.jpg" 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%2Fsrd2nnv3zryvlr8qblkp.jpg" alt="Diagram of JS runtime" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Components of the JS runtime
&lt;/h2&gt;

&lt;p&gt;In the browser world, the JavaScript runtime environment is made up of the following: &lt;/p&gt;
&lt;h4&gt;
  
  
  Call Stack (part of the JS engine)
&lt;/h4&gt;

&lt;p&gt;The call stack makes up part of the JS engine. As the name suggests, it’s a stack data structure which keeps track of which functions are running. This is emblematic of the single threaded nature of JavaScript as, being a stack, &lt;strong&gt;the call stack can only handle one function at a time.&lt;/strong&gt; The call stack follows a &lt;em&gt;last-in first-out&lt;/em&gt; principle. In order to facilitate asynchronous code, callback and microtasks queues are implemented (see below). &lt;/p&gt;
&lt;h4&gt;
  
  
  Memory Heap &amp;amp; Memory Stack (part of the JS engine)
&lt;/h4&gt;

&lt;p&gt;Another piece of the JS engine is the memory heap/stack which is responsible for allocating space for any data in your JavaScript file. Depending on the type of data, the JS engine will allocate space in either the memory heap or the memory stack. The memory stack is reserved for static data, such as primitive values or references to objects/functions - data which the JavaScript engine knows the size of at compile time. On the other hand, the memory heap is where the objects and functions themselves are stored. At compile time, the JavaScript engine does not know the size of these pieces of data and will not find out until run time so the memory space allocation is more dynamic.&lt;/p&gt;
&lt;h4&gt;
  
  
  Web APIs (supplied by the browser)
&lt;/h4&gt;

&lt;p&gt;Web APIs are separate from the JS engine, and are provided by the browser. To quote Will Sentance, they are “facade functions” which look like JS functions and which can be interacted with in your code, but they are &lt;strong&gt;not native to the JavaScript language&lt;/strong&gt;. One example is the DOM API which allows you to interact with the HTML of the web page. Other examples include &lt;code&gt;setTimeout()&lt;/code&gt;, &lt;code&gt;fetch()&lt;/code&gt;, local storage interface and event listeners.&lt;/p&gt;
&lt;h4&gt;
  
  
  Callback Queue (supplied by the browser)
&lt;/h4&gt;

&lt;p&gt;The callback queue takes care of executing callback functions in the correct order by pushing them to the call stack when their time comes. It operates on a first-in first-out basis. An example could be timer related functionality such as &lt;code&gt;setTimeout()&lt;/code&gt; or &lt;code&gt;setInterval()&lt;/code&gt;. &lt;/p&gt;
&lt;h4&gt;
  
  
  Microtask Queue (supplied by the browser)
&lt;/h4&gt;

&lt;p&gt;Similarly, the microtask queue also deals with sending callbacks to the call stack. What’s the difference? The microtask queue handles callback functions coming from &lt;strong&gt;promises&lt;/strong&gt;, &lt;code&gt;queueMicrotask()&lt;/code&gt; invocations and &lt;strong&gt;mutation observers&lt;/strong&gt;. The microtask queue takes priority over the callback queue, so the callback queue will have to wait for the microtask queue to be empty before it can send anything to the call stack.&lt;/p&gt;
&lt;h4&gt;
  
  
  Event Loop (supplied by the browser)
&lt;/h4&gt;

&lt;p&gt;The event loop is the middle man between the callback/microtask queues and the call stack. It keeps track of what is in the callback/microtask queue and sends the relevant function to the call stack. As mentioned above, it &lt;strong&gt;prioritises the microtask queue&lt;/strong&gt; and will only send callback queue functionality to the call stack once the microtask queue is completely empty.&lt;/p&gt;
&lt;h4&gt;
  
  
  Now let's put it into practice...
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="s1"&gt;ahoj&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;0&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="s1"&gt;hola&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt; &lt;/span&gt;
&lt;span class="c1"&gt;// hola&lt;/span&gt;
&lt;span class="c1"&gt;// ahoj&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In the snippet above, the &lt;code&gt;console.log('hola')&lt;/code&gt; will be executed first  because the &lt;code&gt;setTimeout()&lt;/code&gt; function will be sent to the callback queue as it's utilising the timer from the Browser API. Even though the timer is set for 0 seconds, it waits for the call stack to be empty before triggering.&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="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="s1"&gt;ahoj&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;0&lt;/span&gt;&lt;span class="p"&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;hi&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="nx"&gt;val&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;val&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="s1"&gt;hola&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt; &lt;/span&gt;
&lt;span class="c1"&gt;// hola&lt;/span&gt;
&lt;span class="c1"&gt;// hi&lt;/span&gt;
&lt;span class="c1"&gt;// ahoj&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this second example, as with the above, the &lt;code&gt;console.log('hola')&lt;/code&gt; runs first. The promise comes second as this is sent to the microtask queue which takes precedence over the callback queue. Finally, the &lt;code&gt;setTimeout()&lt;/code&gt; callback runs after the microtask is clear and the callback queue functions are pushed to the call stack by the event loop. &lt;/p&gt;

&lt;p&gt;In the case of &lt;code&gt;fetch()&lt;/code&gt; and &lt;code&gt;setTimeout()&lt;/code&gt; you might get a different order to the one you were expecting. This is due to the fact that the microtask queue may be empty if it's still waiting for the response from the XHR request, meaning that a &lt;code&gt;setTimeout()&lt;/code&gt; with a timer set to 0ms could potentially run first.&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="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://dog.ceo/api/breeds/image/random&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;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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;data&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;data&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="s1"&gt;ahoj&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;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT --&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// ahoj&lt;/span&gt;
&lt;span class="c1"&gt;// { Response Object }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a great video summary of the topic&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/8aGhZQkoFbQ?start=55"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
