<?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: Adam Yeats</title>
    <description>The latest articles on DEV Community by Adam Yeats (@xadamy).</description>
    <link>https://dev.to/xadamy</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%2F172724%2Fa30461a1-fa70-4d3f-865f-e0601cd3faa3.jpeg</url>
      <title>DEV Community: Adam Yeats</title>
      <link>https://dev.to/xadamy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xadamy"/>
    <language>en</language>
    <item>
      <title>JavaScript Errors: An Exceptional History - Part II</title>
      <dc:creator>Adam Yeats</dc:creator>
      <pubDate>Tue, 26 Nov 2019 15:05:58 +0000</pubDate>
      <link>https://dev.to/appsignal/javascript-errors-an-exceptional-history-part-ii-2j03</link>
      <guid>https://dev.to/appsignal/javascript-errors-an-exceptional-history-part-ii-2j03</guid>
      <description>&lt;p&gt;Hello again! Welcome to the finalé of a two-part series of posts on errors in JavaScript.&lt;/p&gt;

&lt;p&gt;Last time, we took &lt;a href="https://blog.appsignal.com/2019/10/17/javascript-errors-an-exceptional-history.html"&gt;a look into the history of errors in JavaScript&lt;/a&gt; — how JavaScript shipped without runtime exceptions, how error handling mechanisms were later added both to the fledgeling web browsers of the day and to the ECMAScript spec, and how they future efforts to standardise these features would be connected to the politics of the browser wars of the late 90's and 2000's.&lt;/p&gt;

&lt;p&gt;This time, we'll focus a little more on the state of affairs in JavaScript today. We'll look at the different ways you can handle errors in your app today, the various idiosyncrasies they have, and how you can use our JavaScript client library to report errors from your app to our dashboard.&lt;/p&gt;

&lt;p&gt;Let's do it!&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling Errors Today
&lt;/h2&gt;

&lt;p&gt;After the last post, you may be forgiven for thinking that handling errors gracefully in JavaScript might be a bit of a nightmare. Luckily, it's not so daunting a prospect as it might seem, but there are quite a few different ways to handle errors with varying levels of scope and different use cases.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;window.onerror&lt;/code&gt; Handler
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;window.onerror&lt;/code&gt; handler exists today in all modern web browsers as a means to catch uncaught exceptions from the current &lt;code&gt;window&lt;/code&gt;. Any thrown error that is not otherwise handled in a &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt; block will be passed to the handler as the first argument to that function. The current &lt;code&gt;window&lt;/code&gt; refers to the current global context, so it's important to note that &lt;code&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt;s and Web Workers (for example) will have their own &lt;code&gt;window&lt;/code&gt; context.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When we use a &lt;code&gt;window&lt;/code&gt; in the following examples, we're referring to the global &lt;code&gt;window&lt;/code&gt; context of the browser.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By assigning a function to &lt;code&gt;window.onerror&lt;/code&gt;, we can write custom logic to handle any uncaught exceptions that are thrown during the lifecycle of our application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// NOTE: using typescript syntax here in order to show what types the arguments are&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lineno&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;colno&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;number&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="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// error handling code here!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onerror&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You might notice that some of these arguments are marked as optional. This is because, as you might guess, browsers disagree on the number of arguments passed to the &lt;code&gt;onError&lt;/code&gt; handler. Browsers as recent as Safari 9, for example, do not pass an &lt;code&gt;Error&lt;/code&gt; object as its fifth argument. Internet Explorer 9 passes neither the &lt;code&gt;colno&lt;/code&gt; or &lt;code&gt;error&lt;/code&gt; arguments. Because of this inconsistency, care needs to be taken when writing an &lt;code&gt;onError&lt;/code&gt; handler that works in older browsers.&lt;/p&gt;

&lt;p&gt;However, thanks to the existence of the &lt;code&gt;Error&lt;/code&gt; object in most modern browsers, you can normally rely on that 5th argument to be present, which will include some useful information that might come in handy when debugging, such as the current stack trace (&lt;code&gt;error.stack&lt;/code&gt;). &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As this handler tends to be a little noisy (thanks, browser extensions...), the AppSignal JavaScript library doesn't automatically catch exceptions passed to the &lt;code&gt;window.onerror&lt;/code&gt; handler. Instead, we have packaged this functionality in an optional plugin — the &lt;code&gt;@appsignal/plugin-window-events&lt;/code&gt; package on &lt;code&gt;npm&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As a convenience, once the &lt;code&gt;onError&lt;/code&gt; handler is called, most browsers will call &lt;code&gt;console.error&lt;/code&gt; behind the scenes to display the &lt;code&gt;Error&lt;/code&gt; object (often including its stacktrace) in the console.&lt;/p&gt;

&lt;p&gt;The Document Object Model Level 2 specification introduced the &lt;code&gt;EventTarget&lt;/code&gt; interface to provide a generic way to bind event listeners to an &lt;code&gt;Element&lt;/code&gt; (or other objects like &lt;code&gt;Document&lt;/code&gt; and &lt;code&gt;Window&lt;/code&gt;) that worked cross-browser, but also added features like the ability to have multiple handlers bound to an event. This means that many of the older event handlers, such as our friend &lt;code&gt;onError&lt;/code&gt;, received a modern facelift.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// error handling code here!&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this example, you can see that the &lt;code&gt;event&lt;/code&gt; of type &lt;code&gt;ErrorEvent&lt;/code&gt; is passed as the single argument to your callback. The &lt;code&gt;event&lt;/code&gt; object contains both the information about the error but also the event itself, but again, older browsers differ in the information they provide in the &lt;code&gt;event&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt; Operator
&lt;/h3&gt;

&lt;p&gt;For synchronous code, the humble &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt; operator remains the most common way to handle exceptions. As we discussed in the previous post, &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt; exception handling allows you to &lt;em&gt;try&lt;/em&gt; executing a block of code that may &lt;em&gt;throw&lt;/em&gt; errors at runtime; if it does, the exception is then &lt;em&gt;caught&lt;/em&gt; by the &lt;code&gt;catch&lt;/code&gt; block, allowing us to control what happens and what state our app is left in.&lt;/p&gt;

&lt;p&gt;While it's certainly true that JavaScript still allows you to throw any value as an exception, community convention has filled the gap where the ECMAScript specification leaves ambiguity; it is more commonplace to receive &lt;code&gt;Error&lt;/code&gt; objects as the argument to the &lt;code&gt;catch&lt;/code&gt; block nowadays, and good library implementors &lt;a href="https://github.com/goldbergyoni/nodebestpractices/blob/master/sections/errorhandling/useonlythebuiltinerror.md"&gt;will generally throw &lt;code&gt;Error&lt;/code&gt; objects for you to handle&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm broken&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// generates an exception&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;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// statements to handle any exceptions&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="c1"&gt;// clean up&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;catch&lt;/code&gt; block, you should add any code that allows you to put your app back into a &lt;em&gt;defined state&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;React's documentation for their Error Boundaries feature explains the problem well from a UI perspective, and the same is also true for exception handling as a whole:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For example, in a product like Messenger leaving the broken UI visible could lead to somebody sending a message to the wrong person. Similarly, it is worse for a payments app to display the wrong amount than to render nothing.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's also a good idea to log your exception somewhere — failing silently is rarely useful, your aim here is to surface the exception as best as you can to debug problems before they become a problem for the user.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In your code, the &lt;code&gt;catch&lt;/code&gt; block is where you would want to include your call to &lt;code&gt;appsignal.sendError()&lt;/code&gt; if you're using our JavaScript library. Here, you can pass the &lt;code&gt;Error&lt;/code&gt; object as an argument and have it appear in your Errors dashboard.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;finally&lt;/code&gt; block tends to not be as useful in JavaScript as it is in other languages. In the &lt;code&gt;finally&lt;/code&gt; block, normally should try to clean-up any resources created before the exception was thrown, however as JavaScript is a garbage-collected language and resources are allocated and de-allocated dynamically, we often don't have to think about this much. There are times where this can be useful, however, such as for closing open connections to remote services regardless of whether the request to it was successful or not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Promises and Async JavaScript
&lt;/h2&gt;

&lt;p&gt;Admittedly, in our last post, we might have seemed a little negative about the design of JavaScript as a language. While it's almost certainly true that a lot of mistakes were made — and thanks to the ever-present need for backwards compatibility, many of them still exist today — arguably, there has been a lot of ground covered since then to make amends, and many aspects of the original design of JavaScript still hold up well today.&lt;/p&gt;

&lt;p&gt;One of those areas that JavaScript is great at is asynchronous programming. JavaScript is an &lt;em&gt;event-driven&lt;/em&gt; language, which is, in its simplest terms, the means to allow code to be executed by listening for &lt;em&gt;events&lt;/em&gt; that can be triggered based on user interaction, or even messages from other programs. This is a great fit for a language like JavaScript that is mostly found embedded in a graphical environment, where you might feasibly want to execute code based on mouse clicks, or key presses. &lt;/p&gt;

&lt;p&gt;Thanks to JavaScript's Event Loop (a concept we'll cover in full in a later edition of &lt;a href="https://blog.appsignal.com/category/javascript-sorcery.html"&gt;JavaScript Sorcery&lt;/a&gt;) and recent developments in the language, JavaScript lets you define points in your program where the flow of execution can be returned to the program in lieu of a value, allowing the rest of your program to run and the UI to update, and the value to the latter be filled later. We call these values &lt;code&gt;Promise&lt;/code&gt;s.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Promise&lt;/code&gt;s themselves can contain exceptions, which when they are thrown, cause the &lt;code&gt;Promise&lt;/code&gt; to become rejected. Once rejected, a &lt;code&gt;Promise&lt;/code&gt; can execute a user-defined callback that we chain to it using &lt;code&gt;.catch&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// You can catch errors asynchronously by listening to Promises...&lt;/span&gt;
&lt;span class="nx"&gt;asyncActionThatReturnsAPromise&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;appsignal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sendError&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Errors can also be caught in the &lt;code&gt;onRejected&lt;/code&gt; handler, a second parameter to &lt;code&gt;.then&lt;/code&gt; that takes a function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;asyncActionThatReturnsAPromise&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;onFulfilled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onRejected&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first argument to the &lt;code&gt;.catch&lt;/code&gt; callback will normally be an &lt;code&gt;Error&lt;/code&gt; object, but just like the &lt;code&gt;try&lt;/code&gt;/ &lt;code&gt;catch&lt;/code&gt; statements above, there is no explicit rule about what kind of value a &lt;code&gt;Promise&lt;/code&gt; can be rejected with and thus passed to the &lt;code&gt;.catch&lt;/code&gt; callback. It could technically be any value. We recommend that, when writing your own &lt;code&gt;Promise&lt;/code&gt;s, you do yourself and any future developers using your code the courtesy to reject &lt;code&gt;Promise&lt;/code&gt;s with &lt;code&gt;Error&lt;/code&gt; objects.&lt;/p&gt;

&lt;p&gt;Any &lt;code&gt;Promise&lt;/code&gt;s that become rejected that don't have a callback bound to the &lt;code&gt;.catch&lt;/code&gt; handler will instead fire a callback on the &lt;code&gt;window&lt;/code&gt; object called &lt;code&gt;onunhandledrejection&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onunhandledrejection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// error handling code here!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Recently, the ECMAScript standard was amended to add the &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt; keywords. With these keywords, we can write async code that looks like synchronous code by using the &lt;code&gt;await&lt;/code&gt; keyword within an &lt;code&gt;async&lt;/code&gt; function to denote to the program that it should pause execution of the async function and wait for a value that a &lt;code&gt;Promise&lt;/code&gt; is fulfilled with.&lt;/p&gt;

&lt;p&gt;As we can use &lt;code&gt;async&lt;/code&gt;/ &lt;code&gt;await&lt;/code&gt; and async functions to write code that looks like it's synchronous even though it's not, then it's sensible to expect that we can also use the &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt; statement to handle exceptions within them, and in fact, we can!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ...or by using async/await&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&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;asyncActionThatReturnsAPromise&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;appsignal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sendError&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="c1"&gt;// handle the error&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;C'est tout!&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;That's all we have for this week!&lt;/p&gt;

&lt;p&gt;Don't forget: our JavaScript integration was released recently and we'd love for you to &lt;a href="https://docs.appsignal.com/front-end/"&gt;give it a try&lt;/a&gt; in your front-end applications and tell us what you think.&lt;/p&gt;

&lt;p&gt;If you liked this post, &lt;a href="https://blog.appsignal.com/javascript-sorcery"&gt;subscribe to our new JavaScript Sorcery list&lt;/a&gt; for a monthly deep dive into more magical JavaScript tips and tricks.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScript Errors: An Exceptional History</title>
      <dc:creator>Adam Yeats</dc:creator>
      <pubDate>Fri, 18 Oct 2019 15:10:23 +0000</pubDate>
      <link>https://dev.to/appsignal/javascript-errors-an-exceptional-history-43mh</link>
      <guid>https://dev.to/appsignal/javascript-errors-an-exceptional-history-43mh</guid>
      <description>&lt;p&gt;It's a historic week here at AppSignal! 🎉 This week we released the first version of our new and improved &lt;a href="https://docs.appsignal.com/front-end/"&gt;JavaScript error monitoring&lt;/a&gt;. Now you can have your front end code, Ruby or Elixir back end code, your hosts, performance, everything monitored in one interface. &lt;/p&gt;

&lt;p&gt;To celebrate the launch, in a two-part series of posts, we'll be taking a look at the history of Errors in JavaScript, including how to handle them in your code today. In this part, join us as we go through the origins and turbulent years of growth of JavaScript and see it grow into the language it is today.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Comedy of Errors
&lt;/h2&gt;

&lt;p&gt;In accordance with &lt;a href="https://en.wikipedia.org/wiki/Murphy%27s_law"&gt;Murphy's Law&lt;/a&gt;, arguably ticket #1 on the backlog of the great Jira board of the universe, runtime errors have existed as a concept since the very first version of JavaScript, shipped as part of Netscape Navigator 2.0 in 1995, and later as part of the second iteration of Internet Explorer. &lt;/p&gt;

&lt;p&gt;Here in 2019, it's difficult to conceive of a primordial version of JavaScript. Famously prototyped in 10 days, and publicly released mere months later, the early history of JavaScript exists as a flawed, but nonetheless impressive, monument to compromise and short-term corporate thinking; plans to implement &lt;a href="https://en.wikipedia.org/wiki/Scheme_(programming_language)"&gt;Scheme&lt;/a&gt; as the embedded scripting language in Netscape Navigator gave way to desires to include a "glue language" to compliment Java, producing a hybrid of syntax vaguely resembling Java but with ideas cribbed from dynamically typed languages like &lt;a href="https://en.wikipedia.org/wiki/Scheme_(programming_language)"&gt;Scheme&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/HyperTalk"&gt;HyperTalk&lt;/a&gt; and &lt;a href="http://www.selflanguage.org/"&gt;Self&lt;/a&gt; in order to differentiate itself from the more "serious" and statically typed Java. &lt;/p&gt;

&lt;h2&gt;
  
  
  One Point Uh-Oh
&lt;/h2&gt;

&lt;p&gt;JavaScript 1.0 shipped without some common language features. For instance, support for a list-like data structure (the &lt;code&gt;Array&lt;/code&gt; object was added in version 1.1). Object and array literals (&lt;code&gt;[]&lt;/code&gt; and &lt;code&gt;{}&lt;/code&gt;) would not arrive until JavaScript 1.2. Some features changed rapidly — JavaScript 1.2 changed the &lt;code&gt;==&lt;/code&gt; and &lt;code&gt;!=&lt;/code&gt; operators to make them no longer coerce types if the compared values were not of the same type. This would later be reversed with version 1.3 with the advent of the ECMA standardization, and the &lt;code&gt;===&lt;/code&gt; and &lt;code&gt;!==&lt;/code&gt; operators would fill the gap.&lt;/p&gt;

&lt;p&gt;Perhaps most pertinent to the theme of this post, however, JavaScript shipped without the ability to handle runtime errors. If your script produced a runtime error, at the time, there was no mechanism to recover from this. A runtime error would "crash" the page, and present the user with an ugly, cryptic error message and no other option than to reload and hope that, fingers crossed, the script would evaluate correctly this time. Those of us using the web in those days don't get the warm-and-fuzzies when we remember the dreaded un-closable  "Script error" dialog, but luckily those days are far behind us.&lt;/p&gt;

&lt;p&gt;The wake of these, and many other, decisions of those ten days in May 1995 has stretched unusually far into the future. It can be argued that the perception of JavaScript as a folly, a mere toy language for those not competent enough to embrace "real" languages, is birthed from these early design flaws and Netscape's own marketing attempts to position JavaScript as a sidecar to Java, a reputation that has been hard for JavaScript to shake as its adoption and ubiquity grows.&lt;/p&gt;

&lt;p&gt;Ultimately, however, extensions were made to the JavaScript language to address many of these initial hurdles, leading to the eventual standardization of JavaScript as ECMAScript in 1997, the formation of TC39, and the beginning of the tumultuous journey to the JavaScript of today.&lt;/p&gt;

&lt;h2&gt;
  
  
  An Exceptional History
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%201st%20edition,%20June%201997.pdf"&gt;The first version of the ECMA-262 standardization of JavaScript&lt;/a&gt; included a section on errors short enough to be almost memorizable. It talks very little about runtime errors, only hinting at "catchable [errors] in future versions". It would take until the 3rd Edition of ECMA-262 (colloquially known as ES3) for proper exception handling semantics to be ratified as part of the ECMAScript specification.&lt;/p&gt;

&lt;p&gt;In the meantime, it would be Netscape who would take the initial steps and add two of the core pillars of JavaScript error handling.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;window.onerror&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Version 1.1 of JavaScript (shipped with Netscape Navigator 3.0), would be the first to have a mechanism for error handling of any kind, in the form of the &lt;code&gt;window.onerror&lt;/code&gt; event. JavaScript was designed as an event-driven language from the very beginning.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://web.archive.org/web/20060318153542/wp.netscape.com/eng/mozilla/3.0/handbook/javascript/index.html"&gt;the Netscape Navigator 3.0 documentation&lt;/a&gt;, the &lt;code&gt;window.onerror&lt;/code&gt; event is originally defined as firing when "the loading of a document or image causes an error".&lt;/p&gt;

&lt;p&gt;In Navigator 3.0, handling an error would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lineno&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// error handling code here!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onerror&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;onError&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This enabled JavaScript developers of the time to create something resembling a trace from the arguments provided to the event handler but did not offer any solutions with regards from recovering from an error.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Basic exception handling constructs like &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt; and &lt;code&gt;throw&lt;/code&gt; were not a part of language until JavaScript 1.4, a version of JavaScript that was only used for Netscape LiveWire, a server-side JavaScript implementation released in 1999, predating Node.js by a decade. They do not appear in the ECMAScript specification until version 3. This can be considered the second "true" version of the ECMAScript standard (ECMAScript 2 contained only editorial changes).&lt;/p&gt;

&lt;p&gt;Although exception handling as a concept predates it by many years in both software and hardware, &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt; exception handling was a concept first introduced in software in C++ and later extended with the &lt;code&gt;finally&lt;/code&gt; block in Java. A program can "try" executing a block of code that may throw an exception, and interrupt the normal flow of execution of our program and execute code defined in the &lt;code&gt;catch&lt;/code&gt; block if it does.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;throw&lt;/code&gt; statement will pass the value to its right-hand side to the first argument of the &lt;code&gt;catch&lt;/code&gt; block. This could be any value; a common pattern in JavaScript was to throw a string and handle the error conditionally. Over time, conventions and best practices evolved, and it would become more common to throw an &lt;code&gt;Error&lt;/code&gt; object instead. No further statements beyond the &lt;code&gt;throw&lt;/code&gt; statement will execute. Instead, the code inside the &lt;code&gt;catch&lt;/code&gt; block will execute. This allows a developer to write logic to recover from runtime errors or log them somewhere for debugging at a later stage.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;finally&lt;/code&gt; block always executes, regardless of whether an exception was thrown or caught. If no catch block exists in the call stack when an exception is thrown, the script will simply terminate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm broken&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;// generates an exception&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;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// statements to handle any exceptions&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="c1"&gt;// clean up&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This change to JavaScript syntax would modify the semantics of the &lt;code&gt;window.onerror&lt;/code&gt; handler somewhat. As many errors could now be handled at runtime, the &lt;code&gt;window.onerror&lt;/code&gt; would begin to evolve into a funnel for unhandled exceptions.&lt;/p&gt;

&lt;h2&gt;
  
  
  The &lt;code&gt;Error&lt;/code&gt; object
&lt;/h2&gt;

&lt;p&gt;Another crucial ingredient of the JavaScript error handling picture is the &lt;code&gt;Error&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;The Error page on MDN is a fascinating read in itself. The &lt;code&gt;Error&lt;/code&gt; object is a compendium of inconsistency — a child of the badlands of the late 90's and oughts, an era in web development where new JavaScript features, rather than introduced and standardised on a yearly basis in a co-operative effort between vendors and developers like they are today, were created in a standoff of sorts between two major browser vendors vying for dominance of an industry in its infancy.&lt;/p&gt;

&lt;p&gt;The roots of the &lt;code&gt;Error&lt;/code&gt; object can be &lt;a href="https://web.archive.org/web/20000520064545/http://msdn.microsoft.com/workshop/languages/jscript/handling.asp"&gt;traced back to JScript 5&lt;/a&gt;, Microsoft's own reverse-engineered implementation of the ECMAScript standard and included in Internet Explorer. This marked the first time that instances of an &lt;code&gt;Error&lt;/code&gt; object could be thrown when runtime errors occurred, and could also be inherited from to create custom exceptions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;broken&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am an error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;broken&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;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// e.name = "Error"&lt;/span&gt;
  &lt;span class="c1"&gt;// e.message = "I am an error"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Early documentation from Netscape Navigator exists that implies the creation of your own custom exception object was expected, however Microsoft was somewhat ahead of the curve here with a more detailed exception object that could be thrown in place of a primitive value like a string. &lt;/p&gt;

&lt;p&gt;A quirk of the early Microsoft &lt;code&gt;Error&lt;/code&gt; object compared to the ECMA-standardised version is that the alternative arguments you could provide:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first argument was intended to be a value which, when combined with the &lt;code&gt;&amp;amp;&lt;/code&gt; (bitwise And) operator to combine the number property with the hexadecimal number &lt;code&gt;0xFFFF&lt;/code&gt;, would return the actual error code.&lt;/p&gt;

&lt;p&gt;The existence of the &lt;code&gt;Error&lt;/code&gt; object was later given legitimacy as part of the ECMAScript 3 specification, however, it's implementations remain inconstant across browsers to this day. The &lt;code&gt;Error&lt;/code&gt; object as specified in ECMA-262 has two intrinsic properties: the description of the error (&lt;code&gt;message&lt;/code&gt;, &lt;code&gt;description&lt;/code&gt; in earlier Microsoft implementations) and the name of the error (&lt;code&gt;name&lt;/code&gt;). &lt;/p&gt;

&lt;p&gt;However, browser vendors have since added their own extensions to the &lt;code&gt;Error&lt;/code&gt; object, the most notable of which being the &lt;code&gt;stack&lt;/code&gt; property, returning a string representation of which functions were called leading up to the exception, in what order, from which line and file, and with what arguments. Although not part of the ECMAScript standard, almost all modern browsers include an &lt;code&gt;Error&lt;/code&gt; object with a &lt;code&gt;stack&lt;/code&gt; property. Older browsers, such as IE9, do not include this functionality.&lt;/p&gt;

&lt;h2&gt;
  
  
  Backtracing to today
&lt;/h2&gt;

&lt;p&gt;This brings us back to today. The &lt;a href="https://github.com/appsignal/appsignal-javascript"&gt;AppSignal JavaScript library&lt;/a&gt; depends heavily on the &lt;code&gt;stack&lt;/code&gt; property's presence to provide a backtrace in the UI — if it's not present, then there's no backtrace that we can display. &lt;/p&gt;

&lt;p&gt;In some cases (namely inside the &lt;a href="https://github.com/appsignal/appsignal-javascript/tree/develop/packages/plugin-window-events"&gt;plugin for window events&lt;/a&gt;), we attempt to construct a partial stack trace if none is present from the best information available at the time, but in some situations, no stack trace will be available at all. Luckily for all of us, the percentage of browsers in use today that do not support the &lt;code&gt;Error&lt;/code&gt; object's &lt;code&gt;stack&lt;/code&gt; property is relatively marginal.&lt;/p&gt;

&lt;h2&gt;
  
  
  That’s it for now!
&lt;/h2&gt;

&lt;p&gt;Next time, we'll look at how errors are handled in JavaScript &lt;em&gt;today&lt;/em&gt;, including methods to handle asynchronous code and how to best use the AppSignal JavaScript library to track them. &lt;/p&gt;

&lt;p&gt;We're super excited to have our JavaScript error tracking out in the wild, so please &lt;a href="https://docs.appsignal.com/front-end/"&gt;give it a try&lt;/a&gt; and tell us what you think. &lt;/p&gt;

&lt;p&gt;If you liked this post, &lt;a href="https://blog.appsignal.com/javascript-sorcery"&gt;subscribe to our new JavaScript Sorcery list&lt;/a&gt; for a monthly deep dive into more magical JavaScript tips and tricks. &lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>JavaScript Iterators and Iterables</title>
      <dc:creator>Adam Yeats</dc:creator>
      <pubDate>Wed, 17 Jul 2019 11:11:21 +0000</pubDate>
      <link>https://dev.to/appsignal/javascript-iterators-and-iterables-1bak</link>
      <guid>https://dev.to/appsignal/javascript-iterators-and-iterables-1bak</guid>
      <description>&lt;p&gt;Welcome to our first in-depth post on JavaScript! Here at AppSignal, we're gearing up to launch our all-new front-end monitoring solution, something we're very excited about and hope you will be, too.&lt;/p&gt;

&lt;p&gt;Over the last few years, I've watched JavaScript evolve from somewhat of an oddity—an admittedly imperfect, but often misunderstood, scripting language for the browser—to a powerful and expressive language in its own right, deployable in multiple environments, and almost ubiquitous in today's computing landscape.&lt;/p&gt;

&lt;p&gt;The aim of this corner of &lt;a href="http://appsignal.com"&gt;AppSignal.com&lt;/a&gt; is to explore this language in greater detail and uncover The Good Parts™ that make JavaScript awesome. Much like our sister blogs, &lt;a href="https://blog.appsignal.com/category/ruby-magic.html"&gt;Ruby Magic&lt;/a&gt; and &lt;a href="https://blog.appsignal.com/category/elixir-alchemy.html"&gt;Elixir Alchemy&lt;/a&gt;, we'll deep-dive into language features, patterns and frameworks, and share some other JavaScript insights along the way, too.&lt;/p&gt;

&lt;p&gt;Let's get to it! But first, let's talk about Ruby.&lt;/p&gt;

&lt;h2&gt;
  
  
  On Linked Lists and Rubyists
&lt;/h2&gt;

&lt;p&gt;In &lt;a href="https://blog.appsignal.com/2018/05/29/ruby-magic-enumerable-and-enumerator.html"&gt;a previous edition of Ruby Magic&lt;/a&gt;, Jeff explored Ruby's &lt;code&gt;Enumerator&lt;/code&gt; objects and &lt;code&gt;Enumerable&lt;/code&gt; module. This is described by Jeff like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Enumeration refers to traversing over objects. In Ruby, we call an object enumerable when it describes a set of items and a method to loop over each of them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Alright, sounds useful! I can already see plenty of reasons why you'd want this. In the aforementioned post, Jeff uses &lt;code&gt;Enumerable&lt;/code&gt; to implement a &lt;a href="https://en.wikipedia.org/wiki/Linked_list"&gt;Linked List&lt;/a&gt; — a common, almost evergreen data structure type that is a collection of data elements, in which each element points to the next. Each element in the list has two values, named the head and the tail. The head holds the element’s value, and the tail is a link to the rest of the list.&lt;/p&gt;

&lt;p&gt;By ensuring that the linked list responds to the &lt;code&gt;#each&lt;/code&gt; method, and by including the &lt;code&gt;Enumerable&lt;/code&gt; module, it's possible to implement this data structure in Ruby without writing a whole mess of code. This got me thinking - I wonder if JavaScript can do something like that?&lt;/p&gt;

&lt;p&gt;The answer: yes, it can! But, this wouldn't be a JavaScript blog post unless I told you that, of course, things are a little different here. Today, we're going to introduce you to JavaScript's close relative of Ruby's &lt;code&gt;Enumerable&lt;/code&gt; class, the &lt;em&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols"&gt;Iterable&lt;/a&gt;&lt;/em&gt;, and how we can leverage it to write a &lt;code&gt;LinkedList&lt;/code&gt; class of our own.&lt;/p&gt;

&lt;p&gt;Some of you may have never had to have implemented a Linked List before. No doubt, many of you have had to have implemented one as part of a job interview. Perhaps you, &lt;a href="https://medium.com/react-in-depth/the-how-and-why-on-reacts-usage-of-linked-list-in-fiber-67f1014d0eb7"&gt;like the React team&lt;/a&gt;, are already using them to do non-trivial things in your codebase. The example we'll be implementing today is almost an exact port of Jeff's Ruby &lt;code&gt;LinkedList&lt;/code&gt; class to JavaScript, which I really like due to the simplicity of the implementation. It is, perhaps, a little easier to grasp what's going on here than it would otherwise be with a "full-fat" implementation.&lt;/p&gt;

&lt;p&gt;It doesn't catch all the edge cases, or provide a number of class methods that you might expect, but should help illustrate the idea. Consider yourself warned: you will be sent to programming hell if I catch you using this code in production, and there, no amount of random key combinations will help you exit Vim.&lt;/p&gt;

&lt;p&gt;Okay, let's begin.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, what's an iterator?
&lt;/h2&gt;

&lt;p&gt;An iterable in JavaScript is an object that defines custom iteration behaviour via a method on itself or any of the objects in its prototype chain. You're probably already quite familiar with some of the built-in JavaScript types that are iterables, chiefly &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Map&lt;/code&gt;, &lt;code&gt;Set&lt;/code&gt; and &lt;code&gt;String&lt;/code&gt;. In common programming parlance, we say that these types can be "looped over"; given a construct like a &lt;code&gt;for&lt;/code&gt; loop, we can extract each value in order from the iterable and do something with it. &lt;/p&gt;

&lt;p&gt;JavaScript provides the &lt;code&gt;for...of&lt;/code&gt; loop for iterating over a generic iterable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;value&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;iterable&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="nx"&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also &lt;em&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment"&gt;destructure&lt;/a&gt;&lt;/em&gt; an iterable to get a subset of its values as named variables. In the following example, &lt;code&gt;a === 'a'&lt;/code&gt; and &lt;code&gt;b === 'b'&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&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;b&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Iterables can even be spread into an array literal, transforming your iterable into a linear array and allowing you to call array methods like .map() or .filter() on the returned value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So what makes an object iterable? Here's where things start to get a little more advanced. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;@@iterator&lt;/code&gt; - The Invisible Property
&lt;/h2&gt;

&lt;p&gt;In order to become an iterable, a special function must be implemented on the object itself - &lt;code&gt;@@iterator&lt;/code&gt;. Now, to many of you out there, you would be forgiven to have been blissfully unaware that this property ever existed. It can't be accessed by calling &lt;code&gt;iterable.@@iterator&lt;/code&gt;. It doesn't show up in a  &lt;code&gt;for&lt;/code&gt; loop or when calling &lt;code&gt;Object.keys&lt;/code&gt; on an iterable. Often, &lt;code&gt;console.log&lt;/code&gt; won't even reveal this property. So, where is it?&lt;/p&gt;

&lt;p&gt;Unlike other programming languages, JavaScript doesn't &lt;a href="https://github.com/tc39/proposal-private-methods"&gt;(yet)&lt;/a&gt; have the concept of private methods or private fields on an object, but we can make a property of an object "pseudo-private" by referencing it using a special JavaScript type called a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol"&gt;Symbol&lt;/a&gt;. The &lt;code&gt;@@iterator&lt;/code&gt; property is implemented in this way: the value of the &lt;code&gt;@@iterator&lt;/code&gt; property can only be referenced using a &lt;code&gt;Symbol&lt;/code&gt; key that is defined as a constant on the &lt;code&gt;Symbol&lt;/code&gt; type itself: &lt;code&gt;Symbol.iterator&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Accessing it works like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;LinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;iterator&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;// ...or using an object literal&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;LinkedList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;span class="nx"&gt;LinkedList&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;iterator&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;On a given &lt;code&gt;class&lt;/code&gt; or object, where the key is &lt;code&gt;Symbol.iterator&lt;/code&gt;, the value must be a function. In a classic, synchronous implementation of an iterator, this function returns an object (called an &lt;em&gt;iterable&lt;/em&gt;) that implements a function called &lt;code&gt;next()&lt;/code&gt; as a property. Let's expand a little further on our example to see what this looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;LinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;iterator&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="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;next&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="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a value&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Holy nested statements! We've managed to erect a small pyramid in our shiny new codebase, but we have successfully implemented an &lt;em&gt;iterator&lt;/em&gt; that returns an &lt;em&gt;iterable&lt;/em&gt;. The iterable itself returns an object with two properties: &lt;code&gt;value&lt;/code&gt; and &lt;code&gt;done&lt;/code&gt;. Unsurprisingly, &lt;code&gt;value&lt;/code&gt; is the current value of the iterator, and &lt;code&gt;done&lt;/code&gt; is a boolean value to communicate to the iterator if the sequence of values has ended. If &lt;code&gt;done === true&lt;/code&gt;, then the &lt;code&gt;value&lt;/code&gt; property can be emitted.&lt;/p&gt;

&lt;p&gt;Now we know a little more about how iterators and iterables work, let's see how we can apply this knowledge to build a &lt;code&gt;LinkedList&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building the &lt;code&gt;LinkedList&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Let's start out by just porting Jeff's Ruby class into JavaScript, sans the &lt;code&gt;#each&lt;/code&gt; method used to create an &lt;code&gt;Enumerable&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;LinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&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;rest&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="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;LinkedList&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rest&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="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// roughly equivalent to `rest.any?` in ruby&lt;/span&gt;
    &lt;span class="k"&gt;else&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;rest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;LinkedList&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;rest&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;LinkedList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So far, so good. Using the above example, we can already create a new &lt;code&gt;LinkedList&lt;/code&gt;, and add new items to the head of the &lt;code&gt;LinkedList&lt;/code&gt;, using the &lt;em&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters"&gt;rest&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax"&gt;spread&lt;/a&gt;&lt;/em&gt; operator (&lt;code&gt;...&lt;/code&gt;) to create the tail. As the first argument to the constructor, we allow anyone using our &lt;code&gt;LinkedList&lt;/code&gt; class to pass a &lt;code&gt;head&lt;/code&gt; as the top of the linked list, and the &lt;em&gt;rest&lt;/em&gt; operator in the &lt;code&gt;constructor&lt;/code&gt; is able to convert any remaining arguments that are not &lt;code&gt;head&lt;/code&gt;, and convert them to an array. The &lt;code&gt;else if&lt;/code&gt; statement creates a new &lt;code&gt;LinkedList&lt;/code&gt; from this array, and continues to do so until the last item in &lt;code&gt;rest&lt;/code&gt; belongs to the &lt;code&gt;head&lt;/code&gt; of a &lt;code&gt;LinkedList&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, we'll need to implement the logic to retrieve the items from the &lt;code&gt;LinkedList&lt;/code&gt;, but I can already see a problem. If we implement an iterator, and the subsequent iterable, using the technique outlined above, then we're already deviating from Jeff's initial design quite considerably. There's a lot more code to write, and we'll need to maintain state somehow, as we need to tell the iterable that our sequence is finished by setting &lt;code&gt;done&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt;. It's certainly possible, but I think we can come up with something more elegant.&lt;/p&gt;

&lt;p&gt;Enter the Generator function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Generator functions
&lt;/h2&gt;

&lt;p&gt;The value we set as &lt;code&gt;Symbol.iterator&lt;/code&gt; can also be a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*"&gt;generator&lt;/a&gt;, a new type of function that was introduced with ECMAScript 2015. The easiest way to think of a generator function is a function that you can exit and return to at will, optionally returning a value with the &lt;code&gt;yield&lt;/code&gt; keyword. Using the power of &lt;a href="https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch5.md"&gt;closures&lt;/a&gt;, we can maintain the state of the function across multiple &lt;code&gt;yield&lt;/code&gt;s and re-entries. Importantly, generator functions have the same interface as an iterable, meaning values can be retrieved in the same manner as if we had implemented the iterable ourselves.&lt;/p&gt;

&lt;p&gt;Let's implement an iterator to get all of the values from our &lt;code&gt;LinkedList&lt;/code&gt; using a generator function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;LinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...implementation&lt;/span&gt;

  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;iterator&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&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;next&lt;/span&gt; &lt;span class="o"&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;tail&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&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="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  The Full Implementation
&lt;/h2&gt;

&lt;p&gt;So, when all is said and done, this is what we end up with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;LinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&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;rest&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="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;LinkedList&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rest&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="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// roughly equivalent to `rest.any?` in ruby&lt;/span&gt;
    &lt;span class="k"&gt;else&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;rest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;LinkedList&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;rest&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;LinkedList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&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="p"&gt;}&lt;/span&gt;

  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;iterator&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&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;next&lt;/span&gt; &lt;span class="o"&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;tail&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&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="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can then use our new &lt;code&gt;LinkedList&lt;/code&gt; class like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ll&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;LinkedList&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="mi"&gt;1&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="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&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;value&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;ll&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="nx"&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="c1"&gt;// output: 0, 1, 1, 2, 3, 5, 8, 13&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ll&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// a = 0, b = 1&lt;/span&gt;

&lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;ll&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;num&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// output: 0, 1, 1, 2, 3, 5, 8, 13&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  And that's it!
&lt;/h2&gt;

&lt;p&gt;The first time the function is run, we &lt;code&gt;yield&lt;/code&gt; the current head. Then, as long as there is a tail to read from, we &lt;code&gt;yield&lt;/code&gt; the head of the list item on the tail. Once we've done that, the iterator is implicitly &lt;code&gt;done&lt;/code&gt;. In seven lines of code, we've implemented our iterator. Awesome!&lt;/p&gt;

&lt;p&gt;Let us know what you think about this blog, or what JavaScript wonders you'd like us to write about on Twitter &lt;a href="https://www.twitter.com/appsignal"&gt;@AppSignal&lt;/a&gt;&lt;/p&gt;

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