<?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: Max Cerrina</title>
    <description>The latest articles on DEV Community by Max Cerrina (@alephnaught2tog).</description>
    <link>https://dev.to/alephnaught2tog</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%2F30765%2F09c17240-a815-423b-ae83-87ef79d6b64c.png</url>
      <title>DEV Community: Max Cerrina</title>
      <link>https://dev.to/alephnaught2tog</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alephnaught2tog"/>
    <language>en</language>
    <item>
      <title>Packages 101</title>
      <dc:creator>Max Cerrina</dc:creator>
      <pubDate>Wed, 20 Feb 2019 18:36:33 +0000</pubDate>
      <link>https://dev.to/alephnaught2tog/packages-101-4857</link>
      <guid>https://dev.to/alephnaught2tog/packages-101-4857</guid>
      <description>&lt;p&gt;Many web development projects use different packages. You will often see people talk about packages -- installing them, which ones they like, which ones they don't, how their size compares to others', whether a certain package was useful, etc. Packages are closely related to dependencies -- things your code needs in order to work.&lt;/p&gt;

&lt;p&gt;Dependency management can be &lt;em&gt;very&lt;/em&gt; complex; we won't go over things like versioning, etc. Different people and teams use different package managers; here, we'll just be using &lt;code&gt;npm&lt;/code&gt;, which is very common.&lt;/p&gt;

&lt;p&gt;We won't make a real project, just go over the package management portion of it. If you want to try the commands yourself, you will need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.npmjs.com/downloading-and-installing-node-js-and-npm"&gt;node and npm&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;a terminal to run the commands in&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  So what &lt;em&gt;are&lt;/em&gt; packages, anyway?
&lt;/h2&gt;

&lt;p&gt;A &lt;em&gt;package&lt;/em&gt; is code you want to use in your own project that comes from somewhere else. They can be used either just for the developers -- such as a package that lets you write and run tests for your code -- or in the project itself, such as a library of functions to make it easier to talk to websockets, or a package that gives you pre-made CSS classes to use in your code. Using a package instead of writing it yourself can save you a lot of time, and also make things easier. For example, maybe you don't know (or care to!) how the websocket protocol works, you just want a tool that lets you use it a chat app. Frequently, packages are talked about as dependencies: things your code needs in order to function correctly.&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;package manager&lt;/em&gt; is a tool you can use to handle packages for you: usually that means you use it to add (install) new packages, remove (uninstall) them, find new ones, etc. &lt;a href="https://docs.npmjs.com/"&gt;npm&lt;/a&gt; is a package manager. You &lt;em&gt;could&lt;/em&gt; just find the code somewhere online and put it in its own script file in your source folder instead of as a package -- but if that code changes, or gets updated, you have to go find it again, get the new code, put it in yourself ... assuming you even know the code you're using was updated at all.&lt;/p&gt;

&lt;p&gt;A package manager like npm also helps you manage &lt;em&gt;which&lt;/em&gt; version you should install. It has a &lt;em&gt;registry&lt;/em&gt; of the packages, where it stores the packages, their versions, etc. npm's registry is at &lt;a href="https://www.npmjs.com/"&gt;npmjs.com&lt;/a&gt;. Usually it will also take care of installing any dependencies of the packages you've installed, so that they will work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick syntax notes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;package_name&amp;gt;&lt;/code&gt;: the name of a &lt;em&gt;single&lt;/em&gt; package. The angle brackets (&lt;code&gt;&amp;lt;&lt;/code&gt; and &lt;code&gt;&amp;gt;&lt;/code&gt;) are a common way of showing that you should replace that whole term (&lt;code&gt;&amp;lt;package_name&amp;gt;&lt;/code&gt;) with what you want it to be. Often, when you see angle brackets in documentation, it indicates that the value that goes there is required. &lt;strong&gt;You do NOT use the &lt;code&gt;&amp;lt;&lt;/code&gt; and &lt;code&gt;&amp;gt;&lt;/code&gt; when you run the commands.&lt;/strong&gt; Examples:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;npm view &amp;lt;package_name&amp;gt;&lt;/code&gt; =&amp;gt; &lt;code&gt;npm view react&lt;/code&gt; to view information about a package named &lt;code&gt;react&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm search &amp;lt;package_name&amp;gt;&lt;/code&gt; =&amp;gt; &lt;code&gt;npm search cool_new_package&lt;/code&gt; to look for a package named &lt;code&gt;cool_new_package&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[list_of_packages...]&lt;/code&gt;: a list of package names. The square brackets (&lt;code&gt;[&lt;/code&gt; and &lt;code&gt;]&lt;/code&gt; are a common way of showing that something should be a list with a varying number of things inside it. It could have one item or many items. When you see something with square brackets in documentation, that usually means it's optional. &lt;strong&gt;You do NOT use the &lt;code&gt;[&lt;/code&gt; and &lt;code&gt;]&lt;/code&gt; when you run the commands.&lt;/strong&gt; For example, &lt;code&gt;npm install [list_of_packages...]&lt;/code&gt; could be:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;npm install jest chalk request&lt;/code&gt;: install three packages -- one named &lt;code&gt;jest&lt;/code&gt;, one named &lt;code&gt;chalk&lt;/code&gt;, and one named &lt;code&gt;request&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm install react&lt;/code&gt;: install one package named &lt;code&gt;react&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm install&lt;/code&gt;: install all of the packages listed in the &lt;code&gt;package.json&lt;/code&gt; file&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;an &lt;strong&gt;option&lt;/strong&gt; is something like &lt;code&gt;--save-dev&lt;/code&gt; or &lt;code&gt;--depth&lt;/code&gt;: it's used to give the command itself more information, like arguments to a function. Often they have a short form and a long form; e.g., the option for saving something in &lt;code&gt;devDependencies&lt;/code&gt; on an &lt;code&gt;npm&lt;/code&gt; command is written as &lt;code&gt;--save-dev&lt;/code&gt; or &lt;code&gt;-D&lt;/code&gt;. Usually the long version has two dashes (&lt;code&gt;--save-dev&lt;/code&gt;), whereas the short version usually as just one (&lt;code&gt;-D&lt;/code&gt;). We'll use the long versions here (I do when I run them myself, honestly!) because they are easier to understand.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;Before we can add packages to a project, we need a project to add them to!&lt;/p&gt;

&lt;p&gt;Often, you'll be working on a project that already exists, so you won't need to do this, but it's good to know how. It's a great way to create a sandbox to try things in without worrying that you might do something wrong.&lt;/p&gt;

&lt;p&gt;npm uses a file called &lt;code&gt;package.json&lt;/code&gt; for getting information about your project, like what dependencies it has. While it contains a lot of other important information, today we'll just focus on what it does for packages (explained more when we address how to add them).&lt;/p&gt;

&lt;p&gt;You can make a new project by creating a new folder, and from within that folder, running one of two commands, both of which result in npm making a &lt;code&gt;package.json&lt;/code&gt; file for you.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;npm init&lt;/code&gt;: starts an interactive app that asks you some questions and then creates a &lt;code&gt;package.json&lt;/code&gt; file for you&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm --yes init&lt;/code&gt;: creates a default &lt;code&gt;package.json&lt;/code&gt; file for you, and doesn't make you answer any questions or pick anything (usually this is what I do to get started, it's faster)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don't worry if you change your mind about an option you picked, or if you decide you want to add something later; &lt;code&gt;package.json&lt;/code&gt; is just a file, and you can edit it in a text editor afterwards if you like.&lt;/p&gt;

&lt;p&gt;If you are using git in your project, &lt;strong&gt;make sure you have &lt;code&gt;node_modules&lt;/code&gt; added to your &lt;code&gt;.gitignore&lt;/code&gt; file.&lt;/strong&gt; You can do that by adding it to the file in a text editor, or by running &lt;code&gt;echo 'node_modules' &amp;gt;&amp;gt; .gitignore&lt;/code&gt; from the command line.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding packages
&lt;/h2&gt;

&lt;p&gt;The easiest way to find a package is probably to look at the &lt;a href="http://www.npmjs.com"&gt;npm registry's site&lt;/a&gt; -- you can search for packages, see how many people have used it, etc., get an idea of how its documentation is, etc. There are a &lt;em&gt;lot&lt;/em&gt; of packages available: for whatever you're looking for, there is almost certainly an existing package, if not many, that will do what you need. If you try one and you don't like it, or it's too hard to use, try looking for another!&lt;/p&gt;

&lt;p&gt;When you're looking at different packages, it can be helpful to look at the package's Github; this can give you an idea of how active it is, how many issues there are, etc. A package that hasn't been updated in a while isn't necessarily bad or out-of-date -- it may not have &lt;em&gt;needed&lt;/em&gt; updates. Similarly, a package with a lot of issues on Github may have that many issues because it has a &lt;em&gt;lot&lt;/em&gt; of users; it doesn't mean the package is bad or poorly maintained.&lt;/p&gt;

&lt;p&gt;There are also commands you can run to see a lot of the same information from the command-line&lt;br&gt;
&lt;em&gt;(remember, don't use the &lt;code&gt;&amp;lt;&lt;/code&gt; and &lt;code&gt;&amp;gt;&lt;/code&gt; when you run these with real package names!)&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;npm search &amp;lt;package_name&amp;gt;&lt;/code&gt;: look for all packages in the npm registry whose name matches &lt;code&gt;&amp;lt;package_name&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm view &amp;lt;package_name&amp;gt;&lt;/code&gt;: view detailed information about a package, such as its versions, keyswords, description, its own dependencies, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Adding new packages to a project
&lt;/h2&gt;

&lt;p&gt;When you install a package, npm gets the package and puts it into your &lt;code&gt;node_modules&lt;/code&gt; folder, as well as anything else that package itself needs. npm uses your &lt;code&gt;package.json&lt;/code&gt; file to know what packages (and which versions) to install, as well as a &lt;a href="https://docs.npmjs.com/files/package.json"&gt;lot of other things&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There are two main kinds of dependencies in your &lt;code&gt;package.json&lt;/code&gt; folder: &lt;code&gt;dependencies&lt;/code&gt; and &lt;code&gt;devDependencies&lt;/code&gt;. (There are also other kinds, but we won't worry about them today.)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;dependencies&lt;/code&gt; are packages your project needs in order to run. If you are using a package like &lt;code&gt;moment&lt;/code&gt; to handle math involving dates in your project, that would be a package you should have in &lt;code&gt;dependencies&lt;/code&gt;: it's required for your project to work.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;devDependencies&lt;/code&gt; are packages you want but that your code itself doesn't need to run when it's finished. A tool like TypeScript or Babel that compiles your code is a good example of a common &lt;code&gt;devDependencies&lt;/code&gt; package: you need it while you're working on the code, but your project &lt;em&gt;itself&lt;/em&gt; doesn't need it to run. Something like a testing tool, or a linter, are also good examples of what would belong in &lt;code&gt;devDependencies&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To add a new package, you use the &lt;code&gt;npm install&lt;/code&gt; command. npm will also add the package information to your &lt;code&gt;package.json&lt;/code&gt; file as a dependency automatically.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Remember, don't use the &lt;code&gt;[&lt;/code&gt; and &lt;code&gt;]&lt;/code&gt; when you run these with real package names!)&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;npm install --dry-run [list_of_packages...]&lt;/code&gt;: do everything as if you were going to install these packages, short of &lt;em&gt;actually&lt;/em&gt; installing them.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm install --save-dev [list_of_packages...]&lt;/code&gt;: install these packages and add them to our &lt;code&gt;package.json&lt;/code&gt; as &lt;code&gt;devDependencies&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm install --save-prod [list_of_packages...]&lt;/code&gt;: install these packages and add them to our &lt;code&gt;package.json&lt;/code&gt; as &lt;code&gt;dependencies&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm install --global [list_of_packages...]&lt;/code&gt;: install these packages &lt;em&gt;globally&lt;/em&gt; -- if you do this from a project folder, these packages will &lt;strong&gt;not be added&lt;/strong&gt; to your &lt;code&gt;package.json&lt;/code&gt; files in the project. A good example of when you might want to use this is for a tool like &lt;code&gt;create-react-app&lt;/code&gt; that helps you &lt;em&gt;start&lt;/em&gt; a new project: because you want it to &lt;em&gt;make&lt;/em&gt; the project, you don't want to install it locally &lt;em&gt;inside&lt;/em&gt; the project.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Dealing with existing packages
&lt;/h2&gt;

&lt;p&gt;If you've just cloned an existing project, the first thing you should do is run &lt;code&gt;npm install&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;node_modules&lt;/code&gt; folders get &lt;em&gt;very&lt;/em&gt; large very quickly, they are almost always excluded from version control like git. That means that when you clone down a project, any packages you need aren't there yet, and the code won't work.&lt;/p&gt;

&lt;p&gt;Once you have packages in a project, you will sometimes need to remove or update them; it's good to also just peruse things every so often, make sure you don't have unnecessary packages left lying around, etc.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;npm uninstall [list_of_packages...]&lt;/code&gt;: remove the packages listed (can be just one package); this will remove them from your &lt;code&gt;package.json&lt;/code&gt; file as well as from your &lt;code&gt;node_modules&lt;/code&gt; directory.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm list --depth 0&lt;/code&gt;: view list of all packages installed at the top level; to view &lt;em&gt;all&lt;/em&gt; packages and all &lt;em&gt;their&lt;/em&gt; dependencies, you can do &lt;code&gt;npm list --depth 1000&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm la --depth 0&lt;/code&gt;: view list of all packages installed, along with their descriptions at the top level&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm outdated&lt;/code&gt;: view packages which are behind the desired version&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Remember that using tools is a skill, and it takes practice to get good at it; it might feel weird or seem silly, but it can be helpful to make and delete a dozen fake projects &lt;em&gt;just&lt;/em&gt; to practice setting them up, wrangling packages, etc.&lt;/p&gt;

&lt;p&gt;There are a &lt;a href="https://docs.npmjs.com/cli-documentation/"&gt;&lt;em&gt;many&lt;/em&gt; more commands&lt;/a&gt;, and a lot of different ways to use them; you can read more about them in the documentation for the npm, or by running &lt;code&gt;npm help &amp;lt;command&amp;gt;&lt;/code&gt; to get information about that specific command.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>dependencies</category>
      <category>tooling</category>
    </item>
    <item>
      <title>Minimal (yes, truly) TypeScript setup</title>
      <dc:creator>Max Cerrina</dc:creator>
      <pubDate>Sat, 05 Jan 2019 21:55:13 +0000</pubDate>
      <link>https://dev.to/alephnaught2tog/minimal-yes-truly-typescript-setup-lil</link>
      <guid>https://dev.to/alephnaught2tog/minimal-yes-truly-typescript-setup-lil</guid>
      <description>&lt;p&gt;One of the most frustrating things &lt;em&gt;ever&lt;/em&gt; is wanting to try something out and having to install a million things and learn 3.14 new tools just to get something runnable in your browser.&lt;/p&gt;

&lt;p&gt;I wrote up a &lt;a href="https://github.com/aleph-naught2tog/ts_without_dependencies" rel="noopener noreferrer"&gt;minimal server and repository&lt;/a&gt; for a project that compiles your TypeScript down for use in a browser environment, so that people can try TypeScript if they want, without worrying about things like loaders, webpack, etc.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/aleph-naught2tog" rel="noopener noreferrer"&gt;
        aleph-naught2tog
      &lt;/a&gt; / &lt;a href="https://github.com/aleph-naught2tog/ts_without_dependencies" rel="noopener noreferrer"&gt;
        ts_without_dependencies
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Dependency-free skeleton for a web site with Typescript
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Dependency-free Typescript setup&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;This is a &lt;em&gt;truly&lt;/em&gt; minimal, &lt;strong&gt;dependency-free&lt;/strong&gt; setup that will provide the skeleton for you to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;write your code in Typescript&lt;/li&gt;
&lt;li&gt;compile it for browser use&lt;/li&gt;
&lt;li&gt;use that compiled code in a browser environment&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That's it. This server won't do anything fancy: it won't reload for you, it won't minify or uglify anything.&lt;/p&gt;
&lt;p&gt;It &lt;em&gt;will&lt;/em&gt; serve files for you, and that's it. You can ignore the server entirely if you want; or, if you think writing your own server sounds neat, the &lt;a href="https://github.com/aleph-naught2tog/ts_without_dependencies#about-the-server" rel="noopener noreferrer"&gt;second half of the README&lt;/a&gt; is worth checking out.&lt;/p&gt;
&lt;p&gt;There's no magic here. No Webpack, no loaders, no routing libraries necessary, etc. (If you don't know what those are, don't worry -- the &lt;em&gt;point&lt;/em&gt; of this repository is so you don't have to.) There's nothing wrong with using those tools, but they add complexity, and they can &lt;em&gt;extremely&lt;/em&gt; frustrating to use when all you really want…&lt;/p&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/aleph-naught2tog/ts_without_dependencies" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Its &lt;em&gt;only&lt;/em&gt; dependency is TypeScript itself. Literally. (Well, okay, Node too, but TypeScript itself uses Node, so I think that's permissible.)&lt;/p&gt;

&lt;p&gt;If all you want is to try a basic website with TypeScript, this is perfect for that. &lt;/p&gt;

&lt;p&gt;If you want to try writing your own server, please use this as a base if you want -- I included notes in the server files, let me know if anything is unclear! -- but if you want nothing to do with the server, you shouldn't need to mess with its code.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>new Array(1) =&gt; [empty title x 1]</title>
      <dc:creator>Max Cerrina</dc:creator>
      <pubDate>Fri, 04 Jan 2019 18:52:46 +0000</pubDate>
      <link>https://dev.to/alephnaught2tog/empty-title-x-1-47ng</link>
      <guid>https://dev.to/alephnaught2tog/empty-title-x-1-47ng</guid>
      <description>&lt;p&gt;Imagine it's been a rough week.&lt;/p&gt;

&lt;p&gt;Finally, it's time to refill our weekly chocolate supply. As usual, we're using JavaScript to fill our chocolate supply.&lt;/p&gt;

&lt;p&gt;In pseudocode, "a box of 21 of the same kind of chocolate" would be something like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Get a box.&lt;/li&gt;
&lt;li&gt;Make sure the box has enough slots for 21 chocolates.&lt;/li&gt;
&lt;li&gt;As long as there are still empty slots:

&lt;ul&gt;
&lt;li&gt;Get a chocolate from the giant bag of chocolates.&lt;/li&gt;
&lt;li&gt;Put one chocolate into the slot you're looking at.&lt;/li&gt;
&lt;li&gt;Move on to the next slot.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;Pretty reasonable, right? Let's give it a shot!&lt;/p&gt;

&lt;p&gt;(Note: all snippets should be runnable as-is in a repl or console as desired, just by copy-pasting if you like.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Attempt 1: &lt;code&gt;.map&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;For a first swing, maybe we'd try &lt;code&gt;map&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;let&lt;/span&gt; &lt;span class="nx"&gt;chocolate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;filling&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;raspberry ganache&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Prep our box and make sure it has 21 slots&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;weeklyChocolateSupplyBox&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;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Put one chocolate into every slot&lt;/span&gt;
&lt;span class="nx"&gt;weeklyChocolateSupplyBox&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;_emptySlot&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;chocolate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(If you're wondering about the underscore (i.e., &lt;code&gt;_emptySlot&lt;/code&gt;), that means the variable is either unimportant or unused. Some languages enforce that as a rule, like Elixir; here, it's purely convention.)&lt;/p&gt;

&lt;p&gt;So far, so good: we make an array with 21 slots, we loop over it with &lt;code&gt;map&lt;/code&gt;, and put a chocolate in at each slot.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;We actually put the exact same chocolate in each slot, which would be less than ideal in the real world — any changes to any one chocolate would effect EVERY chocolate, as they are all this way the same chocolate.&lt;/small&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;(╯°□°）╯︵ [Ɛ x ʎʇdɯǝ]&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Maybe not too surprisingly, it doesn't work. Instead of an array containing 21 identical chocolates, if you run that snippet in a console, you'll get something like: &lt;code&gt;[empty × 21]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Less than ideal, to say the least.&lt;/p&gt;

&lt;h2&gt;
  
  
  Attempt 2: &lt;code&gt;for (let index ... )&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;While I love using the various array methods when I can — e.g., &lt;code&gt;forEach&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, etc., I've found that since I learned C-style &lt;code&gt;for&lt;/code&gt; loops first, I often refer back to them when things aren't working. Similarly, as a sanity check, I often log out something before and after the loop, so I can make sure nothing truly whacky is going on like being in the wrong file, etc.&lt;/p&gt;

&lt;p&gt;At the end of the day, a loop is a loop, use what is clearest to you and others.&lt;/p&gt;

&lt;p&gt;So, we try again!&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;// same as before&lt;/span&gt;
&lt;span class="nx"&gt;chocolate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;filling&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;raspberry ganache&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 the variable a whole new array to reset.&lt;/span&gt;
&lt;span class="nx"&gt;weeklyChocolateSupplyBox&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;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;21&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;before loop&lt;/span&gt;&lt;span class="dl"&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;index&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;index&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;weeklyChocolateSupplyBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;loop number %d&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;weeklyChocolateSupplyBox&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;chocolate&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;weeklyChocolateSupplyBox&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;after loop&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;This time, we succeed. We have a box with 21 chocolates in it, as desired! Awesome.&lt;/p&gt;

&lt;h2&gt;
  
  
  Attempt 3: &lt;code&gt;for ... of&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Say that I didn't use an old-school &lt;code&gt;for&lt;/code&gt; loop: say I had gone ahead with a &lt;code&gt;for ... of&lt;/code&gt; loop — after all, I want to loop over this array and put things into it, right? This way, too, I can eliminate needing to increment the index myself, and not worry about if I forgot a condition or something. Great!&lt;/p&gt;

&lt;p&gt;So let's write the code, and use a &lt;code&gt;for ... of&lt;/code&gt; loop instead. We start off the same as before, and sketch out the skeleton of our &lt;code&gt;for&lt;/code&gt; loop.&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;chocolate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;filling&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;raspberry ganache&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 the variable a whole new array to reset.&lt;/span&gt;
&lt;span class="nx"&gt;weeklyChocolateSupplyBox&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;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;21&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;before loop&lt;/span&gt;&lt;span class="dl"&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;emptySlot&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;weeklyChocolateSupplyBox&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;emptySlot&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;emptySlot&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// Put a chocolate into our emptySlot&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;after loop&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;...but what goes inside the loop? We have an &lt;code&gt;emptySlot&lt;/code&gt; — but &lt;em&gt;no&lt;/em&gt; way to add a chocolate to it. If we ran this now, we'd just see &lt;code&gt;emptySlot undefined&lt;/code&gt; logged out 21 times. Not helpful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Attempt 4: &lt;code&gt;for ... in&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;In JavaScript, everything is an object. Arrays are too — in particular, are an object created by the &lt;code&gt;Array&lt;/code&gt; constructor. By definition, they have a &lt;code&gt;length&lt;/code&gt; property, and numeric, ordered keys.&lt;/p&gt;

&lt;p&gt;There's another kind of &lt;code&gt;for&lt;/code&gt; loop we haven't tried: &lt;code&gt;for ... in&lt;/code&gt;, which loops over the properties of an object. For something like an object literal, it loops over the property names; for an array, it loops over the indices. A little weird, but if you think about it, that seems sort of reasonable — we can use both a string key and an array index to set the value, and then later access that value by the key, right?&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;dog&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;Simon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&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="na"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&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;someNumbers&lt;/span&gt; &lt;span class="o"&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;1&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="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;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;dog&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 'name', then 'age', then 'weight'&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// 'Simon', then 13, then 50&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;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;someNumbers&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;someNumbers key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// '0', then '1', then '2'&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;someNumbers value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;someNumbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// 3, then 1, then 4&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, cool, nothing too interesting there, except for maybe being able to do that with arrays as well.&lt;/p&gt;

&lt;p&gt;So, let's try the chocolate experiment again. The normal &lt;code&gt;for&lt;/code&gt; loop worked — let's try the same thing but with a &lt;code&gt;for ... in&lt;/code&gt; loop, and we can use the index to add it to the array like before.&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;chocolate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;filling&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;raspberry ganache&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 the variable a whole new array to reset.&lt;/span&gt;
&lt;span class="nx"&gt;weeklyChocolateSupplyBox&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;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;21&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;before loop&lt;/span&gt;&lt;span class="dl"&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;emptySlotIndex&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;weeklyChocolateSupplyBox&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;emptySlotIndex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;emptySlotIndex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;weeklyChocolateSupplyBox&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;emptySlotIndex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;chocolate&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;after loop&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;This time, we see &lt;code&gt;before loop&lt;/code&gt; and &lt;code&gt;after loop&lt;/code&gt;, and ... literally nothing else.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's the difference?
&lt;/h2&gt;

&lt;p&gt;So, we tried a number of things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;map&lt;/code&gt;: failed -- did nothing&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;for ... of&lt;/code&gt; loop: failed -- no way to add a chocolate&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;for ... in&lt;/code&gt; loop: failed -- never even looped!&lt;/li&gt;
&lt;li&gt;basic &lt;code&gt;for&lt;/code&gt; loop: worked!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of this answers the question, though: why does a &lt;code&gt;for&lt;/code&gt; loop work and the other options fail, with &lt;code&gt;for ... in&lt;/code&gt; never looping?&lt;/p&gt;

&lt;p&gt;The answer lies in the specification of JavaScript itself.&lt;/p&gt;

&lt;p&gt;The Array constructor &lt;em&gt;does&lt;/em&gt; create an &lt;code&gt;Array&lt;/code&gt; object and set its &lt;code&gt;length&lt;/code&gt; to be the (single, numeric) value given&lt;sup&gt;1&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;What it does &lt;em&gt;not&lt;/em&gt; do, though, is set the indices (which are just keys, remember, which happen to be numbers) on the array 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="c1"&gt;// This is about what happens:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;length&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="c1"&gt;// NOT this:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;badNewArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;length&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you've ever tried to remove something from an object — truly get rid of it, not just give it an &lt;code&gt;undefined&lt;/code&gt; value, but remove the property &lt;em&gt;entirely&lt;/em&gt; — you know that &lt;code&gt;chocolate['filling'] = undefined&lt;/code&gt; won't cut it. The property will still be there, just with &lt;code&gt;undefined&lt;/code&gt; as its value.&lt;/p&gt;

&lt;p&gt;To remove a property, you have to &lt;code&gt;delete&lt;/code&gt; it: &lt;code&gt;delete chocolate['filling'];&lt;/code&gt;. After that, if you inspect the object, there will be &lt;em&gt;no&lt;/em&gt; key called &lt;code&gt;filling&lt;/code&gt; present. If we looked at its keys, we would not see &lt;code&gt;filling&lt;/code&gt; listed.&lt;/p&gt;

&lt;p&gt;So, what happens if you &lt;code&gt;delete&lt;/code&gt; an index from an 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;someOtherArray&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;value at 0&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;value at 1&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;value at 2&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;someOtherArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ["value at 0", "value at 1", "value at 2"]&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;someOtherArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 3&lt;/span&gt;

&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;someOtherArray&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="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;someOtherArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; still 3&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;someOtherArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Chrome:  ["value at 0", empty, "value at 2"]&lt;/span&gt;
&lt;span class="c1"&gt;// Firefox: ["value at 0", &amp;lt;1 empty slot&amp;gt;, "value at 2"]&lt;/span&gt;
&lt;span class="c1"&gt;// Safari:  ["value at 0", 2: "value at 2"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each browser shows you the same thing, just differently: an array with length three and only two things in it, at 0 and 2. There's nothing at index 1 anymore — because there is &lt;em&gt;no&lt;/em&gt; index 1. Each array still has a length of 3.&lt;/p&gt;

&lt;p&gt;This explains why &lt;code&gt;for ... in&lt;/code&gt; failed so badly: the &lt;code&gt;for ... in&lt;/code&gt; loop works over the keys of an object: there were no keys (indices) for it to enumerate over. Similarly, if we had looped above, both before and after deleting the index, we would have gone into the loop 3 times before deleting the index, and twice after its deletion.&lt;/p&gt;

&lt;h2&gt;
  
  
  A not-so-well-known symbol
&lt;/h2&gt;

&lt;p&gt;Here's another clue: &lt;code&gt;[...new Array(3)]&lt;/code&gt; does what we had probably originally expected — and gives us &lt;code&gt;[undefined, undefined, undefined]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The answer is &lt;strong&gt;iterators&lt;/strong&gt;; specifically, the value of the &lt;code&gt;Symbol.iterator&lt;/code&gt; on an object. (&lt;code&gt;Symbol&lt;/code&gt;s are a JavaScript primitive whose value is unique, and are often used as identifiers — much like atoms in other languages.)&lt;/p&gt;

&lt;p&gt;If an object has a &lt;code&gt;Symbol.iterator&lt;/code&gt;, that object is iterABLE: it has an iterATOR, an object that adheres to the &lt;em&gt;iterator&lt;/em&gt; protocol. Iterators are very neat and &lt;em&gt;very&lt;/em&gt; powerful — they're the guts behind &lt;code&gt;async&lt;/code&gt;, &lt;code&gt;await&lt;/code&gt;, generators, promises, the spread operator, &lt;code&gt;for ... of&lt;/code&gt;, etc; they allow for entering and exiting different execution contexts asynchronously.&lt;/p&gt;

&lt;p&gt;For our purposes, though, it's enough to know that an iterator essentially keeps track of your place in a loop. Many JavaScript objects have a default iterator — arrays, as well as anything else that you can spread (use &lt;code&gt;...&lt;/code&gt; as above).&lt;/p&gt;

&lt;p&gt;In particular, the default iterator specification&lt;sup&gt;2&lt;/sup&gt; says something like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Start with &lt;code&gt;index = 0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;As long as &lt;code&gt;index&lt;/code&gt; is less than &lt;code&gt;array.length&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Yield&lt;/code&gt; the value of &lt;code&gt;array[index]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Increment the index, &lt;code&gt;index += 1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Keep going&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;small&gt;Lots of other array methods use similar logic — e.g., &lt;code&gt;toString&lt;/code&gt; uses &lt;code&gt;join&lt;/code&gt;, which has a similar algorithm.&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;What do you get when you access a property that isn't on an object? In some languages, it wouldn't compile at all; in JavaScript, however, you don't get an error, you just get &lt;code&gt;undefined&lt;/code&gt; — which, of course, can also be the value if the key &lt;em&gt;is&lt;/em&gt; there.&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;withKeyAndUndefined&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;apples&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;pears&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;withKeyAndValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;apples&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;pears&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;99&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;withoutKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;pears&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;74&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;withKeyAndUndefined&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;apples&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; undefined&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;withKeyAndValue&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;apples&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;     &lt;span class="c1"&gt;// =&amp;gt; 12;&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;withoutKey&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;apples&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;          &lt;span class="c1"&gt;// =&amp;gt; undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As for &lt;code&gt;map&lt;/code&gt; failing as well?&lt;/p&gt;

&lt;p&gt;Well... The specification&lt;sup&gt;3&lt;/sup&gt; for &lt;code&gt;map&lt;/code&gt; (and &lt;code&gt;forEach&lt;/code&gt; and other similar methods) spells out that the callback given is &lt;em&gt;only&lt;/em&gt; executed for those values "which are not missing" — that is, non-empty slots or where the indices are defined (so, nowhere right after construction).&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;yetAnotherArray&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;Array&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="nx"&gt;yetAnotherArray&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;value&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="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="s1"&gt;never gonna happen&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;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="c1"&gt;// now we put something in every spot&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;value&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="nx"&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;now, this will show "null": &lt;/span&gt;&lt;span class="dl"&gt;'&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="k"&gt;return&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;Meanwhile, our basic &lt;code&gt;for&lt;/code&gt;-loop worked right off the bat: because &lt;em&gt;we&lt;/em&gt; were creating those indices by setting a value under that key, the same way I can do &lt;code&gt;const dog = {name: 'Simon'}; dog.favoriteFood = 'peanut butter';&lt;/code&gt; without &lt;code&gt;favoriteFood&lt;/code&gt; ever having been defined as being on the original 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;array&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;Array&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="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;index&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;index&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// does 'index' exist? Yes! It's its own variable, after all&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;index&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&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="s2"&gt;`before: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; in array?`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="k"&gt;in&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;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&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;whee&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`after: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; in array?`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;array&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;There is, conveniently, a method to &lt;code&gt;fill&lt;/code&gt; an array with any value. We can use that here, too.&lt;/p&gt;

&lt;p&gt;For a simple case, we can just do &lt;code&gt;new Array(5).fill(chocolate)&lt;/code&gt;; for anything more complex, though, we need first to &lt;code&gt;fill&lt;/code&gt; the array with something — anything, even &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&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="nx"&gt;weeklyChocolateSupplyBox&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;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chocolate&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;weeklyChocolateSupplyBox&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;rangeFrom_1_to_10&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;Array&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="nx"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&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;_null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;index&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;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;rangeFrom_1_to_10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember, though, that what we actually end up with here is 21 references to the same chocolate — if we melt &lt;em&gt;one&lt;/em&gt; chocolate, they all melt, as what we really did was put the same identical chocolate into every slot through some truly spectacular quantum confectionary. (Chocolate, however, seemed much more enjoyable than an array of strings or numbers.)&lt;/p&gt;




&lt;ol&gt;
    &lt;li id="foot_1"&gt;
        &lt;a href="https://www.ecma-international.org/ecma-262/6.0/#sec-array-len"&gt;
        Array constructor specification
        &lt;/a&gt;
    &lt;/li&gt;
    &lt;li id="foot_2"&gt;
        &lt;a href="https://www.ecma-international.org/ecma-262/6.0/#sec-%arrayiteratorprototype%.next"&gt;
        Iterator specification
        &lt;/a&gt;
    &lt;/li&gt;
    &lt;li id="foot_3"&gt;
        &lt;a href="https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.map"&gt;
        &lt;code&gt;map&lt;/code&gt; specification
        &lt;/a&gt;
    &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>arrays</category>
      <category>deepdive</category>
    </item>
    <item>
      <title>Possessive Quantifiers -- or "Hey, that's MY match!"</title>
      <dc:creator>Max Cerrina</dc:creator>
      <pubDate>Sat, 01 Sep 2018 01:06:03 +0000</pubDate>
      <link>https://dev.to/alephnaught2tog/possessive-quantifiers----or-hey-thats-my-match-1fd1</link>
      <guid>https://dev.to/alephnaught2tog/possessive-quantifiers----or-hey-thats-my-match-1fd1</guid>
      <description>&lt;p&gt;While messing around with parsing the &lt;a href="https://dev.to/devteam/state-of-the-web-data---call-for-analysis-2o75"&gt;Dev.To survey&lt;/a&gt;, I found myself with a particularly clear-cut example of a possessive quantifier in a regular expression -- one of the rare(r) pockets of regex syntax, and one I've never really gotten, so I was excited to finally see an example of it "in the wild" that made it all click for me.&lt;/p&gt;

&lt;p&gt;First, an extremely whirlwind regular expression breakdown: a &lt;em&gt;regular expression&lt;/em&gt; is a string written with a special syntax that is used to describe what a string it tests can have.&lt;/p&gt;

&lt;p&gt;A good example of this is a regex for an &lt;code&gt;rgb&lt;/code&gt; color in CSS: we could write something that checks a given string against every single possible &lt;code&gt;rgb&lt;/code&gt; color -- so &lt;code&gt;rgb(0,0,0)&lt;/code&gt;, &lt;code&gt;rgb(0,0,1)&lt;/code&gt;, all the way up to &lt;code&gt;rgb(255,255,255)&lt;/code&gt; -- but boy, wouldn't it be easier if we could just describe what one looks like? &lt;/p&gt;

&lt;p&gt;We know it starts with &lt;code&gt;rgb&lt;/code&gt;, then some parentheses, and that inside those parentheses will be three numbers. A regular expression lets you do just that -- you get something you can test strings against, to see if they match. Your regular expression for an rgb color would describe just that: an rgb color starts with &lt;code&gt;rgb&lt;/code&gt;, then opens the parentheses, and that inside those parentheses will be three numbers, and then we close the parenthesis. If we translated that literally to regular expressions, it wouldn't be a perfect regex for an RGB color, but it would be a good start -- it would definitely reject things with no numbers, with no parentheses, that didn't start with &lt;code&gt;rgb&lt;/code&gt;, etc. &lt;/p&gt;

&lt;h3&gt;
  
  
  Quick notes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Regexes are signified by delimiters, often two &lt;code&gt;/&lt;/code&gt;s: so &lt;code&gt;/apple/&lt;/code&gt; is a regex whose characters are the same as the word "apple", but &lt;code&gt;apple&lt;/code&gt; isn't a regular expression, it's just a string.&lt;/li&gt;
&lt;li&gt;When I talk about a regex &lt;em&gt;matching&lt;/em&gt; below, I am going to use that to mean the whole string being tested: e.g., &lt;code&gt;/a/&lt;/code&gt; matches &lt;code&gt;a&lt;/code&gt; and not &lt;code&gt;apple&lt;/code&gt;; &lt;code&gt;/\d+/&lt;/code&gt; matches &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;92819281&lt;/code&gt;, but does not match &lt;code&gt;ag3&lt;/code&gt;. &lt;em&gt;Finding a match&lt;/em&gt; refers to finding SOME string portion which it matches inside a bigger string -- &lt;code&gt;/apple/&lt;/code&gt; matches "apple", and finds a match in "oranges and apples". &lt;/li&gt;
&lt;li&gt;To try these, I suggest using &lt;a href="https://regex101.com/"&gt;Regex101&lt;/a&gt;, because it has the PHP option enabled by default, and it also has syntax highlighting, a debugger, etc. &lt;strong&gt;JavaScript WILL NOT WORK for the main examples below.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Quantifiers
&lt;/h2&gt;

&lt;p&gt;In regexes, &lt;em&gt;quantifiers&lt;/em&gt; lets you say how many of something you want. &lt;/p&gt;

&lt;p&gt;The most common ones are: &lt;code&gt;?&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;+&lt;/code&gt;, and the literals, which are one number in curly braces, or two comma-separated numbers in curly braces. We're just going to talk about the first three today.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;?&lt;/code&gt; means the unit can show up 0 or 1 time: the unit is optional, but if it is there, it can only be there once. &lt;code&gt;/apples?/&lt;/code&gt; matches both &lt;code&gt;apple&lt;/code&gt; and &lt;code&gt;apples&lt;/code&gt;, but not &lt;code&gt;applesssss&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;*&lt;/code&gt; means the unit can how up 0 times (not at all), or any other number of times: 0 is fine, 1 is fine, 15000 is fine. &lt;code&gt;/apples*/&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;+&lt;/code&gt; means a unit is required to be present at least once, but it's OK if it shows up &lt;em&gt;more&lt;/em&gt; than once, too.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most frequently, you will see &lt;code&gt;*&lt;/code&gt; and &lt;code&gt;+&lt;/code&gt; with a &lt;em&gt;wildcard&lt;/em&gt; like &lt;code&gt;.&lt;/code&gt; (which can match anything that is not a linebreak), or &lt;em&gt;classes&lt;/em&gt; (a group of characters that are allowed in one spot -- &lt;code&gt;/[a-z]/&lt;/code&gt; means all characters from &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;z&lt;/code&gt;, &lt;code&gt;/[xyz]/&lt;/code&gt; is only those three letters, etc. Some common character classes have a shorthand notation -- for example, &lt;code&gt;/\d/&lt;/code&gt; is the same as &lt;code&gt;/[0-9]/&lt;/code&gt;: any character from the character &lt;code&gt;0&lt;/code&gt; to the character &lt;code&gt;9&lt;/code&gt;; &lt;code&gt;/\s/&lt;/code&gt; usually[1] matches at least spaces or tabs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/[a-z]\d?/&lt;/code&gt; means we want one letter, and maybe a number after -- &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;a2&lt;/code&gt; would match, but &lt;code&gt;a331&lt;/code&gt; would fail.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/[a-z]\d*/&lt;/code&gt; means any or no numbers are fine -- &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;a12345&lt;/code&gt; would both match. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/[a-z]\d+/&lt;/code&gt; means 1 to many numbers are fine -- &lt;code&gt;a12&lt;/code&gt; would match, but &lt;code&gt;a&lt;/code&gt; would fail.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It makes sense that we see these most with wildcards or classes -- if we are matching more than one of something, odds are high that we are talking about a broader category than one character. &lt;code&gt;/[a-z]+/&lt;/code&gt; could describe just about most words -- "at least one, and possibly many, letters". Note that they don't have to be the SAME letter -- &lt;code&gt;/[a-z]+/&lt;/code&gt; &lt;em&gt;can&lt;/em&gt; match "aaaaaaah", but could also match "kslqm".&lt;/p&gt;

&lt;h2&gt;
  
  
  Possessive quantifiers
&lt;/h2&gt;

&lt;p&gt;Some languages let you combine them to make a &lt;del&gt;super ultra fancy hardcore&lt;/del&gt; other quantifiers, like to make a possessive quantifier or a lazy quantifier. &lt;/p&gt;

&lt;p&gt;A possessive quantifier looks like &lt;code&gt;/\d*+/&lt;/code&gt; or &lt;code&gt;/\d++/&lt;/code&gt; -- it means "when you match this, refuse to give any characters back to match."&lt;/p&gt;

&lt;p&gt;Perfectly clear, right? Yeah... no.&lt;/p&gt;

&lt;p&gt;OK, remember how I said I had an example? So let's do this. I had wanted to start processing the survey results, and wanted to start off by stripping out a bunch of stuff from the data itself and getting it into a format that I wanted, like a bunch of &lt;code&gt;insert&lt;/code&gt; statements or something. &lt;/p&gt;

&lt;p&gt;So, for the headers, I wanted to ditch the numbers, specifically.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Which is your primary browser for development    2. Which of the following technologies have you made use of?    2. Which of the following technologies have you made use of?    3. How old 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;While I was at it, I knew I would want to also wanted to ditch the whitespace and punctuation, so I started tinkering. &lt;/p&gt;

&lt;p&gt;Like with a lot of other things, I try and start with something that I know will work, which for a regex is usually just making sure I can start where I want and stop where I want. I could see the questions were numbered, and that the numbers had periods after them, then some whitespace, then after the whole question there was more whitespace before the next number. I used "not a digit" for most of the question, because it's faster and more accurate[2] for what I wanted (at least here -- I'm making a pretty massive assumption, that none of the questions contain numbers).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://regex101.com/r/nGrjKq/3/"&gt;Here is the regex&lt;/a&gt; I ended up with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# link: https://regex101.com/r/nGrjKq/3/
/(?x)
\d+     # at least one digit
\.      # a period
\s      # a space
[^\d]+  # something that isn't a digit -- at least one, and maybe more than one
\s+     # a space -- at least one, and maybe more than one
/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, that works great. &lt;/p&gt;

&lt;p&gt;So why would I try a possessive quantifier? &lt;/p&gt;

&lt;p&gt;I could be clever and say it's because I know they're usually faster (which is true) -- but honestly, it's because I hit the &lt;code&gt;+&lt;/code&gt; twice on accident and it failed, and I was confused. &lt;/p&gt;

&lt;p&gt;Confused about what? Well, &lt;a href="https://regex101.com/r/zM15hD/1/"&gt;the regex below&lt;/a&gt; will fail on the same string we were testing above, whereas the first one will work. There's a second &lt;code&gt;+&lt;/code&gt; on line 5 -- in the first version, it is &lt;code&gt;[^\d]+&lt;/code&gt;, below, it is &lt;code&gt;[^\d]++&lt;/code&gt;. (Oofda.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# failing version: https://regex101.com/r/zM15hD/1/ 
/(?x)
\d+     # at least one digit
\.      # a period
\s      # a space
[^\d]++ # something that isn't a digit -- at least one, and maybe more than one
\s+     # a space -- at least one, and maybe more than one
/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With the above version, we fail completely: no matches are found. Because the possessive quantifier won't give any back -- &lt;code&gt;+&lt;/code&gt; is greedy by default, so it keeps on chomping until it fails. When it fails, the regex engine backtracks, if it can, to see if it can find a match by doing so -- to backtrack, it gives up what it has consumed so far, in hopes of making a match. &lt;/p&gt;

&lt;p&gt;Uh, what?&lt;/p&gt;

&lt;h2&gt;
  
  
  "Don't give any back"
&lt;/h2&gt;

&lt;p&gt;When a regular expression engine tries to match, it goes as long as what it finds is accepted by its current state -- which is a fancy way of saying "it goes until it can't any more, because it's out of charaters or fails completely." If you play around on Regex101, you'll see pretty fast that when you write one that doesn't match, the step count is often way higher than when it does match -- because the engine will keep going through the string, hoping to find a location it can match from. If it were trying to find the phrase "Dev.To" in Hamlet, it would have a tooooon of steps -- because it would search everything, hoping to find it starting at the next character since it hadn't yet. &lt;/p&gt;

&lt;p&gt;As it goes, it consumes things. Once something has been consumed, it's not able to be used again if things are going well -- if it fails, though, the engine will start to backtrack to try and find a match. (This is often one of the biggest issues in terms of regex efficiency, by the way.) &lt;/p&gt;

&lt;p&gt;In our example -- we want a bunch of stuff that isn't a digit, then at least one space. When the regex engine walks down the string, it gobbles up eeeeeverything that isn't a digit until it finds one -- in our test string, that would be the &lt;code&gt;2&lt;/code&gt;. Now, though, it needs a space to match -- so it backtracks, since "not a digit" could certainly be a space character, and sure enough, it finds some, and uses those to match the &lt;code&gt;\s+&lt;/code&gt;, and matches fine. &lt;/p&gt;

&lt;p&gt;When we use the possessive modifier -- we don't allow it to backtrack. So the same thing happens: it walks, it gobbles up everything that isn't a digit and it hits that &lt;code&gt;2&lt;/code&gt;, but needs a space to match. Since we used the &lt;code&gt;++&lt;/code&gt;, it can't backtrack, so it fails to match -- it consumed those spaces already.&lt;/p&gt;

&lt;h2&gt;
  
  
  Moral of the story
&lt;/h2&gt;

&lt;p&gt;Sharing is caring! (Er, probably not the right one here, huh.)&lt;/p&gt;

&lt;p&gt;What I ended up with a little tweaking was &lt;a href="https://regex101.com/r/7lFnEs/3"&gt;this regex&lt;/a&gt; -- similar, but a little more specific, and only a few steps more at least for this test. I ended up ditching the spaces before the next digit -- that is easily removed with &lt;code&gt;trim&lt;/code&gt;, decided to keep the possessive quantifier because I rarely use them, and I got to use a lookahead, which I just really enjoy for no good reason.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# link: https://regex101.com/r/7lFnEs/3
(?x) 
\d+      # at least one digit
\.       # a period
\s       # a space
[^\d]++  # not a digit, 1 or more of them
(?=\d)   # until we see a digit ahead
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;[1] I say "usually" because you can change how some things behave, including that class, and because different regex flavors behave differently across languages.&lt;/p&gt;

&lt;p&gt;[2] I used &lt;code&gt;/[^\d]+/&lt;/code&gt; -- "not a digit, at least once but also could be many times" -- because if I just did a straightup wildcard, the engine would just chomp on down to the word &lt;code&gt;old&lt;/code&gt;, and I'd have one big old match, which is the opposite of what I wanted. Since I knew the next question started with a digit, I wanted to grab everything AFTER the question number that WASN'T a number -- because it would be part of the question I was on. Basically, you think of the question as delimited by the numbers. Also, negation is often faster. &lt;/p&gt;

</description>
      <category>regularexpressions</category>
    </item>
    <item>
      <title>ELI5: Why cast to an interface?</title>
      <dc:creator>Max Cerrina</dc:creator>
      <pubDate>Sun, 10 Dec 2017 16:51:12 +0000</pubDate>
      <link>https://dev.to/alephnaught2tog/eli5-why-cast-to-an-interface-1gi</link>
      <guid>https://dev.to/alephnaught2tog/eli5-why-cast-to-an-interface-1gi</guid>
      <description>&lt;p&gt;So, I am super new to OOP and Java--specifically, just finishing up my first semester of it.&lt;/p&gt;

&lt;p&gt;I understand interfaces are akin to contracts, and understand the uses of them; similarly, I understand casting, etc. I understand you &lt;em&gt;can&lt;/em&gt; cast to an interface, but I don't get why you would want to. I've checked a number of books, a number of sites, read some arguments on the topic on SO, and have yet to actually get the point of doing so.&lt;/p&gt;

&lt;p&gt;Specifically: if you had an interface called &lt;code&gt;Forecastable&lt;/code&gt; and two classes, &lt;code&gt;Weather&lt;/code&gt; and &lt;code&gt;StockMarket&lt;/code&gt;, both of which implemented &lt;code&gt;Forecastable&lt;/code&gt;--and thus &lt;em&gt;must&lt;/em&gt; have the methods of the interface, and thus any object instantiated in them has access to those methods by definition--then what would the actual use be of casting either some &lt;code&gt;Weather&lt;/code&gt; or &lt;code&gt;StockMarket&lt;/code&gt; object be to &lt;code&gt;Forecastable&lt;/code&gt;, if they can both already access those methods inherently?&lt;/p&gt;

</description>
      <category>oop</category>
      <category>java</category>
      <category>explainlikeimfive</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
