<?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: Allen Reinmeyer</title>
    <description>The latest articles on DEV Community by Allen Reinmeyer (@areinmeyer).</description>
    <link>https://dev.to/areinmeyer</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%2F58829%2Fb0c63dce-daaa-4ee1-badc-5dfbe4a008b1.jpeg</url>
      <title>DEV Community: Allen Reinmeyer</title>
      <link>https://dev.to/areinmeyer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/areinmeyer"/>
    <language>en</language>
    <item>
      <title>Getting Started with Deno</title>
      <dc:creator>Allen Reinmeyer</dc:creator>
      <pubDate>Sat, 18 Jul 2020 19:41:22 +0000</pubDate>
      <link>https://dev.to/areinmeyer/getting-started-with-deno-1b6a</link>
      <guid>https://dev.to/areinmeyer/getting-started-with-deno-1b6a</guid>
      <description>&lt;p&gt;This will be the first in a series of posts looking at Deno, the new runtime environment written in Rust. These posts will introduce what Deno is, and why it might be of interest to developers. We'll explore getting a server up and running too with Deno.&lt;/p&gt;

&lt;p&gt;Future posts in the series hope to cover these topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Explore a more complex example and using TypeScript&lt;/li&gt;
&lt;li&gt;Dig into the standard modules for Deno&lt;/li&gt;
&lt;li&gt;show what external code is available for Deno and how to use it&lt;/li&gt;
&lt;li&gt;Explore how Rust and Javascript interact together in Deno&lt;/li&gt;
&lt;li&gt;Explore creating executables for replacements to bash scripts&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is Deno?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://deno.land/v1"&gt;Deno&lt;/a&gt; is a new runtime environment for JavaScript.  One of its main creators is Ryan Dahl, who previously created NodeJS and sees this as a new approach to JavaScript running outside the browser.  There's a lot to like now that version 1.0 has just been released on May 13, 2020.  Deno is written in Rust giving it a statically-typed base on which to build from.  Applications using Deno can be written in either Typescript or JavaScript.&lt;/p&gt;

&lt;p&gt;Deno itself is a single executable file.  It acts as both a runtime and a package manager.  There are different API's included in this one executable.  If the API is already a web or JavaScript standard (fetch, setTimeout), the intention is it should interface and behave exactly the way it does in the browser.   Any API's that don't match to a web standard, like the file access functions, sit under a &lt;code&gt;Deno&lt;/code&gt; namespace.  That separation of functionality frees developers from confusing API's in Deno versus a standard, or confusing developers whether a package is overriding common behavior. Additionally, there's a standard library that you can import methods from to extend the functionality.&lt;/p&gt;

&lt;p&gt;You can install Deno by following the instructions &lt;a href="https://deno.land/manual/getting_started/installation"&gt;here&lt;/a&gt;. The overhead to installation seems small and light which is a great way to onboard developers quickly and easily without a list of dependencies, weird errors that have to be Googled.  It seems to just work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deno is not a fork of NodeJS or NPM
&lt;/h2&gt;

&lt;p&gt;Deno is very much a re-interpretation of NodeJS.  NPM packages are not supported.  Apparently, there is a compatibility layer effort underway, but one of the principals of Deno is how modules and third-party code is handled. For starters, Deno has a binding layer called &lt;code&gt;ops&lt;/code&gt; that map to JavaScript promises so async/await is supported with the first release. These promises are actually built with Rust's Futures and behavior is mapped to a JavaScript promise.&lt;/p&gt;

&lt;p&gt;Did you catch the sentence above that &lt;em&gt;NPM packages are not supported&lt;/em&gt;?  Deno uses URLs to import external code into a project.  Those external URLs are locations for &lt;a href="https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/"&gt;ES Modules&lt;/a&gt;.  Deno does not ask you to install an external library as Node does into node_modules folders.  It does have a local caching system so the modules are not externally sourced every time they are referenced. But the caching is system-based, so each project does not store its own dependencies. There's no package.json either or a central configuration file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deno tools
&lt;/h2&gt;

&lt;p&gt;A complaint might be that missing configuration files like &lt;code&gt;package.json&lt;/code&gt; makes it harder to view at a glance what dependencies are used in a project.  Deno provides a command-line option &lt;code&gt;deno info&lt;/code&gt; that generates a dependency graph for a file inspects all standard and third-party modules.  Since imports are URL-based, this works for local files as well as remote files.  Try it by executing the following after you install &lt;code&gt;deno&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;deno info https://deno.land/std@0.50.0/http/server.ts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Deno has an opinionated formatter too that you can use by invoking &lt;code&gt;deno fmt&lt;/code&gt; on a file or project folder.  Since the code is TypeScript/JavaScript you can use your own formatter, but this is a nice addition for completeness.&lt;/p&gt;

&lt;p&gt;A bundler and installer are included as well that compile a larger set of files into either a single JavaScript bundle or executable.  The installer is especially intriguing as the documentation suggests this might be a way to write command-line interfaces in JavaScript instead of bash or python.  This is perhaps one of the more intriguing aspects of Deno for me.  I may write something in bash (or modify more likely) once every six months or so.  I seldom remember the syntax for the &lt;code&gt;if&lt;/code&gt; statements, so doing real work in bash is slow for me.  If I could do it in either JS or TS, I'd be more productive and likely to actually script some things that I do repetitively because I won't feel like I have to always reference docs or Stack Overflow on bash scripting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stay outta my sandbox!
&lt;/h2&gt;

&lt;p&gt;Your Deno project runs in its own sandbox.  By default, the sandbox is extremely limited on permissions.  So beyond import statements for external module loading, you must specify that your Deno code can create network connections.  You must specify that your Deno project can access the filesystem.  Further to that, you can even specify read-only or write-only access and even list out the files or directories that can be read or written.  That means no longer wondering what nested npm packages might be trying to do.  Mining bitcoin, stealing passwords, downloading malware can't happen in Deno unless you allow it.  This intentional security design is a huge boost to developers wanting to write secure code.  As it stands, there are probably consultants out there that can specialize in securing NodeJS or Docker projects given all the nasty ways that malicious agents can sneak into either running Docker containers or NodeJS projects that get built from basic tutorials and shipped out to production.  While some may see this as extremely limiting and cumbersome to specify every permission needed, as a developer who's built NodeJS apps for a large company concerned with security, having a secure-by-design application running saves a lot of heartache down the road when your app is ready to go to production and has to pass security scans and reviews.&lt;/p&gt;

&lt;h2&gt;
  
  
  A simple web server in Deno
&lt;/h2&gt;

&lt;p&gt;As many developers will explore Deno as a comparison to NodeJS, let's take a look at how to start up a simple web server.&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;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;listenAndServe&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://deno.land/std@0.50.0/http/server.ts&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="nx"&gt;stripLeadingSlash&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="kr"&gt;string&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="k"&gt;return&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;startsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&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="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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;router&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="kr"&gt;string&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="k"&gt;switch&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="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Hi! Try adding paths to the url to see different messages!\n`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sorry, I can't help you!&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;listenAndServe&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;9000&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="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="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;stripLeadingSlash&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;router&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;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;respond&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;body&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;While this is a simple example, you can see it's not complex to get a server up and running.  Let's walk through this to explain better.&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;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;listenAndServe&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://deno.land/std@0.50.0/http/server.ts&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 is how Deno's standard library functions get imported.  If we left off the &lt;code&gt;@0.50.0&lt;/code&gt; part of the url, we'd be pulling from whatever is in the default branch (probably &lt;code&gt;master&lt;/code&gt;).  That doesn't matter for our purposes, but it's a good practice to version your url references so breaking changes aren't introduced once you are building and deploying applications.&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;listenAndServe&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;9000&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="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="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;stripLeadingSlash&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;router&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;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;respond&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;body&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;The function &lt;code&gt;listenAndServe&lt;/code&gt; opens a connection on the port passed in and any requests that arrive on that port then get handled by the callback defined with that function. Deno has options already to handle TLS (https) traffic, but in our simple case, we just pass the function a port value and then the callback to tell Deno what to do with each request that comes in.  In our case, we strip the &lt;code&gt;url&lt;/code&gt; of it's leading slash and then pass that value to a function to determine what the body of the response will be.  There's nothing Deno-specific about those helper functions.  In fact, you may have noticed that although Deno is written in Typescript, in this case, the code is just plain old JavaScript.  Deno supports either TypeScript or JavaScript so I have the freedom to test out quick prototypes in JavaScript and then change my extension to &lt;code&gt;.ts&lt;/code&gt; and define types as I need to.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I run this?
&lt;/h3&gt;

&lt;p&gt;You should have Deno installed locally via one of &lt;a href="https://deno.land/manual/getting_started/installation"&gt;these&lt;/a&gt; methods.  Then assuming the above code is saved in a file called &lt;code&gt;server.ts&lt;/code&gt; (Or you cloned my repo &lt;a href="https://github.com/areinmeyer/deno-playground"&gt;here&lt;/a&gt; and are in the root folder) you execute the following command in your shell of choice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;deno run server.ts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Did you get an error?  Remember back to me mentioning that Deno runs in a very protective sandbox?  We are telling Deno to set up a network connection and starting a server.  So we need to explicitly tell Deno that we allow it to access network commands by using the flag &lt;code&gt;--allow-net&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;deno run &lt;span class="nt"&gt;--allow-net&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0.0.0.0 server.ts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That means that Deno can only access localhost (0.0.0.0).  If it tries to go to &lt;a href="https://my.bitcoin.miner"&gt;https://my.bitcoin.miner&lt;/a&gt; all outgoing requests will fail.  This works for local file access as well, where you may want to allow a temp or limited set of folders read/write access and no more.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Deno has a lot of promise.  There's some confusion on my part how the third-party modules, standard library, and &lt;code&gt;deno&lt;/code&gt; executable will all stay in sync and versioning between the three will work.  While the &lt;code&gt;deno&lt;/code&gt; executable is now version 1.1,2, the standard library is still on version 0.59.  There are several pieces too that are hidden and only accessible under an &lt;code&gt;--unstable&lt;/code&gt; flag.&lt;/p&gt;

&lt;p&gt;The interplay between Rust, wasm, and JavaScript will be interesting to watch too.  The core pieces of deno are Rust-based, so will there be a time that we can refer to Rust crates directly?  That time may be now, as Deno publishes a few Rust crates but I'm not familiar with Rust enough to know if other Rust programs could use those and benefit from them.  That would be a big help as many npm modules currently aren't usable as they use the CommonJS module format.  Will that dampen development as users get frustrated with re-implementing working npm modules into a Deno-friendly format?&lt;/p&gt;

&lt;p&gt;Deno should be a big contender in the developer landscape in the coming months.  The attention to security, stability, and performance are always good goals to have.  Developers have embraced the ideals it has, so as more and more features are turned on and interest grows, it will likely be a good-to-know toolkit for developers looking to build either API's or CLI's.&lt;/p&gt;

</description>
      <category>deno</category>
      <category>learninpublic</category>
      <category>javascript</category>
    </item>
    <item>
      <title>JavaScript Sets</title>
      <dc:creator>Allen Reinmeyer</dc:creator>
      <pubDate>Tue, 31 Mar 2020 00:00:00 +0000</pubDate>
      <link>https://dev.to/areinmeyer/javascript-sets-2jdd</link>
      <guid>https://dev.to/areinmeyer/javascript-sets-2jdd</guid>
      <description>&lt;p&gt;&lt;a href="https://unsplash.com/photos/Hys5qHaDbZQ"&gt;Photo by Chris Lawton on Unsplash&lt;/a&gt;&lt;br&gt;
&lt;em&gt;This post originally appeared on &lt;a href="https://areinmeyer.dev/blog/sets"&gt;areinmeyer.dev&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A goal this year has been to explore more nooks and crannies of JavaScript.  I had never been exposed to Sets until I worked on the Advent of Code 2019 and came across some solutions that used them effectively.  So now that I've explored &lt;a href="https://areinmeyer.dev/maps"&gt;Maps&lt;/a&gt; I want to take a deeper dive into Sets to see if they can be useful to me in my daily programming.&lt;/p&gt;

&lt;p&gt;Sets are collections of unique values. Sets share a lot of similarities with Maps as they were introduced at the same time. The values in a Set can be any primitive or Object, and a mix of any type.&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;mixed&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="nx"&gt;mixed&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;mixed&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;mixed&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="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="nx"&gt;mixed&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;one&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="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;mixed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//Set { '1', 1, [ 1, 2 ], { one: 1 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating Sets
&lt;/h2&gt;

&lt;p&gt;Sets have many of the same properties that Maps do and are created similarly.&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;s&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="c1"&gt;//An empty Set&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&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="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="c1"&gt;//A Set populated from an Array&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&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="c1"&gt;//An Array from a Set&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;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Set { 1,2,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;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//[ 1,2,3 ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Manipulating Sets
&lt;/h2&gt;

&lt;p&gt;You can add and delete elements from a Set.  The &lt;code&gt;add()&lt;/code&gt; method returns the new Set with the added element. The Set is also mutated so the return value need not be captured.  The &lt;code&gt;delete()&lt;/code&gt; however, returns whether the Set contained the element requested to be deleted as &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; as well as mutating the Set.  Be careful with these differences!  I might expect that the mutated Set is always returned and try to capture that in a new variable, but that would lead to a nasty bug in your code.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;has()&lt;/code&gt; method checks whether the element is present in the Set or not and returns &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;.  There's a &lt;code&gt;clear()&lt;/code&gt; method as well, that removes all the elements from the Set.  Using &lt;code&gt;clear()&lt;/code&gt; does not seem too useful?  I cannot think of a good example in which I would want to keep using the same Set over and over but instead of creating a new object would want to clear it instead.  Maybe there are performance considerations?&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding
&lt;/h3&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;s&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;duplicateOfS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;s&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//Set { 1, 2, 3, 4}&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;duplicateOfS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//Set { 1, 2, 3, 4}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;itHas4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;itHas4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="c1"&gt;//true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Deleting
&lt;/h3&gt;



&lt;div class="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;is4Deleted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;is4Deleted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//true because the Set contained 4&lt;/span&gt;
&lt;span class="nx"&gt;is4Deleted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;is4Deleted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//false because 4 was previously deleted from the Set&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;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Set { 1,2,3 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The number of items in a Set are easily determined by using the &lt;code&gt;size&lt;/code&gt; property.  This returns an Integer relating to the number of elements in the Set.&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;s&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="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="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="c1"&gt;//3&lt;/span&gt;
&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="c1"&gt;//0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Iterating through a Set
&lt;/h2&gt;

&lt;p&gt;Like Maps, Sets have a plethora of ways to iterate over the values.  The &lt;code&gt;keys()&lt;/code&gt; and &lt;code&gt;values()&lt;/code&gt; methods are both present, though, for Sets, they are equivalent since Sets don't store key/value pairs.  There is the &lt;code&gt;entries()&lt;/code&gt; method that exposes a 2 element array to be consistent with &lt;code&gt;Map.entries()&lt;/code&gt; though both elements in the array are the same value.  The default iterator returns the next item in the Set. The order of insertion is preserved in any of the iterator methods.&lt;/p&gt;

&lt;h3&gt;
  
  
  Default iterator
&lt;/h3&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;s&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="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;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="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;item&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;s&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;item&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//2&lt;/span&gt;
&lt;span class="c1"&gt;//4&lt;/span&gt;
&lt;span class="c1"&gt;//6&lt;/span&gt;
&lt;span class="c1"&gt;//8&lt;/span&gt;
&lt;span class="c1"&gt;//10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This appears to be the simplest and cleanest method for iterating.  It's intuitive with the other iterators for Arrays, Objects, and Maps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Iterating with keys()
&lt;/h3&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;s&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="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;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;//values() could replace keys() here without changes to the output&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;item&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&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;item&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//2&lt;/span&gt;
&lt;span class="c1"&gt;//4&lt;/span&gt;
&lt;span class="c1"&gt;//6&lt;/span&gt;
&lt;span class="c1"&gt;//8&lt;/span&gt;
&lt;span class="c1"&gt;//10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I think the &lt;code&gt;keys()&lt;/code&gt; and &lt;code&gt;values()&lt;/code&gt; methods here are just present for consistency with Maps.  I do not see any benefit in using this way to iterate over the other ways.  The &lt;code&gt;keys()&lt;/code&gt; method is really just syntactic sugar to convert the Set into an Array.&lt;/p&gt;

&lt;h3&gt;
  
  
  Iterating with entries()
&lt;/h3&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;s&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="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;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="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="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;of&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;entries&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;item&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//2&lt;/span&gt;
&lt;span class="c1"&gt;//4&lt;/span&gt;
&lt;span class="c1"&gt;//6&lt;/span&gt;
&lt;span class="c1"&gt;//8&lt;/span&gt;
&lt;span class="c1"&gt;//10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is fine, but could be confusing syntax because you have to wrap the current element (in the example, &lt;code&gt;item&lt;/code&gt;) in an array because &lt;code&gt;entries()&lt;/code&gt; returns 2 values in an Array.  You also have to call the method explicitly, whereas the default iterator mentioned earlier doesn't have either the array or the method call.  The Array methods of &lt;code&gt;map()&lt;/code&gt; and &lt;code&gt;filter()&lt;/code&gt; are not available, though converting to an Array is possible to gain those methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sets killer feature
&lt;/h2&gt;

&lt;p&gt;The killer feature for Sets is that it is comprised only of unique elements.  I will discuss some quirks with equality shortly, but first, let us look at how we can take an Array and turn it into a Set that contains only the unique elements.&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;fullArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;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;2&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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&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;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;fullArray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//15&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;s&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="nx"&gt;fullArray&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;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//5&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;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//Set {1,2,3,4,5}&lt;/span&gt;
&lt;span class="nx"&gt;s&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="c1"&gt;//5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's it.  Creating a new Set with an Array will remove all the duplicates.  Any subsequent adds of an existing value will not change the size of the Set.  Even if you would prefer to not use Sets in your project often, you could create a simplistic utility function like the following:&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;dedupe&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="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="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="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;This function converts an existing Array into a Set, removing any duplicates and then converts the Set back into an array using the spread operator. The example is stripped down for simplicity.  Likely any production level code would want to validate that the parameter is actually an Array.&lt;/p&gt;

&lt;h3&gt;
  
  
  Equality limitations with Sets
&lt;/h3&gt;

&lt;p&gt;Let's discuss some possible limitations though with assuming Sets will always dedupe any types of Arrays.  For the most part, the triple equality test (&lt;code&gt;===&lt;/code&gt;) is used, so Objects that contain the exact same properties won't be considered equal.  But &lt;code&gt;NaN&lt;/code&gt; in this case does equal &lt;code&gt;NaN&lt;/code&gt;.  Usually, that is not the case, as you can easily see yourself if you type &lt;code&gt;NaN !== NaN&lt;/code&gt; into a node or browser console.  But Sets will only contain 1 element set to &lt;code&gt;NaN&lt;/code&gt;.  So our &lt;code&gt;dedupe&lt;/code&gt; function above will not create an Array of only unique objects unless those objects actually point to the same Object references.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tidbits
&lt;/h2&gt;

&lt;p&gt;An interesting note about Sets is that unlike Maps, Sets have no accessor method.  No find, index or other similar methods exist for Sets.  The only way to access values once they are added is to either iterate over the Set or, more likely, to convert the Set back into an Array and use one of the Array built-in methods.&lt;/p&gt;

&lt;p&gt;There's also a lot of examples I found that talk up the mathematical benefits of using Sets, like finding unions, intersections, etc. between multiple Sets.  A Set in mathematical terms does not contain duplicates, so if you're working in a field that adheres to those principles, Sets could be a good data structure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use
&lt;/h2&gt;

&lt;p&gt;As noted previously, Sets are a really nice way to get a unique list of primitives like Integer or String.  They become less useful when dealing with a list of Arrays or Objects since equality in Objects is not about the Object properties but the reference itself.  In a prior project, we had issues with users creating widgets with the same name.  There was no referential integrity issue (The widget name was not a key as a UUID was created instead), but it became confusing if multiple users created a widget with the same name over and over.  Using Sets, we could have done a validation check on the library by gathering up all the name properties and creating a Set, validating that the new widget name wasn't already taken.  Conversion to Arrays and back into Sets is simple to do, so there's a lot of benefit to switching back and forth depending on the use case in the code between Arrays and Sets. This seems to be a great addition to the JavaScript landscape and one that I will reach for now more frequently now that I know more about them!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>es6</category>
      <category>learninpublic</category>
    </item>
    <item>
      <title>JavaScript Maps</title>
      <dc:creator>Allen Reinmeyer</dc:creator>
      <pubDate>Thu, 19 Mar 2020 13:51:51 +0000</pubDate>
      <link>https://dev.to/areinmeyer/javascript-maps-59bj</link>
      <guid>https://dev.to/areinmeyer/javascript-maps-59bj</guid>
      <description>&lt;p&gt;&lt;a href="https://unsplash.com/photos/_SFJhRPzJHs"&gt;Photo by NASA on Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post originally appeared on &lt;a href="https://areinmeyer.dev/blog/maps"&gt;https://areinmeyer.dev&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A goal this year has been to explore more nooks and crannies of JavaScript.  I've never used Maps in production code and had not really come across them much until recently.  Maps may not seem very useful above and beyond Objects, but there are a few key features that can make Maps useful in some circumstances. Maps were introduced in &lt;a href="http://es6-features.org/#MapDataStructure"&gt;ES6&lt;/a&gt; and have support in most recent browsers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Map
&lt;/h2&gt;

&lt;p&gt;Maps can be created in 2 different ways.  Either call the empty constructor&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;thisIsEmpty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="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;thisIsEmpty&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Or you can pre-populate the Map from another Map or Array.&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;fromArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&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;fromArray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Map { 'a' =&amp;gt; 'foo', 'b' =&amp;gt; 'foo', 'c' =&amp;gt; 'foo' }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Map contents
&lt;/h2&gt;

&lt;p&gt;Maps get and set values with a &lt;code&gt;get&lt;/code&gt; or a &lt;code&gt;set&lt;/code&gt; method on the Map instance.&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;let&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Map { 'foo' =&amp;gt; 123 }&lt;/span&gt;
&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//1&lt;/span&gt;
&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//123&lt;/span&gt;

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


&lt;p&gt;A nice feature is the &lt;code&gt;has&lt;/code&gt; method.  The &lt;code&gt;has&lt;/code&gt; allows the code to check if a property exists in the Map and returns &lt;code&gt;undefined&lt;/code&gt; if it's not present.  This can be useful when you have a Map that may not always have keys present.  The syntax seems easier to understand then chaining checks on an Object.&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;let&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//true&lt;/span&gt;
&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//false&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj&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="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;123&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;obj&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&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;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&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;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Maps can use any value for a key like a function, object or any primitive, unlike Objects that only allow a String or Symbol.&lt;br&gt;
That means that the keys of a Map could 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;const&lt;/span&gt; &lt;span class="nx"&gt;myFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is a function!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//"This is a function!"&lt;/span&gt;

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


&lt;p&gt;How useful is this?  To be honest, having functions or Objects as keys do not seem like a terribly common use case.  There are some interesting applications of storing counts or some cached values as the value of a Map where an Object is a key.  Then you can store information about the Object and associate the data but not have to store the data in the Object itself. That allows the data to be loosely associated with the actual Object.  If the data being associated with the Object becomes unnecessary it can be easily deleted without trying to modify the object.&lt;/p&gt;

&lt;p&gt;But in that case, there's a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"&gt;WeakMap&lt;/a&gt; that is likely the better option for the previous case.  Objects get garbage-collected after they are out of scope and cannot be referenced anymore.  But Maps hold onto their references of Objects and so Objects that are a key of a Map aren't garbage collected.  WeakMaps behave the same as Maps, except their hold onto Objects that are used as keys are weak (hence the name!) and so allow the garbage collection to remove the reference to the Object from the WeakMap as well.  That means the size of your WeakMap could unexpectedly change if your Object key suddenly gets garbage-collected.&lt;/p&gt;

&lt;p&gt;A good example of the above scenario would be keeping track of a list of users engaged in a chat and displaying the count of users somewhere on the screen.  As users come into the chat room, you might add the user object as the key and maybe a temporary nickname to a WeakMap, using the &lt;code&gt;size&lt;/code&gt; property to display the active users in chat.  As the user leaves the chat, assuming they leave the app, the WeakMap would allow the user object to be released and the &lt;code&gt;size&lt;/code&gt; would update automatically.  That may not be the best implementation, but it is an example of how one might use Maps/WeakMaps with Objects as keys.&lt;/p&gt;
&lt;h2&gt;
  
  
  Map Size
&lt;/h2&gt;

&lt;p&gt;Maps have a property &lt;code&gt;size&lt;/code&gt; that will report the number of keys in the Map.  So determining the number of items in the Map is always just one line of code.&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;let&lt;/span&gt; &lt;span class="nx"&gt;mapsize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="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="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="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="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;c&lt;/span&gt;&lt;span class="dl"&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="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;mapsize&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is a great feature of Maps.  Arrays have the &lt;code&gt;length&lt;/code&gt; property, which is also a one-liner.  But Objects don't have a built-in method for determining the length or size of the Object and has to be calculated manually.  It still can be one-line, but it involves first getting the keys from the object and then determining the length of the keys array.&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;let&lt;/span&gt; &lt;span class="nx"&gt;objsize&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;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="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="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;c&lt;/span&gt;&lt;span class="dl"&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="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="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;objsize&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;//3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Order Retention and iteration
&lt;/h2&gt;

&lt;p&gt;Maps retain their order of insertion, so retrieving the list of keys, values or entries is always deterministic.  Objects can largely behave the same way the last few years, depending on the JS engine you are using, but that's only if you have the same types of keys in your Object.  If the Object contains a mix of strings and symbols, there's no guarantee of order preservation, and in fact, you have 2 separate methods to return the keys.&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sym&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sym&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bol&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bol&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;sym&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;first&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;bol&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;second&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;third&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fourth&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;stringKeys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//[ 'foo', 'bar' ]&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;symKeys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getOwnPropertySymbols&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//[ Symbol(sym), Symbol(bol) ]&lt;/span&gt;

&lt;span class="c1"&gt;//But with Maps...&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mixedMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;mixedMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sym&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;first&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;mixedMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&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="s2"&gt;second&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;mixedMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bol&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;third&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;mixedMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&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="s2"&gt;fourth&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;mixedMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="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;key&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&lt;/span&gt;
&lt;span class="c1"&gt;//Symbol(sym) first&lt;/span&gt;
&lt;span class="c1"&gt;//foo second&lt;/span&gt;
&lt;span class="c1"&gt;//Symbol(bol) third&lt;/span&gt;
&lt;span class="c1"&gt;//bar fourth&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;As seen in the preceding example, you can iterate over entries with the &lt;code&gt;forEach&lt;/code&gt; method, which takes a callback function as an argument, allowing both key and value as parameters.  Note that value is the first parameter in the callback.  &lt;code&gt;forEach&lt;/code&gt; returns void, so sadly it can't be chained with any other functions.  The &lt;code&gt;keys()&lt;/code&gt; and &lt;code&gt;values()&lt;/code&gt; methods are also present and behave much in the same manner as the related Object methods.&lt;/p&gt;

&lt;p&gt;Another way to iterate is to use the &lt;code&gt;entries&lt;/code&gt; method.  Using the preceding &lt;code&gt;mixedMap&lt;/code&gt; again we could use &lt;code&gt;entries&lt;/code&gt; 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="k"&gt;for&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;entry&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;mixedMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;entries&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;entry&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//Output&lt;/span&gt;
&lt;span class="c1"&gt;//Symbol(sym) first&lt;/span&gt;
&lt;span class="c1"&gt;//foo second&lt;/span&gt;
&lt;span class="c1"&gt;//Symbol(bol) third&lt;/span&gt;
&lt;span class="c1"&gt;//bar fourth&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Another(!) way to iterate is using the default iterator.&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;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;mixedMap&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;key&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;My personal preference is in either the &lt;code&gt;forEach&lt;/code&gt; or default iterator with a for loop.  I think they balance terseness as well as patterns that are recognizable based on other Object and Array methods.  Reliable order may not always be a concern, but when it is, Maps seem to provide the only way to have confidence in that order of insertion being preserved, especially if there is a chance that keys will have different data types.&lt;/p&gt;

&lt;p&gt;Objects can only be iterated on by first retrieving the keys (or possibly values) of the Object and iterating on those.  If your data structure is an Array of Objects or a nested Array, you also have the option of using the &lt;code&gt;map&lt;/code&gt; method that is built-in with Arrays.&lt;/p&gt;
&lt;h2&gt;
  
  
  Performance
&lt;/h2&gt;

&lt;p&gt;MDN mentions that Maps have better performance over Objects in insertion and deletion. In a naive but simple test, this proved out.  Running 1,000,000 insertions and deletions in the same Map and Object, I saw these times reported.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;th&gt;Map Time (ms)&lt;/th&gt;
&lt;th&gt;Object Time (ms)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Insertion&lt;/td&gt;
&lt;td&gt;149&lt;/td&gt;
&lt;td&gt;150&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deletion&lt;/td&gt;
&lt;td&gt;167&lt;/td&gt;
&lt;td&gt;486&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Here's the code I used.  Feel free to point out any shortcomings! While there are some differences in time on insertion, the deletion can't be accounted for by timer of my machine or insignificant differences.  I ran it several times, and each run reported roughly the same times, within a few milliseconds of each.  The insertion times often were negligible, but the deletion was always a significant difference.&lt;/p&gt;


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



&lt;h2&gt;
  
  
  Drawbacks
&lt;/h2&gt;

&lt;p&gt;You can't &lt;code&gt;map&lt;/code&gt; or &lt;code&gt;filter&lt;/code&gt; a Map.  To do that, you would have to convert the Map into an Array or Object first, then use the built-in functions.  The resulting Array or Object could then be turned back into a Map if so desired.  Whatever gains noted above though likely are lost in doing a conversion back and forth from Map to Object or Array.  Maps have an overhead of learning too most likely as it is a newer feature of the language that may not be widely adopted by teams.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use Maps over Objects?
&lt;/h2&gt;

&lt;p&gt;The biggest draw to using Maps over an Object is the benefit of using something like an Object or Date as the key instead of resorting to a string or Symbol.  Being able to quickly and easily see the size of the Map without calling a function is also useful. The &lt;code&gt;has&lt;/code&gt; method associated with a Map is a nice interface for checking if the key is present in the Map.  If you are doing a fair amount of deletions in Objects, Maps might also be more performant.&lt;/p&gt;

&lt;p&gt;The reality is Maps are probably not something that is going to be used every day by most developers, so there is definitely a mental overhead of introducing it into a shared environment.  Built-in iterables for Map and a property checking of &lt;code&gt;has&lt;/code&gt; is beneficial in many uses though, so I definitely will be reaching for Maps now that I know more about them.  It's also a great way to share knowledge with a team and introduce them to this new data structure.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>learninpublic</category>
    </item>
    <item>
      <title>JavaScript's tricky Object mutation</title>
      <dc:creator>Allen Reinmeyer</dc:creator>
      <pubDate>Thu, 20 Feb 2020 01:32:25 +0000</pubDate>
      <link>https://dev.to/areinmeyer/javascript-s-tricky-object-mutation-4bff</link>
      <guid>https://dev.to/areinmeyer/javascript-s-tricky-object-mutation-4bff</guid>
      <description>&lt;p&gt;&lt;em&gt;This post originally appeared on &lt;a href="https://areinmeyer.dev/blog/jsobjects/"&gt;areinmeyer.dev&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One "trick" to JavaScript that used to produce a lot of errors for me was the difference in assigning primitives to variables versus assigning Objects to variables.  But, like a magician's sleight of hand, the "trick" disappears when you understand the sleight-of-hand.&lt;/p&gt;

&lt;p&gt;Let's explain the problem with a couple of examples.&lt;/p&gt;

&lt;p&gt;When you declare a variable and assign it a primitive value, you do something 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;let&lt;/span&gt; &lt;span class="nx"&gt;a&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&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="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;//b is 2, a still is 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But when you do the same thing with Objects, this happens:&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;a&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="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;
&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="c1"&gt;//b is { "foo": 20, "bar": 2 }, a ALSO is { "foo": 20, "bar": 2 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Wait, what!?&lt;br&gt;
&lt;a href="https://i.giphy.com/media/CuQ4VJQjl1BsY/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/CuQ4VJQjl1BsY/giphy.gif" alt="Shia Magic GIF"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Assigning primitives to variables
&lt;/h3&gt;

&lt;p&gt;In most cases, when you assign what's considered a primitive value (numbers, strings, symbols, booleans) to a variable, you are assigning the value.  Unless you're using the &lt;code&gt;const&lt;/code&gt; keyword in declaring the variable, you can change the value of the variable 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;let&lt;/span&gt; &lt;span class="nx"&gt;a&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;one&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;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//"one"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can't though do 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;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;one&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;a&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="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;l&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&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="c1"&gt;//"one"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But you can do 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;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;//alternatively, a++&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;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The distinction may be subtle.  The variable &lt;code&gt;a&lt;/code&gt; points to a primitive, and that primitive value can be reassigned (since we used &lt;code&gt;let&lt;/code&gt;).  The primitive itself can't be changed.  The second example above is trying to do that.  In the first and third examples, we are changing the value of what &lt;code&gt;a&lt;/code&gt; is pointing at.  Don't think of the third example as incrementing &lt;code&gt;a&lt;/code&gt;, instead think of it as changing &lt;code&gt;a&lt;/code&gt; to be the result of &lt;code&gt;a + 1&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Assigning Objects to variables
&lt;/h3&gt;

&lt;p&gt;The magic appears when assigning Objects to variables. Changing an element in an Object or an Array is valid syntax and common.  &lt;/p&gt;

&lt;p&gt;Let's look at a few Object assignment examples:&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;array&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="s2"&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="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&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="mi"&gt;0&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="s2"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&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="c1"&gt;//"c", "b"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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="s2"&gt;B&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;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//"A", "B"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;
&lt;span class="nx"&gt;newArray&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="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newArray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//"A", "b"&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;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//"A", "b"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that the contents of Arrays (which are a specific type of Object) can be mutated.  Using &lt;code&gt;const&lt;/code&gt; in the second example results in a "TypeError: Assignment to constant variable", as that replaces what &lt;code&gt;array&lt;/code&gt; is assigned. That violates the concept of &lt;code&gt;const&lt;/code&gt;, which prevents a re-assignment of values (or Objects) to another value or Object.&lt;/p&gt;

&lt;p&gt;So what's happening in the third example?  Are we creating pointers to Objects?  Are Objects created and passed around by-reference?&lt;/p&gt;

&lt;h3&gt;
  
  
  Sharing is &lt;del&gt;caring&lt;/del&gt; confusing
&lt;/h3&gt;

&lt;p&gt;We won't dive into memory management, whether variables are passed by &lt;a href="https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference"&gt;reference&lt;/a&gt; or &lt;a href="https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_value"&gt;value&lt;/a&gt;, because I don't feel confident to explain it that well yet. 😜 My limited understanding at this point is that Objects get created on the heap in memory, and then a pointer to that location is stored on the memory stack, the place where JavaScript wants to get variables. There's a lot of nuances, and the distinction of Call-by-Reference and &lt;a href="https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing"&gt;Call-by-Share&lt;/a&gt; is not one I can articulate.  &lt;/p&gt;

&lt;p&gt;When Objects are created and then referenced by multiple variables like in the examples previously, what occurs is the variables point to the same Object in memory.  That's why this first example below returns true, while the second example returns false.  The Objects in the first example point to the same Object, while the second example has created two separate Objects. Doing a simple assignment of a variable that is pointing to an Object makes both variables point to the same Object and does not create a second, separate Object.&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;//Both a and b point to the same Object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&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="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&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;a&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Both a and b point to different Objects&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&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="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&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="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  So what's a dev gotta do?
&lt;/h3&gt;

&lt;p&gt;There are several avenues your code can take, depending on the type of Objects you are using.  We can copy the Object into another Object.  A new way that's been added in TC39 Stage 4 is the Object Rest/Spread Properties.  It uses the &lt;code&gt;...&lt;/code&gt; spread syntax that's become common in recent years with destructuring and retrieving nested values from Objects.  Our above example becomes:&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;let&lt;/span&gt; &lt;span class="nx"&gt;a&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="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&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="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="c1"&gt;//b is { "foo": 20, "bar": 2 }, a REMAINS { "foo": 1, "bar": 2 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The same outcome can be achieved by using &lt;code&gt;Object.assign&lt;/code&gt;.  That creates a new Object.&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;let&lt;/span&gt; &lt;span class="nx"&gt;a&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="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;assign&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="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="c1"&gt;//b is { "foo": 20, "bar": 2 }, a REMAINS { "foo": 1, "bar": 2 } &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note here that assign takes an empty Object.  Object.assign mutates the first parameter as well as returning an Object.  You can pass as many Objects as you want to assign, but as you add Objects to the right of the list, those take precedence over the Objects to the left.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Hang on, more problems ahead!
&lt;/h3&gt;

&lt;p&gt;There's some problems with the spread operator or the Object.assign that you need to be aware of.  Are you working with an Object that has nested Objects?  Well, get ready, those aren't copied fully by either of the above methods!  Nested Objects are still shared by the original Object. Only the top-level (or shallow) keys are truly copied to the new Object.&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;let&lt;/span&gt; &lt;span class="nx"&gt;a&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="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&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="s2"&gt;baz&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&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="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;assign&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="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;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;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//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;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//1&lt;/span&gt;
&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="c1"&gt;//b is { "foo": 20, "bar": 2, {"foo": 20 } }, a is also { "foo": 1, "bar": 2 , {"foo": 20 } } &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To avoid the problem in the previous example, you would have to do this:&lt;br&gt;
&lt;code&gt;let b = JSON.parse(JSON.stringify(a))&lt;/code&gt;&lt;br&gt;
But that works if you are using very simple data types.  Dates, functions, Maps, Sets, all would not be copied over as you would expect them to be copied over.  &lt;/p&gt;

&lt;p&gt;The best bet is to examine or use the lodash method &lt;a href="https://lodash.com/docs#cloneDeep"&gt;cloneDeep&lt;/a&gt;.  If you don't want lodash, you can do something similar, but make sure you traverse your Object all the way.  In other words, don't go it alone, use a tried and tested external library if possible.&lt;/p&gt;

&lt;h3&gt;
  
  
  The non-simple answer
&lt;/h3&gt;

&lt;p&gt;What I've started doing now when I am thinking about data structures is to try and avoid nesting Objects inside of Objects to prevent some of these accidental mutations on the original Objects. If I can keep state as local to React components or functions/classes that tends to avoids the need to have complex data structures.  If I have to have complex data structures, I try to make sure that in passing parameters to functions I'm slicing out what I need only for that function.  &lt;/p&gt;

&lt;p&gt;It is easy to fall into the trap of passing large data structures around to functions to avoid listing out 5 or 6 parameters, but when that occurs or I find myself wanting to pass large structures I stop and try to understand how I got to this point and refactor away the need to have large structures passed around or passing a long list of parameters to a function.  &lt;/p&gt;

&lt;p&gt;It's much easier to spot the "magic" now that I understand better how Objects get created and stored. The magical errors have started to vanish and because of it, my code appears to be simpler.&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;p&gt;In researching this problem, I stumbled upon several good articles and resources. The original inspiration for this article was from Dan Abramov's &lt;a href="https://justJavaScript.com/"&gt;JustJavaScript&lt;/a&gt; as I came across this very problem in old code I had written and this is my clumsy attempt to write about it and better understand it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript"&gt;MDN JavaScript Docs&lt;/a&gt; Simply a great resource for all things JavaScript&lt;/li&gt;
&lt;li&gt;&lt;a href="https://exploringjs.com/impatient-js/index.html"&gt;Dr. Axel Rauschmayer's JavaScript for Impatient Programmers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://wafy.me/tech/2016/07/02/call-by-sharing.html"&gt;Call by Sharing&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>learninpublic</category>
    </item>
    <item>
      <title>My 2020 Goals</title>
      <dc:creator>Allen Reinmeyer</dc:creator>
      <pubDate>Mon, 20 Jan 2020 03:35:09 +0000</pubDate>
      <link>https://dev.to/areinmeyer/my-2020-goals-2cmg</link>
      <guid>https://dev.to/areinmeyer/my-2020-goals-2cmg</guid>
      <description>&lt;h3&gt;
  
  
  Learning Javascript for reals
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;This post originally appeared at &lt;a href="https://areinmeyer.dev/blog/2020/"&gt;areinmeyer.dev&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I attempted to complete the wonderful &lt;a href="https://adventofcode.com/2019"&gt;Advent of Code&lt;/a&gt; this past year and got to Day 10.&lt;/p&gt;

&lt;p&gt;Well, I got to Day 8 and then used a lot of help from Reddit and Github searches to get to Day 10. 😭&lt;/p&gt;

&lt;p&gt;What did I learn? I have relied on frameworks or libraries these past 4 years as a bit of a crutch to tell myself I really knew Javascript.&lt;/p&gt;

&lt;p&gt;Is that a bad thing?  Not really.  I started a greenfield project at that point and knew some JS via jQuery and small bits and pieces doing some basic websites.  We were under a tight but do-able schedule at work and needed to show continued progress to keep things running.  A React/NodeJS set-up was the right solution (in part because our front-end UI was all moving to React anyway!).  But it required me to really level up fast not only on Javascript but also React, NodeJS, webpack, ES6 features, Docker and eventually Redux to tie into helper libraries our core team was providing as some internal scaffolding.&lt;/p&gt;

&lt;p&gt;Lots of tutorials, blog posts and heavy use of lodash, we shipped.  It's still running in production today.  I learned GraphQL as well during the process, as we were so successful in showing progress that we decided to roll some of our own solutions instead of relying on third-party vendors for some parts of the puzzle.  It's easy to reach for npm and Google to get past certain hurdles.  But as I went through Advent of Code I tried to keep my solutions basic and use Vanilla JS as much as possible without adding libraries or overhead.  I tried to do functional programming and I quickly learned I didn't have enough comfort to do that either.  I'd limit myself to trying to reference Mozilla MDN too and realized I hadn't ever used Maps, Sets, or understood the number of methods that Array or Object have built-in.&lt;/p&gt;

&lt;p&gt;Does that make me a bad developer?  No, I'd argue a lot of developers don't really use Maps or Sets too often either.  But as I thought about what I wanted to level up on in 2020, it occurred to me that instead of trying out a new language like Rust or Reason, which are both really cool time investments, I should go back to basics to focus on the area that I say I really know.  This year, I intend to do that by continuing the Advent of Code or maybe some other code katas or lessons in a similar vein. I'll probably dive into some of Axel Rauschsmayer's books (&lt;a href="https://exploringjs.com/"&gt;https://exploringjs.com/&lt;/a&gt;) as well as Dan Abramov's &lt;a href="https://justjavascript.com"&gt;https://justjavascript.com&lt;/a&gt; that he just announced.&lt;/p&gt;

&lt;h3&gt;
  
  
  All the Functions!
&lt;/h3&gt;

&lt;p&gt;My other goal is to really get a good understanding of Functional programming.  I've read different articles and blog posts, conference talks, and even have a book sitting on my virtual shelf (Functional Programming in Javascript by Dan Mantyla).  But can I explain what a monad is?  Probably not.  There are several resources that I'll use for that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.packtpub.com/web-development/functional-programming-javascript"&gt;Functional Programming in Javascript&lt;/a&gt;
The afore-mentioned book that's been sitting on my shelf for far too long&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://jrsinclair.com/"&gt;James Sinclair's blog&lt;/a&gt;
There's so much good content here.  And he references another good series from ...&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.tomharding.me/fantasy-land/"&gt;Tom Harding explaining the Fantasyland spec&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://mostly-adequate.gitbooks.io/mostly-adequate-guide/"&gt;Professor Frisby's Mostly Adequate Guide to Functional Programming&lt;/a&gt;
Brian Lonsdorf's Javascript guide to functional programming. It's available on Github and PR's are welcome.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Several of these I've read before or started, but I think it'll be a good exercise to re-read and really try to apply the lessons learned here.  Currently, I'm not writing a lot of code during my day job, so having examples and lessons to try and apply will be helpful. My goal is to try and really document my process as well.  I think doing both simultaneously won't be too challenging (i.e., a chapter a week on general Javascript and another chapter on Functional Programming).  I've read that to truly understand Functional Programming you need to learn a language like Haskell which really only allows you to use Algebraic structures and functional concepts.&lt;/p&gt;

&lt;p&gt;Feel free to send me any additional references that would be helpful. So maybe I'll be taking a detour as I go during the year to learn a new language.  Wish me luck here's to a great 2020!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>functional</category>
    </item>
    <item>
      <title>Docker commands I can't live without</title>
      <dc:creator>Allen Reinmeyer</dc:creator>
      <pubDate>Tue, 13 Aug 2019 01:31:20 +0000</pubDate>
      <link>https://dev.to/areinmeyer/docker-commands-i-can-t-live-without-29ko</link>
      <guid>https://dev.to/areinmeyer/docker-commands-i-can-t-live-without-29ko</guid>
      <description>&lt;p&gt;&lt;em&gt;This post originally appeared on &lt;a href="https://areinmeyer.dev/blog/dockertips"&gt;areinmeyer.dev&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I have used Docker since late 2015 when I started our greenfield React/NodeJS project.  Like many developers, I tried to grok enough of it to get the project up and running.  And then as we spun off new micro-services, I copied and pasted the previous implementation to the new project.  &lt;/p&gt;

&lt;p&gt;But over the last year, I've had to dig into the guts of Docker more and more.  This is the first part of two posts about what I've learned.  There are several commands or settings that I've picked up on that I think are incredibly useful.  These commands have helped increase my understanding and productivity when working with docker containers locally.&lt;/p&gt;

&lt;h3&gt;
  
  
  Docker Preferences
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Save a running container as an image
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;docker commit container image2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Docker builds layers or images for each command that you specify in the Dockerfile. This is a great way to "save" a working copy for further experimentation or other local changes.  When you run the command, the existing container and all of the changes you have made to it are stored as a new image.  This has saved me a couple of times when I started the container but maybe forgot to open a port.  &lt;/p&gt;

&lt;p&gt;There is no clean way to open a port on a running container, and if you are trying to get something working without this command you would have to start a new container with the port open and redo any changes.  &lt;/p&gt;

&lt;p&gt;Don't use this command to save an image that you want to publish.  The associated Dockerfile isn't updated so you would never be able to recreate the new image that you are creating.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Get a summary of all images/containers in Docker
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;docker system &lt;span class="nb"&gt;df&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Want to see how much memory all of your containers and images are using?  This command gives a nice summary of the space used by images, containers, and volumes.  Usually, when a docker container complains that it is out of memory, the likely true problem is that whatever you have allocated overall for Docker to consume has filled up.  Seeing what images and containers are using then can be a good starting point for freeing up space.  Though remember, you can't delete images that are being used by a container, either stopped or running!&lt;/p&gt;

&lt;h3&gt;
  
  
  See ALL images on your machine
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;docker ps &lt;span class="nt"&gt;-a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Without the &lt;code&gt;-a&lt;/code&gt; parameter, this shows you all of the running containers.  Add the parameter though, and you see all of the stopped containers.  These could be stopped containers or failed builds.  Use either the name or the container ID to then &lt;code&gt;docker rm &amp;lt;name&amp;gt;&lt;/code&gt; to remove the container.  Or &lt;code&gt;docker start &amp;lt;name&amp;gt;&lt;/code&gt; to restart a container.  Often this can be a treasure hunt of past attempts or tests that are no longer needed.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Remove all unused images
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;docker rmi &lt;span class="si"&gt;$(&lt;/span&gt;docker images &lt;span class="nt"&gt;-q&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="s2"&gt;"dangling=true"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If I was smarter about setting up cron jobs, I would have this command run about every 2 weeks.  Every time you build a docker image locally, each step in your dockerfile creates an image.  Most of the time the intermediate layers are removed but untagged images can remain with errors or interrupted builds.  Docker doesn't do a great job of cleaning up those images and they persist until either your disk fills up or you remove them.  &lt;/p&gt;

&lt;p&gt;This command gets a list of all your image ids in docker locally (&lt;code&gt;docker images -q&lt;/code&gt;) and displays only the untagged leaves (&lt;code&gt;-f "dangling=treu"&lt;/code&gt;).  Those are then removed with the &lt;code&gt;docker rmi&lt;/code&gt; command.  The nice part of this is that docker will warn and skip deleting any images that are in use from a container.  So it's a relatively safe command to run.&lt;/p&gt;

&lt;h3&gt;
  
  
  See all processes running in a container without logging into the container
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;docker top &amp;lt;name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Don't want to exec into a container just to see what's running?  Open up a tmux window or a new terminal window and run this command.  Much like UNIX's &lt;code&gt;top&lt;/code&gt;, it'll take over your window (hence my new window suggestion!) and show you processes running on the container you've specified.  Really handy for seeing what processes get spawned (or not) in the container.  Also handy to see why my Macbook's fan suddenly kicked in.  This helped start me on the path to tuning a container that was running out of memory and getting OOMKilled in kubernetes.  I could recreate locally and troubleshoot the issue.&lt;/p&gt;

&lt;p&gt;None of these commands are revolutionary or dreamed up by me.  Most I found by googling and reading the docker docs.  Maybe though I've helped you, dear reader, save a few hours by compiling them together in one shot?  Some came with little explanation, so my hope is I've now provided a better understanding of what these do.&lt;/p&gt;

</description>
      <category>docker</category>
    </item>
  </channel>
</rss>
