<?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: BaasMurdo</title>
    <description>The latest articles on DEV Community by BaasMurdo (@baasmurdo).</description>
    <link>https://dev.to/baasmurdo</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%2F871448%2F2f80dda1-dab5-4ea0-9112-85b778568866.jpg</url>
      <title>DEV Community: BaasMurdo</title>
      <link>https://dev.to/baasmurdo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/baasmurdo"/>
    <language>en</language>
    <item>
      <title>Some JS Weirdness</title>
      <dc:creator>BaasMurdo</dc:creator>
      <pubDate>Tue, 19 Sep 2023 19:10:40 +0000</pubDate>
      <link>https://dev.to/baasmurdo/some-js-weirdness-3ah</link>
      <guid>https://dev.to/baasmurdo/some-js-weirdness-3ah</guid>
      <description>&lt;p&gt;In reality, the seemingly strange behavior from Javascript could be an entire series of posts.&lt;br&gt;
Before we get ahead of ourselves, let us start small and point out a couple of interesting Javascript behaviors.&lt;/p&gt;
&lt;h2&gt;
  
  
  baNaNa
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="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="o"&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="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="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// -&amp;gt; 'baNaNa'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This is an old-school joke in JavaScript, but remastered. Here's the original one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="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="o"&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;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;// -&amp;gt; 'fooNaN'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  💡 Explanation:
&lt;/h3&gt;

&lt;p&gt;The expression is evaluated as &lt;code&gt;'foo' + (+'bar')&lt;/code&gt;, which converts &lt;code&gt;'bar'&lt;/code&gt; to not a number.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.ecma-international.org/ecma-262/#sec-addition-operator-plus"&gt;&lt;strong&gt;12.8.3&lt;/strong&gt; The Addition Operator (&lt;code&gt;+&lt;/code&gt;)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.ecma-international.org/ecma-262/#sec-unary-plus-operator"&gt;12.5.6 Unary + Operator&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;---------------------------------------------&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;NaN&lt;/code&gt; is not a &lt;code&gt;NaN&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kc"&gt;NaN&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;NaN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// -&amp;gt; false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  💡 Explanation:
&lt;/h3&gt;

&lt;p&gt;The specification strictly defines the logic behind this behavior:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;If &lt;code&gt;Type(x)&lt;/code&gt; is different from &lt;code&gt;Type(y)&lt;/code&gt;, return &lt;strong&gt;false&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;Type(x)&lt;/code&gt; is Number, then

&lt;ol&gt;
&lt;li&gt;If &lt;code&gt;x&lt;/code&gt; is &lt;strong&gt;NaN&lt;/strong&gt;, return &lt;strong&gt;false&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;y&lt;/code&gt; is &lt;strong&gt;NaN&lt;/strong&gt;, return &lt;strong&gt;false&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;… … …&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;— &lt;a href="https://www.ecma-international.org/ecma-262/#sec-strict-equality-comparison"&gt;&lt;strong&gt;7.2.14&lt;/strong&gt; Strict Equality Comparison&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Following the definition of &lt;code&gt;NaN&lt;/code&gt; from the IEEE:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Four mutually exclusive relations are possible: less than, equal, greater than, and unordered. The last case arises when at least one operand is NaN. Every NaN shall compare unordered with everything, including itself.&lt;/p&gt;

&lt;p&gt;— &lt;a href="https://stackoverflow.com/questions/1565164/1573715#1573715"&gt;“What is the rationale for all comparisons returning false for IEEE754 NaN values?”&lt;/a&gt; at StackOverflow&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;---------------------------------------------&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  It's a fail
&lt;/h2&gt;

&lt;p&gt;You would not believe it, but …&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[])[&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;[]]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[])[&lt;/span&gt;&lt;span class="o"&gt;+!+&lt;/span&gt;&lt;span class="p"&gt;[]]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
  &lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;[]]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[][[]])[&lt;/span&gt;&lt;span class="o"&gt;+!+&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;[]]]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[])[&lt;/span&gt;&lt;span class="o"&gt;!+&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="p"&gt;[]];&lt;/span&gt;
&lt;span class="c1"&gt;// -&amp;gt; 'fail'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  💡 Explanation:
&lt;/h3&gt;

&lt;p&gt;By breaking that mass of symbols into pieces, we notice that the following pattern occurs often:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt; &lt;span class="c1"&gt;// -&amp;gt; 'false'&lt;/span&gt;
&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt; &lt;span class="c1"&gt;// -&amp;gt; false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So we try adding &lt;code&gt;[]&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt;. But due to a number of internal function calls (&lt;code&gt;binary + Operator&lt;/code&gt; -&amp;gt; &lt;code&gt;ToPrimitive&lt;/code&gt; -&amp;gt; &lt;code&gt;[[DefaultValue]]&lt;/code&gt;) we end up converting the right operand to a string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[].&lt;/span&gt;&lt;span class="nf"&gt;toString&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;p&gt;Thinking of a string as an array we can access its first character via &lt;code&gt;[0]&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;false&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// -&amp;gt; 'f'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rest is obvious, but the &lt;code&gt;i&lt;/code&gt; is tricky. The &lt;code&gt;i&lt;/code&gt; in &lt;code&gt;fail&lt;/code&gt; is grabbed by generating the string &lt;code&gt;'falseundefined'&lt;/code&gt; and grabbing the element on index &lt;code&gt;['10']&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;More examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;+!&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;          &lt;span class="c1"&gt;// -&amp;gt; 0&lt;/span&gt;
&lt;span class="o"&gt;+!!&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;         &lt;span class="c1"&gt;// -&amp;gt; 1&lt;/span&gt;
&lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;          &lt;span class="c1"&gt;// -&amp;gt; true&lt;/span&gt;
&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;           &lt;span class="c1"&gt;// -&amp;gt; false&lt;/span&gt;
&lt;span class="p"&gt;[][[]]&lt;/span&gt;        &lt;span class="c1"&gt;// -&amp;gt; undefined&lt;/span&gt;
&lt;span class="o"&gt;+!!&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="p"&gt;[]&lt;/span&gt;  &lt;span class="c1"&gt;// -&amp;gt; Infinity&lt;/span&gt;
&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;       &lt;span class="c1"&gt;// -&amp;gt; "[object Object]"&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;           &lt;span class="c1"&gt;// -&amp;gt; NaN&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;a href="http://patriciopalladino.com/blog/2012/08/09/non-alphanumeric-javascript.html"&gt;Brainfuck beware: JavaScript is after you!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://bluewings.github.io/en/writing-a-sentence-without-using-the-alphabet/#weird-javascript-generator"&gt;Writing a sentence without using the Alphabet&lt;/a&gt; — generate any phrase using JavaScript&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;---------------------------------------------&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Minimal value is greater than zero
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Number.MIN_VALUE&lt;/code&gt; is the smallest number, which is greater than zero:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MIN_VALUE&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// -&amp;gt; true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  💡 Explanation:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;Number.MIN_VALUE&lt;/code&gt; is &lt;code&gt;5e-324&lt;/code&gt;, i.e. the smallest positive number that can be represented within float precision, i.e. that's as close as you can get to zero. It defines the best resolution that floats can give you.&lt;/p&gt;

&lt;p&gt;Now the overall smallest value is &lt;code&gt;Number.NEGATIVE_INFINITY&lt;/code&gt; although it's not really numeric in a strict sense.&lt;/p&gt;

&lt;p&gt;— &lt;a href="https://stackoverflow.com/questions/26614728/why-is-0-less-than-number-min-value-in-javascript"&gt;“Why is &lt;code&gt;0&lt;/code&gt; less than &lt;code&gt;Number.MIN_VALUE&lt;/code&gt; in JavaScript?”&lt;/a&gt; at StackOverflow&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.ecma-international.org/ecma-262/#sec-number.min_value"&gt;&lt;strong&gt;20.1.2.9&lt;/strong&gt; Number.MIN_VALUE&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Making a percentage-based scroll to the top indicator</title>
      <dc:creator>BaasMurdo</dc:creator>
      <pubDate>Mon, 18 Sep 2023 17:29:47 +0000</pubDate>
      <link>https://dev.to/baasmurdo/making-a-percentage-based-scroll-to-the-top-indicator-94m</link>
      <guid>https://dev.to/baasmurdo/making-a-percentage-based-scroll-to-the-top-indicator-94m</guid>
      <description>&lt;p&gt;This is a fairly simple thing to get done once you know how.&lt;br&gt;
At the time that I first encountered this, I was very curious as to how this would work and how I would go about implementing this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So here is what I did&lt;/strong&gt;&lt;br&gt;
First, I had to determine how far I had scrolled &amp;amp; how far I still needed to scroll to reach the end of the page.&lt;/p&gt;

&lt;p&gt;To do this I added a function that would use either the &lt;code&gt;document.docmentElement&lt;/code&gt; or the &lt;code&gt;document.body&lt;/code&gt; depending on which had a value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Then a little bit of math&lt;/strong&gt;&lt;br&gt;
I then did a calculation in shorthand which is hard to read so I will write it out a bit better here.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(document.documentElement.scrollTop || document.body.scrollTop) / (document.documentElement.scrollHeight|| document.body.scrollHeight) - document.documentElement.clientHeight) * 100;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is not much of an improvement over the Codepen version, but it is a lot more readable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How &amp;amp; when should I trigger this function&lt;/strong&gt;&lt;br&gt;
I added an event listener on the scroll on the window &amp;amp; that would then trigger the final function to either show or hide the scroll to the top indicator.&lt;br&gt;
The event listener I added looks like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;window.addEventListener("scroll", function() {&lt;br&gt;
   const svgElement = document.getElementsByClassName('progress-circle')[0]&lt;br&gt;
  svgElement.firstElementChild.style["stroke-dashoffset"] = percentage(getScrollPercent(), 307.919);&lt;br&gt;
}, false)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As seen here the final function would return a number that I can then use to set the &lt;code&gt;stroke-dashoffset&lt;/code&gt; of the SVG, sure that makes sense. &lt;/p&gt;

&lt;p&gt;soooooo what is the 307.919 for?&lt;/p&gt;

&lt;p&gt;Well, I don't know, if someone reading this does know and can explain it in more Layman's terms, please leave a comment and I will adjust this part and you will have my thanks.&lt;br&gt;
The way I go to this is just playing around with the values and this worked without issue so I kept it in.&lt;/p&gt;

&lt;p&gt;This function would then go on to take these two values and do a calculation to determine if the page has been scrolled and by how much compared to the total available y-axis to scroll.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var t = totalValue - (totalValue * (percent / 100));&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If this resulting number is larger than 0 but smaller than 306, then the indicator will come into view in the bottom right-hand corner.&lt;br&gt;
Otherwise, it will fade away. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Finishing touches&lt;/strong&gt;&lt;br&gt;
All I really had to do now was to make everything comically large so that nothing was missed.&lt;br&gt;
Add in a simple animation or two.&lt;br&gt;
Last but not least, to make the page scroll to the top nice and smooth once the indicator has been clicked &amp;amp; here is how I did that: &lt;br&gt;
&lt;code&gt;document.getElementById('wrap').addEventListener('click', function() {&lt;br&gt;
  window.scrollTo({ top: 0, behavior: 'smooth' });&lt;br&gt;
});&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Codepen Below&lt;/strong&gt;&lt;br&gt;
Please feel free to go through the Codepen below, check the code, and play around. This was more a proof of concept to satisfy my curiosity and I would advise a lot of testing before using this in a production environment.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/Murdo/embed/NWvwzga?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JS detect mobile device</title>
      <dc:creator>BaasMurdo</dc:creator>
      <pubDate>Mon, 25 Jul 2022 11:31:00 +0000</pubDate>
      <link>https://dev.to/baasmurdo/js-detect-mobile-device-b15</link>
      <guid>https://dev.to/baasmurdo/js-detect-mobile-device-b15</guid>
      <description>&lt;p&gt;Hey there all! I guess this is kinda like my &lt;em&gt;Hello world&lt;/em&gt; of writing posts, so don't be too harsh 😄&lt;/p&gt;

&lt;h2&gt;
  
  
  Disclaimer
&lt;/h2&gt;

&lt;p&gt;I am not claiming this is the best way &amp;amp; I wouldn't call it the most complete way to check if a device is a "popular-ish" mobile device. But it is simple and should work correctly for the most part. (If you do see/find an issue, please feel free to let me know) Last but not least, I did not come up with this. I found it and will share a link &lt;em&gt;here&lt;/em&gt; if and when I find the post again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Firstly, if you want to apply styles to something depending on actual screen sizes. &lt;em&gt;&lt;strong&gt;(my opinion)&lt;/strong&gt;&lt;/em&gt; Use the built-in CSS media queries, very reliable once you understand them.&lt;/p&gt;

&lt;p&gt;The use case for this would be more if you want certain functionality to only trigger on a mobile or a desktop.&lt;/p&gt;

&lt;p&gt;Now that I have that out of the way, here is the solution and a minor explanation after.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;agentDetails&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userAgent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isMobile&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/Android|Mobi|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;agentDetails&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Hit here if the device is a "popular-ish" mobile device&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="c1"&gt;// Hit here if the device is  NOT a "popular-ish" mobile device&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Ok so let's start dissecting this...&lt;/p&gt;

&lt;p&gt;The first line &lt;code&gt;var agentDetails = navigator.userAgent&lt;/code&gt; gets the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator/userAgent"&gt;user agent data&lt;/a&gt; for the browser used.&lt;/p&gt;

&lt;p&gt;In short, this gets the &lt;code&gt;Platform, Security, OS-or-CPU, Localization, revision-version-number&lt;/code&gt; relavent to the current device. (at time of writing)&lt;br&gt;
This will contain the info we want to see and will be the White text in the Codepen below.&lt;/p&gt;

&lt;p&gt;The second important part is the &lt;code&gt;if()&lt;/code&gt; statement. The string in the &lt;code&gt;if()&lt;/code&gt; has a &lt;code&gt;/&lt;/code&gt; at the start and a &lt;code&gt;/i&lt;/code&gt; at the end (the &lt;code&gt;/i&lt;/code&gt; at the end, just makes it case-insensitive) &amp;amp; is delimited by pipes ("|").&lt;br&gt;
This creates a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"&gt;regular expresion&lt;/a&gt; of the string. (This could be an entire other series 😅)&lt;/p&gt;

&lt;p&gt;The reason we make it a regular expression is so that we can use the &lt;code&gt;.test()&lt;/code&gt; method against it which "executes a search for a match between a regular expression and a specified string." you can read more about it &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So if the user agent data returns for instance:&lt;br&gt;
"Mozilla/5.0 (iPad; CPU OS 15 5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"&lt;/p&gt;

&lt;p&gt;This would be an iPad, so it would return true as the string we are checking (user agent) &amp;amp; the regular expression both contain the word iPad.&lt;/p&gt;
&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I think that is it, as promised here is the Codepen I created, it's not fancy, it's not pretty, but it works.&lt;br&gt;
As seen in the user agent documentation, this info can be altered by the user or third-party apps. So beware, results may vary.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/Murdo/embed/VwXbBQq?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
