<?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: Donavon West</title>
    <description>The latest articles on DEV Community by Donavon West (@donavon).</description>
    <link>https://dev.to/donavon</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%2F191190%2F3db83dd4-238b-4530-b825-3e22d11aad5b.jpeg</url>
      <title>DEV Community: Donavon West</title>
      <link>https://dev.to/donavon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/donavon"/>
    <language>en</language>
    <item>
      <title>A Mental Model for Writing React Hook Components</title>
      <dc:creator>Donavon West</dc:creator>
      <pubDate>Thu, 17 Sep 2020 17:17:22 +0000</pubDate>
      <link>https://dev.to/donavon/a-mental-model-for-writing-react-hook-components-2k8f</link>
      <guid>https://dev.to/donavon/a-mental-model-for-writing-react-hook-components-2k8f</guid>
      <description>&lt;p&gt;By now we've probably all written a component that uses React hooks. Many times, however, we struggle with what hooks we need or if we need a custom hook.&lt;/p&gt;

&lt;p&gt;This article will explain my mental model or how I go about determining how to write my component. We'll do this a step at a time so you can see my process and I'll tell you what I'm thinking at each step.&lt;/p&gt;

&lt;h2&gt;
  
  
  Write the render first
&lt;/h2&gt;

&lt;p&gt;I think visually, so I like to start with what I'd like to render. This will help me determine what data elements I'm going to need.&lt;/p&gt;

&lt;p&gt;For example, if I'm writing a weather component that displays the temp and current conditions, I start with the markup.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💭 What I'm thinking...&lt;br&gt;
I want to display the current temperature and weather conditions for a given city.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It could be 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;const&lt;/span&gt; &lt;span class="nx"&gt;Weather&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;city&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;˚&lt;/span&gt;&lt;span class="nx"&gt;F&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Cool! Now I know what I want to render and in doing so, I've discovered what pieces of data that I'll need.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stub in the data
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;💭 What I'm thinking...&lt;br&gt;
I'd like to see my markup visually before I proceed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At this point, the app won't compile as there are missing variables, so often I'll mock that data so I can see things working.&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;Weather&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;72&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;conditions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Perfect&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;˚&lt;/span&gt;&lt;span class="nx"&gt;F&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see above, I've mocked &lt;code&gt;temp&lt;/code&gt; and &lt;code&gt;conditions&lt;/code&gt;. Now when I run the app I can see my component.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: In reality, your component will probably need to have a loading state (unless you're using Suspense), as the data is loaded asynchronously, but for simplicity's sake, I'm not showing that here.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If I like what I see visually, I can move on to actually getting the data. And for this, I like to encapsulate everything into a custom hook.&lt;/p&gt;

&lt;h2&gt;
  
  
  Write the call to the custom hook
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;💭 What I'm thinking...&lt;br&gt;
What will the custom hook signature look like?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We now have everything we need to write a custom hook. We know the input (i.e. &lt;code&gt;city&lt;/code&gt;) and the output (i.e. &lt;code&gt;temp&lt;/code&gt; and &lt;code&gt;conditions&lt;/code&gt;). &lt;strong&gt;This is our contract.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now let's replace the mock data in the component with a call to our yet-to-be-written custom hook. Our component will now 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;Weather&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;conditions&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useWeather&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;˚&lt;/span&gt;&lt;span class="nx"&gt;F&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Write a custom hook
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;💭 What I'm thinking...&lt;br&gt;
Now I'll write a custom hook that satisfies the contract.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With everything in place, it's time to write a hook that fulfills our contract.&lt;/p&gt;

&lt;p&gt;Here is a stubbed out custom hook called &lt;code&gt;useWeather&lt;/code&gt; which, for now, will return mocked data.&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;useWeather&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;city&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="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;72&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Perfect&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The app should still run as the mock data is coming from the custom hook.&lt;/p&gt;

&lt;p&gt;It should be noted that how we get the data in the hook is an "implementation detail". Meaning that the component shouldn't care how or where it gets the data. We could write something that fetches from a REST endpoint or uses Apollo to get the data from a GraphQL server. The point is that once we've designed the contract, it doesn't matter to the component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's recap
&lt;/h2&gt;

&lt;p&gt;When designing a component...&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start with the rendered markup&lt;/li&gt;
&lt;li&gt;Stub in the variables needed&lt;/li&gt;
&lt;li&gt;Replace the stubbed variables with a call to get the data&lt;/li&gt;
&lt;li&gt;Write a custom hook that fulfills the contract&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I find that if I follow these steps, using this as my mental model, writing React components with hooks becomes more straightforward.&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>I code using const (and why you should too)</title>
      <dc:creator>Donavon West</dc:creator>
      <pubDate>Fri, 03 Apr 2020 16:23:56 +0000</pubDate>
      <link>https://dev.to/donavon/i-code-using-const-and-why-you-should-too-4i6d</link>
      <guid>https://dev.to/donavon/i-code-using-const-and-why-you-should-too-4i6d</guid>
      <description>&lt;p&gt;There have been endless tweets and many blog posts (even this &lt;a href="https://jamie.build/const"&gt;expletive-laden rant&lt;/a&gt;) about how &lt;code&gt;const&lt;/code&gt; really isn't constant or how "It's just compiled down to &lt;code&gt;let&lt;/code&gt; by Babel anyway, so why use it?"&lt;/p&gt;

&lt;p&gt;I'm here to tell you why I ignore all of this "sound advice" and rely on &lt;code&gt;const&lt;/code&gt; as an indication of code quality.&lt;/p&gt;

&lt;h2&gt;
  
  
  Myth: const isn't constant
&lt;/h2&gt;

&lt;p&gt;The fact is you can never re-assign a &lt;code&gt;const&lt;/code&gt;. The value that's assigned to it will remain that value until the variable loses scope and is destroyed ('til death do us part?)&lt;/p&gt;

&lt;p&gt;Let's take a look.&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;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;carA&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;carB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nl"&gt;VM101&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="nx"&gt;Uncaught&lt;/span&gt; &lt;span class="nx"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Assignment&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;constant&lt;/span&gt; &lt;span class="nx"&gt;variable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the example above, you see that you &lt;strong&gt;can not&lt;/strong&gt;, re-assign &lt;code&gt;myCar&lt;/code&gt;. If you do, you get an exception.&lt;/p&gt;

&lt;h2&gt;
  
  
  Selectively assign a value to a const
&lt;/h2&gt;

&lt;p&gt;Hold on. If a &lt;code&gt;const&lt;/code&gt; is, um… constant, then how in the world can you selective assign it a value? &lt;/p&gt;

&lt;p&gt;Let's look at some code that sets a variable based on some user preference.&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;myCar&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;perferSportsCar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sportsCar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;minivan&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 code above is something that you might find in many a source code repo today. Some people point to this as a valid use case for using &lt;code&gt;let&lt;/code&gt; over &lt;code&gt;const&lt;/code&gt;. I see it as "&lt;a href="https://en.wikipedia.org/wiki/Code_smell"&gt;code smell&lt;/a&gt;". It's verbose, repetitive, and adds visual clutter.&lt;/p&gt;

&lt;p&gt;Let's try it again using a &lt;code&gt;const&lt;/code&gt; instead.&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;VM101&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="nx"&gt;Uncaught&lt;/span&gt; &lt;span class="nx"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Assignment&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;constant&lt;/span&gt; &lt;span class="nx"&gt;variable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Yeah, we all should have seen that coming. &lt;/p&gt;

&lt;p&gt;How then can we use a &lt;code&gt;const&lt;/code&gt; and what was all that talk about code smell?&lt;/p&gt;

&lt;p&gt;When I see a &lt;code&gt;let&lt;/code&gt;, it tells me that maybe there is a better way to express what you are trying to accomplish. Let's refactor the code to allow for a &lt;code&gt;const&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;perferSportsCar&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;sportsCar&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;minivan&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here we decide on a value using a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator"&gt;ternary&lt;/a&gt; and then assign it. The value of &lt;code&gt;myCar&lt;/code&gt; is never re-assigned, but its initial value is selectively determined.&lt;/p&gt;

&lt;h2&gt;
  
  
  What if we can't use a simple ternary?
&lt;/h2&gt;

&lt;p&gt;There are times when a simple ternary won't cut it. Take this common scenario.&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;myCar&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;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sportscar&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;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sportsCar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;minivan&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;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;minivan&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;suv&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;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;suv&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;luxury&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;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;luxury&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;economy&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;Based on &lt;code&gt;type&lt;/code&gt; we assign &lt;code&gt;myCar&lt;/code&gt; to one of four different cars and default to &lt;code&gt;economy&lt;/code&gt; if it's not one of the tested conditions.&lt;/p&gt;

&lt;p&gt;When I'm doing a code review and something like this and see that &lt;code&gt;let&lt;/code&gt;, it's a red flag. 🚩&lt;/p&gt;

&lt;p&gt;Here's something a little more maintainable.&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;cars&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;sportscar&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;minivan&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;suv&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;luxury&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;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cars&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;economy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What do you know, it can be expressed using a &lt;code&gt;const&lt;/code&gt; (but you knew I was going to say that, didn't you?)&lt;/p&gt;

&lt;p&gt;Are there exceptions to this? Certainly. Just the other day I wrote some code that set the value of a &lt;code&gt;let&lt;/code&gt; based on a range of numeric values.  This would have been more difficult, and make the code less maintainable if I had used a &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  If const is constant, then why can I change its value?
&lt;/h2&gt;

&lt;p&gt;If your &lt;code&gt;const&lt;/code&gt; contains an array or an object, yes, you can change its &lt;strong&gt;properties&lt;/strong&gt;, but the &lt;strong&gt;value&lt;/strong&gt; remains constant. Take this example.&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;sportsCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sportscar&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;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sportsCar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;myCar&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;sportsCar&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;myCar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;myCar&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;sportsCar&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;p&gt;Notice that we've changed (i.e. mutated) &lt;code&gt;myCar&lt;/code&gt; by setting the &lt;code&gt;color&lt;/code&gt; property to "red", yet its value (i.e. &lt;code&gt;sportsCar&lt;/code&gt;) remains the same.&lt;/p&gt;

&lt;p&gt;Think of it like this. If I buy a car and then paint it red. Is it the same car? Yes. What if I install some sweet new rims? It's still the same car. Starting to get the picture?&lt;/p&gt;

&lt;p&gt;Simply changing some aspect of my car doesn't make it a different car, just like setting &lt;code&gt;myCar.color='red'&lt;/code&gt; doesn't change the value &lt;code&gt;myCar&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: There are ways to &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze"&gt;freeze&lt;/a&gt; an object, which prevents mutation, but that's a different story.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  In conclusion
&lt;/h2&gt;

&lt;p&gt;Show me a project coded using all &lt;code&gt;const&lt;/code&gt; and I'll show you one that is in all likelihood well written.&lt;/p&gt;

&lt;p&gt;For me, &lt;code&gt;const&lt;/code&gt; vs. &lt;code&gt;let&lt;/code&gt; is less about re-assignment and more about what it says about your code. If your code can be written with all &lt;code&gt;const&lt;/code&gt; then I get a better feeling that nothing fishy 🐠 is going on. And when I see that one &lt;code&gt;let&lt;/code&gt;, it tells me that I need to examine what's going on a little further.&lt;/p&gt;

&lt;p&gt;So let (pun intended) Babel changes my &lt;code&gt;const&lt;/code&gt;'s to &lt;code&gt;let&lt;/code&gt;'s a transpile time. As many proponents of &lt;code&gt;let&lt;/code&gt; are quick to point out, we save one character per instance in our bundle size! 💃 &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>const</category>
      <category>let</category>
      <category>programming</category>
    </item>
    <item>
      <title>Math.max() without Auguments</title>
      <dc:creator>Donavon West</dc:creator>
      <pubDate>Sat, 21 Mar 2020 14:59:54 +0000</pubDate>
      <link>https://dev.to/donavon/math-max-without-auguments-eak</link>
      <guid>https://dev.to/donavon/math-max-without-auguments-eak</guid>
      <description>&lt;p&gt;I posted the following "poll" on Twitter yesterday asking what would be returned if you called JavaScript's &lt;code&gt;Math.max()&lt;/code&gt; function &lt;em&gt;without&lt;/em&gt; any arguments.&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--dGLberuf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/1229500863925932037/RrODZcsO_normal.jpg" alt='donavon "#WashYourHands" west profile image'&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        donavon "#WashYourHands" west
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        &lt;a class="comment-mentioned-user" href="https://dev.to/donavon"&gt;@donavon&lt;/a&gt;

      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P4t6ys1m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      🏆 &lt;a href="https://twitter.com/hashtag/JavaScript"&gt;#JavaScript&lt;/a&gt; Quiz time! (no cheating) 🏆&lt;br&gt;&lt;br&gt;We all know that `Math.max(1, 2, 3)` will return 3.&lt;br&gt;&lt;br&gt;But what is the result if you call `Math.max()` without any arguments. Bonus points if you can explain why in the replies.
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      21:42 PM - 20 Mar 2020
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1241117895347838989" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-reply-action.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1241117895347838989" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-retweet-action.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      2
      &lt;a href="https://twitter.com/intent/like?tweet_id=1241117895347838989" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-like-action.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
      11
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;p&gt;I gave 4 possible answers: &lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;-1&lt;/code&gt;, &lt;code&gt;0&lt;/code&gt;, and &lt;code&gt;-Infinity&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I'm just going to go ahead and spoil it for you. The answer is &lt;code&gt;-Infinity&lt;/code&gt; (about half of you got it right 👏). But why?&lt;/p&gt;

&lt;p&gt;Let's dive into the possible JavaScript-based implementations of &lt;code&gt;Math.max&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using reduce to return -Infinity
&lt;/h2&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;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;max&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;max&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;max&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kc"&gt;Infinity&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;Here we accept a single rest parameter called &lt;code&gt;values&lt;/code&gt;, which will be an array. &lt;code&gt;reduce&lt;/code&gt; is aptly named as it works to translate an array into a single value. We seed the initial &lt;code&gt;max&lt;/code&gt; value with &lt;code&gt;-Infinity&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With each iteration through the array, we return &lt;code&gt;value&lt;/code&gt; (if it is greater than &lt;code&gt;max&lt;/code&gt;) or &lt;code&gt;max&lt;/code&gt;. When all values have been compared, we return &lt;code&gt;max&lt;/code&gt; to the caller.&lt;/p&gt;

&lt;p&gt;So because we seeded &lt;code&gt;max&lt;/code&gt; with &lt;code&gt;-Infinity&lt;/code&gt; if there are zero items in the array, we return the initial value of &lt;code&gt;max&lt;/code&gt; (i.e. &lt;code&gt;-Infinity&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Let's test our code by calling it with some sample data.&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;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kc"&gt;Infinity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Should be -Infinity&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Should be -1000&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;max&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="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Should be 3&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;Yep. They all pass!&lt;/p&gt;

&lt;p&gt;This is a short and sweet implementation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using reduce to return 0 (or -1)
&lt;/h2&gt;

&lt;p&gt;If we took our code above and replaced the initial value of &lt;code&gt;-Infinity&lt;/code&gt; with &lt;code&gt;0&lt;/code&gt;, the code would still work. Or would it?&lt;/p&gt;

&lt;p&gt;Let's see this by running our tests again – changing the first one to check for zero instead.&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;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Should be 0&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Should be -1000&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;max&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="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Should be 3&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;As you see, calling &lt;code&gt;max()&lt;/code&gt; without any arguments did correctly return &lt;code&gt;0&lt;/code&gt;, but we get an error with the test for &lt;code&gt;-1000&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AssertionError: Should be -1000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Why is &lt;code&gt;max(-1000)&lt;/code&gt; failing? What do you think this is returning? It is errantly returning &lt;code&gt;0&lt;/code&gt; instead of &lt;code&gt;-1000&lt;/code&gt;. This is because a &lt;code&gt;value&lt;/code&gt; of &lt;code&gt;-1000&lt;/code&gt; is not greater than a &lt;code&gt;max&lt;/code&gt; of &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;People tend to think in terms of positive numbers. Returning &lt;code&gt;0&lt;/code&gt; would break some valid conditions as we've shown.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using reduce to return undefined
&lt;/h2&gt;

&lt;p&gt;What if we wanted to return &lt;code&gt;undefined&lt;/code&gt;? This isn't a bad idea (my 8-year-old guessed this when I "quizzed" my family) but would add complexity to our solution above.&lt;/p&gt;

&lt;p&gt;Let's take a look.&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;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;max&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;max&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;max&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;max&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You see, we would explicitly need to check if &lt;code&gt;max&lt;/code&gt; was &lt;code&gt;undefined&lt;/code&gt; inside of our loop. It works, but conditions add time and code complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  In conclusion
&lt;/h2&gt;

&lt;p&gt;We've learned that returning &lt;code&gt;-Infinity&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt; are the only possible valid conditions of the four I presented in my Twitter poll. &lt;/p&gt;

&lt;p&gt;However, &lt;code&gt;-Infinity&lt;/code&gt; greatly reduces code complexity and IMO, is the best solution.&lt;/p&gt;

&lt;h3&gt;
  
  
  Follow up from Brendan Eich
&lt;/h3&gt;

&lt;p&gt;It turns out that my guess was right! This is exactly how &lt;code&gt;Math.max&lt;/code&gt; is implemented. I received the following tweet from the father of JavaScript, Brendan Eich.&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--ki8YGwOK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/603270050556956672/T0mfRsil_normal.png" alt="BrendanEich profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        BrendanEich
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        @brendaneich
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P4t6ys1m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      &lt;a href="https://twitter.com/donavon"&gt;@donavon&lt;/a&gt; That's right. Math.max in ur-JS and ES1&amp;amp;2 took only two parameters, but ES3 generalized to compute maximum of a variable number of arguments. This motivates -∞ as the max of the empty set (0 args) basis case, as you showed.
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      18:16 PM - 21 Mar 2020
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1241428538311340034" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-reply-action.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1241428538311340034" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-retweet-action.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      0
      &lt;a href="https://twitter.com/intent/like?tweet_id=1241428538311340034" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-like-action.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
      2
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Block Promoted Tweets on Twitter using Brave</title>
      <dc:creator>Donavon West</dc:creator>
      <pubDate>Sun, 16 Feb 2020 21:20:24 +0000</pubDate>
      <link>https://dev.to/donavon/disable-promoted-tweets-in-twitter-using-brave-3e37</link>
      <guid>https://dev.to/donavon/disable-promoted-tweets-in-twitter-using-brave-3e37</guid>
      <description>&lt;p&gt;If like me, you'd like to stop seeing obnoxious "Promoted" tweets in your Twitter feed, then this article for you! &lt;/p&gt;

&lt;p&gt;I get it. Ad revenue powers the web. But in 2019, Twitter's annual revenue came to 3.46 billion U.S. dollars. So I'll sleep just fine tonight.&lt;/p&gt;

&lt;p&gt;Note that this only works if you are using Brave as your browser, but I might ask why you aren't using it already. :)&lt;/p&gt;

&lt;p&gt;Brave has a feature where you can filter HTML elements based on a CSS selector. As luck would have it, Twitter marks it's promoted tweets with a &lt;code&gt;data-testid="placementTracking"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To set this in Brave:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to Twitter.&lt;/li&gt;
&lt;li&gt;Right-click anywhere on the background and select the &lt;code&gt;Brave&lt;/code&gt; then &lt;code&gt;Block element via selector&lt;/code&gt;.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uTqaTw7n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ny3mu5x4k7xk7hbmsonr.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;A popup dialog will appear like this.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dYCNBe47--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/58d6y11pvjrvdbeueirk.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;Enter &lt;code&gt;div[data-testid="placementTracking"] article&lt;/code&gt; in the text box and click the &lt;code&gt;OK&lt;/code&gt; button.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's it! Your feed will now be free from obnoxious ads for automobile companies, mobile phones, and others.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Twitter could change it's markup at any time, making this selector invalid. If/when they do, I'll do my best to update the instructions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vYx0ojvo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3n9veh6i2v4o73c0uw45.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vYx0ojvo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3n9veh6i2v4o73c0uw45.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>twitter</category>
      <category>brave</category>
    </item>
  </channel>
</rss>
