{"componentChunkName":"component---src-templates-tags-js","path":"/tags/javascript/","result":{"data":{"allMarkdownRemark":{"totalCount":22,"edges":[{"node":{"id":"db113d4f-1e9c-531c-82cd-4571a56e8a9a","html":"<p>Searching for items in an array has been the point of discussion for many years and debate on what is the best and optimum way for searching for objects has had many solutions, some effective and some not.</p>\n<p>However, with the all new <code class=\"language-text\">findIndex</code> method on <code class=\"language-text\">Array.prototype</code> you have the flexibility to search for objects using your own comparison callback method.</p>\n<!--more-->\n<h2 id=\"the-what\" style=\"position:relative;\"><a href=\"#the-what\" aria-label=\"the what permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The what</h2>\n<p>The <code class=\"language-text\">findIndex</code> method returns the index of the first element in an array if the callback method passed to it returns <code class=\"language-text\">true</code>, otherwise it returns <code class=\"language-text\">-1</code>.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"69284524062846080000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const isPerfectSquare = (num) => {\r\n  return num > 0 && Math.sqrt(num) % 1 === 0;\r\n}\r\n\r\nconsole.log([1, 3, 8, 9, 12].findIndex(isPerfectSquare)); // 1\r\nconsole.log([1, 6, 7, 10, 14].findIndex(isPerfectSquare)); // -1`, `69284524062846080000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token function-variable function\">isPerfectSquare</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">num</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">return</span> num <span class=\"token operator\">></span> <span class=\"token number\">0</span> <span class=\"token operator\">&amp;&amp;</span> Math<span class=\"token punctuation\">.</span><span class=\"token function\">sqrt</span><span class=\"token punctuation\">(</span>num<span class=\"token punctuation\">)</span> <span class=\"token operator\">%</span> <span class=\"token number\">1</span> <span class=\"token operator\">===</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token number\">8</span><span class=\"token punctuation\">,</span> <span class=\"token number\">9</span><span class=\"token punctuation\">,</span> <span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">findIndex</span><span class=\"token punctuation\">(</span>isPerfectSquare<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 1</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">6</span><span class=\"token punctuation\">,</span> <span class=\"token number\">7</span><span class=\"token punctuation\">,</span> <span class=\"token number\">10</span><span class=\"token punctuation\">,</span> <span class=\"token number\">14</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">findIndex</span><span class=\"token punctuation\">(</span>isPerfectSquare<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// -1</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>There are two facts you should know:</p>\n<ul>\n<li>This method does not run once it found the first matching element.</li>\n<li>It does not change the original array.</li>\n</ul>\n<h2 id=\"syntax\" style=\"position:relative;\"><a href=\"#syntax\" aria-label=\"syntax permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Syntax</h2>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"18333125617480817000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`array.findIndex(function(currentValue, index, arr), thisValue);`, `18333125617480817000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\">array<span class=\"token punctuation\">.</span><span class=\"token function\">findIndex</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">currentValue<span class=\"token punctuation\">,</span> index<span class=\"token punctuation\">,</span> arr</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span> thisValue<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<h3 id=\"parameters\" style=\"position:relative;\"><a href=\"#parameters\" aria-label=\"parameters permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Parameters</h3>\n<ul>\n<li><strong>function:</strong> This is your callback function which checks for a condition to match the element you’re after.</li>\n<li><strong>currentValue:</strong> This holds the current element’s value.</li>\n<li><strong>index:</strong> This is an optional parameter which holds the current index.</li>\n<li><strong>arr:</strong> This is also an optional parameter which holds the array that the current element belongs to.</li>\n<li><strong>thisValue:</strong> Yet another optional parameter, if a value is passed, it will be used as <code class=\"language-text\">this</code> value inside the function, otherwise <code class=\"language-text\">undefined</code> will be passed.</li>\n</ul>\n<h3 id=\"return-value\" style=\"position:relative;\"><a href=\"#return-value\" aria-label=\"return value permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Return value</h3>\n<p>It will return the index of the found item or <code class=\"language-text\">-1</code> if the callback function is not satisfied.</p>\n<h2 id=\"how-it-works-under-the-hood\" style=\"position:relative;\"><a href=\"#how-it-works-under-the-hood\" aria-label=\"how it works under the hood permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How it works under the hood?</h2>\n<p>When the <code class=\"language-text\">findIndex</code> is called with one or two arguments, the following steps are executed:</p>\n<ol>\n<li>Let <code class=\"language-text\">O</code> be ? <code class=\"language-text\">ToObject(this value)</code></li>\n<li>Let <code class=\"language-text\">len</code> be ? <code class=\"language-text\">LengthOfArrayLike(O)</code></li>\n<li>If <code class=\"language-text\">IsCallback(predicate)</code> is <code class=\"language-text\">false</code>, throw a <strong>TypeError</strong> exception</li>\n<li>Let <code class=\"language-text\">k</code> be 0</li>\n<li>Repeat, while <code class=\"language-text\">k &lt; len</code></li>\n</ol>\n<ul>\n<li>Let <code class=\"language-text\">Pk</code> be ! <code class=\"language-text\">ToString(k)</code></li>\n<li>Let <code class=\"language-text\">kValue</code> be ? <code class=\"language-text\">Get(O, Pk)</code></li>\n<li>Let <code class=\"language-text\">testResult</code> be ! <code class=\"language-text\">ToBoolean(? Call(predicate, thisArg, « kValue, k, O »))</code></li>\n<li>If <code class=\"language-text\">testResult</code> is <code class=\"language-text\">true</code>, return <code class=\"language-text\">k</code></li>\n<li>Set <code class=\"language-text\">k</code> to <code class=\"language-text\">k + 1</code></li>\n</ul>\n<ol start=\"6\">\n<li>Return <code class=\"language-text\">-1</code></li>\n</ol>","timeToRead":1,"frontmatter":{"title":"Find an item in an array with the new 'findIndex' method 🔎","unformattedDate":"2020-10-05T00:00:00.000Z","date":"Oct 05, 2020","path":"/array-findindex/","author":"Yaser Adel Mehraban","tags":["showdev","webdev","javascript","findindex"],"thumbnail":null}}},{"node":{"id":"311de7ae-f9be-5da6-b42d-e979c22a3b3e","html":"<p>It’s been a while since I last wrote about Intersection Observer V2, partly because I had a lot on my plate and I was feeling exhausted and partly because I was working on my first <a href=\"app.pluralsight.com/library/courses/web-performance-progressive-web-apps\">PluralSight course on web performance for PWAs</a>.</p>\n<p>But I am back, and this time we’re going to review how to take a selfie from your webcam using <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/MediaStream_Image_Capture_API\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Image Capture API</a>.</p>\n<!--more-->\n<h2 id=\"image-capture-api\" style=\"position:relative;\"><a href=\"#image-capture-api\" aria-label=\"image capture api permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Image Capture API</h2>\n<p>There are some really useful APIs which allow us to work with media like audio, video, etc. I will write about those too, but this time I wanted to show you how you can extract a frame from your video feed such as your webcam with a few lines of code.</p>\n<p><a href=\"https://developer.mozilla.org/en-US/docs/Web/API/MediaStream_Image_Capture_API\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Image Capture API</a> enables us to capture an image or frame from video devices. In addition to capturing data, it also allows you to retrieve information about device capability such as image size, red-eye detection and whether or not there is flash turned on.</p>\n<h2 id=\"usage\" style=\"position:relative;\"><a href=\"#usage\" aria-label=\"usage permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Usage</h2>\n<p>We need to take some actions to be able to work with media devices. First thing first we need to get a reference of the device:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"30174315677986742000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`navigator.mediaDevices.getUserMedia({ video: true })\r\n  .then(mediaStream => {\r\n    // Do something with the stream.\r\n  })`, `30174315677986742000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\">navigator<span class=\"token punctuation\">.</span>mediaDevices<span class=\"token punctuation\">.</span><span class=\"token function\">getUserMedia</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">video</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\r\n  <span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">mediaStream</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token comment\">// Do something with the stream.</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Next we need to get the visual parts of the media stream by calling <code class=\"language-text\">getVideoTracks</code> method of the <code class=\"language-text\">mediaStream</code> object:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"76983858953704200000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const track = mediaStream.getVideoTracks()[0];`, `76983858953704200000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> track <span class=\"token operator\">=</span> mediaStream<span class=\"token punctuation\">.</span><span class=\"token function\">getVideoTracks</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>Of course we’re assuming that the first item in the array is the one you want to use, but don’t worry if not, you can loop through all the tracks, find the one you need and either get it with its right index, or by calling <code class=\"language-text\">getTrackById</code> method.</p>\n<p>After we’ve got our track, it’s time to capture our image. If you wanted to configure some of the settings on your media device such as zoom level, you need to do it now before capturing our image:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"54809987753683110000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const capabilities = track.getCapabilities();\r\n// Check whether zoom is supported or not.\r\nif(!capabilities.zoom) {\r\n  return;\r\n}\r\n\r\nconst zoom = capabilities.zoom.max - capabilities.zoom.min;\r\n\r\ntrack.applyConstraints({ advanced : [{ zoom: zoom }] });`, `54809987753683110000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> capabilities <span class=\"token operator\">=</span> track<span class=\"token punctuation\">.</span><span class=\"token function\">getCapabilities</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token comment\">// Check whether zoom is supported or not.</span>\r\n<span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>capabilities<span class=\"token punctuation\">.</span>zoom<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">return</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token keyword\">const</span> zoom <span class=\"token operator\">=</span> capabilities<span class=\"token punctuation\">.</span>zoom<span class=\"token punctuation\">.</span>max <span class=\"token operator\">-</span> capabilities<span class=\"token punctuation\">.</span>zoom<span class=\"token punctuation\">.</span>min<span class=\"token punctuation\">;</span>\r\n\r\ntrack<span class=\"token punctuation\">.</span><span class=\"token function\">applyConstraints</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">advanced</span> <span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">zoom</span><span class=\"token operator\">:</span> zoom <span class=\"token punctuation\">}</span><span class=\"token punctuation\">]</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>When we’re done with settings, we can then create an instance of the <code class=\"language-text\">ImageCapture</code> object:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"17388920753778358000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let imageCapture = new ImageCapture(track);`, `17388920753778358000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">let</span> imageCapture <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">ImageCapture</span><span class=\"token punctuation\">(</span>track<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>Once that’s done, you can capture an image from your video feed by calling the <code class=\"language-text\">takePhoto</code> method:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"12082815144094839000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`imageCapture.takePhoto()\r\n  .then(blob => createImageBitmap(blob))\r\n  .then(imageBitmap => {\r\n    // do something with the photo\r\n  })\r\n  .catch(error => console.error(error));`, `12082815144094839000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\">imageCapture<span class=\"token punctuation\">.</span><span class=\"token function\">takePhoto</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\r\n  <span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">blob</span> <span class=\"token operator\">=></span> <span class=\"token function\">createImageBitmap</span><span class=\"token punctuation\">(</span>blob<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\r\n  <span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">imageBitmap</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token comment\">// do something with the photo</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\r\n  <span class=\"token punctuation\">.</span><span class=\"token function\">catch</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">error</span> <span class=\"token operator\">=></span> console<span class=\"token punctuation\">.</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"permissions\" style=\"position:relative;\"><a href=\"#permissions\" aria-label=\"permissions permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Permissions</h2>\n<p>Similar to other APIs you would need permission to be able to access the webcam on devices. If you didn’t see any pop up, make sure you give the camera permission to the site on your browser to be able to get your code working properly. You can try that with below demo.</p>\n<h2 id=\"demo\" style=\"position:relative;\"><a href=\"#demo\" aria-label=\"demo permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Demo</h2>\n<p>You can see a working example of this in my CodePen below:</p>\n<iframe height=\"265\" style=\"width: 100%;\" scrolling=\"no\" title=\"Image Capture API\" src=\"https://codepen.io/yashints/embed/preview/bGpaWKY?height=265&theme-id=light&default-tab=result\" frameborder=\"no\" loading=\"lazy\" allowtransparency=\"true\" allowfullscreen=\"true\">\r\n  See the Pen <a href='https://codepen.io/yashints/pen/bGpaWKY'>Image Capture API</a> by Yaser Adel Mehraban\r\n  (<a href='https://codepen.io/yashints'>@yashints</a>) on <a href='https://codepen.io'>CodePen</a>.\r\n</iframe>\n<p>I hope you’ve learnt a new thing or two and till next time 👋🏽.</p>","timeToRead":2,"frontmatter":{"title":"Take a selfie 🤳 using Image Capture API and a few lines of code","unformattedDate":"2020-09-06T00:00:00.000Z","date":"Sep 06, 2020","path":"/image-capture-api/","author":"Yaser Adel Mehraban","tags":["showdev","webdev","javascript","imagecaptureapi"],"thumbnail":null}}},{"node":{"id":"7764a9b0-4602-58ba-98eb-d277a36b4768","html":"<p>As I mentioned in <a href=\"https://yashints.dev/blog/2018/11/12/web-perf-4\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">one of my other posts</a>, <strong>Intersection Observer</strong> will report when an element appears in the viewport, after applying all <code class=\"language-text\">overflow</code> and <code class=\"language-text\">CSS clips</code>. However, there is no way currently to find out whether an item is being put on top of this element, or some filter is applied to it which may alter or obscure the element’s display.</p>\n<!--more-->\n<h2 id=\"context\" style=\"position:relative;\"><a href=\"#context\" aria-label=\"context permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Context</h2>\n<p><a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Intersection Observer</a> is now widely used by developers especially since Safari added support a while back and it’s now available in all major browsers. If you want to be notified when an element appears in the viewport such as an image, if you want to lazy load it you can use this API. In its most basic form the code looks like this:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"23157413180259190000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const logIfIsInViewPort = (entries) => {\r\n  for (const entry of entries) {\r\n    if (entry.isIntersecting) {\r\n      console.log(entry);\r\n    }\r\n  }\r\n};\r\n\r\nconst observer = new IntersectionObserver(logIfIsInViewPort);\r\nobserver.observe(document.querySelector('.element-selector'));`, `23157413180259190000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> <span class=\"token function-variable function\">logIfIsInViewPort</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">entries</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">const</span> entry <span class=\"token keyword\">of</span> entries<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>entry<span class=\"token punctuation\">.</span>isIntersecting<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n      console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>entry<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token punctuation\">}</span>\r\n  <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">const</span> observer <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">IntersectionObserver</span><span class=\"token punctuation\">(</span>logIfIsInViewPort<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\nobserver<span class=\"token punctuation\">.</span><span class=\"token function\">observe</span><span class=\"token punctuation\">(</span>document<span class=\"token punctuation\">.</span><span class=\"token function\">querySelector</span><span class=\"token punctuation\">(</span><span class=\"token string\">'.element-selector'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"the-problem\" style=\"position:relative;\"><a href=\"#the-problem\" aria-label=\"the problem permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The problem</h2>\n<p>As great as it sounds, this API is not perfect. For example it can’t tell you whether or not an element is covered by another content (which is called occluded), or if a filter is applied to it to hide or obscure it’s content (such as <code class=\"language-text\">opacity</code>, <code class=\"language-text\">filter</code>, <code class=\"language-text\">transform</code>, etc.).</p>\n<p>This can lead to dangerous situations where some people might do fraud or abuse the web. So <a href=\"https://w3c.github.io/IntersectionObserver/v2/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Intersection Observer V2</a> was born to prevent these types of scenarios.</p>\n<h2 id=\"intersection-observer-v2\" style=\"position:relative;\"><a href=\"#intersection-observer-v2\" aria-label=\"intersection observer v2 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Intersection Observer V2</h2>\n<p>As I said, the primary motivation for this API is to prevent those situations where a fraud might be possible, or people could abuse the user’s privacy in a way especially if the said content is put in an iframe.</p>\n<p>Intersection Observer V2 works by tracking the actual visibility of the element, just like the end user would see it. By passing an option to it’s constructor, the collection of <code class=\"language-text\">IntersectionObserverEntry</code> will contain a new boolean called <code class=\"language-text\">isVisible</code>.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"8151325527475839000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const handler = (changes) => {\r\n  for(const change in changes) {\r\n    // feature detection\r\n    if(typeof change.isVisible !== 'undefined') {\r\n      if(change.isIntersecting && change.isVisible) {\r\n        console.log(\\`Visible since \\${change.time}\\`);\r\n      } else {\r\n        // fallback to v1 if not supported\r\n        change.isVisible = true;\r\n      }\r\n    }\r\n  }\r\n}\r\n\r\nconst observer = new IntersectionObserver(handler, {\r\n  threshold: [1.0],  \r\n  trackVisibility: true, // this will give you the isVisible property\r\n  delay: 100\r\n});\r\n\r\nobserver.observe(document.querySelector('.target-element-selector'));`, `8151325527475839000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token function-variable function\">handler</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">changes</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">for</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">const</span> change <span class=\"token keyword\">in</span> changes<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token comment\">// feature detection</span>\r\n    <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">typeof</span> change<span class=\"token punctuation\">.</span>isVisible <span class=\"token operator\">!==</span> <span class=\"token string\">'undefined'</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n      <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>change<span class=\"token punctuation\">.</span>isIntersecting <span class=\"token operator\">&amp;&amp;</span> change<span class=\"token punctuation\">.</span>isVisible<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n        console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Visible since </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>change<span class=\"token punctuation\">.</span>time<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n      <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\r\n        <span class=\"token comment\">// fallback to v1 if not supported</span>\r\n        change<span class=\"token punctuation\">.</span>isVisible <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span>\r\n      <span class=\"token punctuation\">}</span>\r\n    <span class=\"token punctuation\">}</span>\r\n  <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token keyword\">const</span> observer <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">IntersectionObserver</span><span class=\"token punctuation\">(</span>handler<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token literal-property property\">threshold</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1.0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>  \r\n  <span class=\"token literal-property property\">trackVisibility</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token comment\">// this will give you the isVisible property</span>\r\n  <span class=\"token literal-property property\">delay</span><span class=\"token operator\">:</span> <span class=\"token number\">100</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\nobserver<span class=\"token punctuation\">.</span><span class=\"token function\">observe</span><span class=\"token punctuation\">(</span>document<span class=\"token punctuation\">.</span><span class=\"token function\">querySelector</span><span class=\"token punctuation\">(</span><span class=\"token string\">'.target-element-selector'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"additional-parameters\" style=\"position:relative;\"><a href=\"#additional-parameters\" aria-label=\"additional parameters permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Additional parameters</h2>\n<p>The object which is passed in the constructor has a few properties:</p>\n<ul>\n<li><strong>threshold</strong>: A list of thresholds which will trigger a callback.</li>\n<li><strong>trackVisibility</strong>: A boolean indicating whether or not to track the visibility of the element.</li>\n<li><strong>delay</strong>: A number defining the minimum delay in milliseconds between notification for a given object.</li>\n</ul>\n<blockquote>\n<p>💡 Note that tracking visibility is an expensive operation and should only be used when necessary. The performance implications can cause more harm than good if abused.</p>\n</blockquote>\n<h2 id=\"how-does-it-determine-visibility\" style=\"position:relative;\"><a href=\"#how-does-it-determine-visibility\" aria-label=\"how does it determine visibility permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How does it determine visibility?</h2>\n<p>Determining whether the element is visible or not is not as simple as you might think. Based on the spec:</p>\n<ul>\n<li>If <code class=\"language-text\">false</code> is passed as the value of <code class=\"language-text\">trackVisibility</code>, the element is considered visible.</li>\n<li>If the target element has an effective transformation matrix other than <code class=\"language-text\">2D</code> then the element is considered invisible.</li>\n<li>If the target element or any of its children, has an effective capacity other than <code class=\"language-text\">1.0</code>, then it’s considered invisible.</li>\n<li>If the target element or any of its children has a filter applied, then it’s considered invisible.</li>\n<li>If the browser cannot guarantee the target is fully visible, then it’s considered invisible.</li>\n</ul>\n<h2 id=\"whats-the-catch\" style=\"position:relative;\"><a href=\"#whats-the-catch\" aria-label=\"whats the catch permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What’s the catch?</h2>\n<p>This is in a draft state and still work in progress. Apart from Chrome, none of the other major browsers currently support it.</p>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>Although this hasn’t been implemented in most browsers yet, it’s a great tool to prevent some of the common attacks such as <code class=\"language-text\">clickjacking</code> or <code class=\"language-text\">likejacking</code>, or <code class=\"language-text\">cursorjacking</code>.</p>","timeToRead":2,"frontmatter":{"title":"Introducing the Intersection Observer V2 ⛌","unformattedDate":"2020-06-22T00:00:00.000Z","date":"Jun 22, 2020","path":"/Intersection-observer-v2/","author":"Yaser Adel Mehraban","tags":["showdev","webdev","javascript","intersectionobserver"],"thumbnail":null}}},{"node":{"id":"e17ac429-5a2f-512f-9056-66626a7b7ec0","html":"<p>You probably have used fragments in links to point a link to a part of your page such as a heading. This technique is used in table of content for example and it’s a common pattern.</p>\n<!--more-->\n<h2 id=\"background\" style=\"position:relative;\"><a href=\"#background\" aria-label=\"background permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Background</h2>\n<p>To extend the current support for scrolling to an anchor element in a page, the W3C have proposed a solution which basically adds the ability to link to a DOM element with id, or textual content on a page to make it easier for browsers to understand what the users are interested and scroll there when they visit the page. In addition to make it visually easier to follow, they have added the highlight to the text so that users will know where to look at on first sight.</p>\n<h2 id=\"scroll-to-text-fragment\" style=\"position:relative;\"><a href=\"#scroll-to-text-fragment\" aria-label=\"scroll to text fragment permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Scroll to Text Fragment</h2>\n<p>So that’s how <a href=\"https://github.com/WICG/scroll-to-text-fragment\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Scroll to Text Fragment</a> was born. It has been shipped to Chrome and Microsoft Edge called <a href=\"https://github.com/WICG/scroll-to-text-fragment\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Scroll to Text Fragment</a> which allows you to link to a specific text on a page, using a fragment provided in the URL. When the page is loaded, the browser highlights the text and scrolls it into view.</p>\n<p>This is the generic syntax:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-text line-numbers\"><code class=\"language-text\">#:~:text=[prefix-,]textStart[,textEnd][,-suffix]\r\n          context  |-------match-----|  context</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<blockquote>\n<p>💡 Square brackets indicate an optional parameter.</p>\n</blockquote>\n<p>Here is an example:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"8790737169202512000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<a href=&quot;https://yashints.dev/#:~:text=Meet&text=Yas&quot;>\r\n  Click to go to a specific portion of a page\r\n</a>`, `8790737169202512000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>a</span> <span class=\"token attr-name\">href</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>https://yashints.dev/#:~:text=Meet&amp;text=Yas<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>\r\n  Click to go to a specific portion of a page\r\n<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>a</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>And you can test it live by <a href=\"https://yashints.dev#:~:text=Meet&#x26;text=Yas\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">clicking here</a>.</p>\n<p>If you want to target an exact text:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"23168441677398864000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<a href=&quot;https://yashints.dev/#:~:text=almond%20croissant%20addict&quot;>\r\n  Click to go to a specific portion of a page\r\n</a>`, `23168441677398864000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>a</span> <span class=\"token attr-name\">href</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>https://yashints.dev/#:~:text=almond%20croissant%20addict<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>\r\n  Click to go to a specific portion of a page\r\n<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>a</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p><a href=\"https://yashints.dev/#:~:text=almond%20croissant%20addict\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Try it here</a>.</p>\n<p>You can also add an end text in which case the text directive refers to a range of text on the page.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"42259565083372120000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<a href=&quot;https://yashints.dev/#:~:text=Although,web%20developer&quot;>\r\n  Click to go to a specific portion of a page\r\n</a>`, `42259565083372120000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>a</span> <span class=\"token attr-name\">href</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>https://yashints.dev/#:~:text=Although,web%20developer<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>\r\n  Click to go to a specific portion of a page\r\n<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>a</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p><a href=\"https://yashints.dev/#:~:text=Although,web%20developer\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Try it here</a>.</p>\n<h2 id=\"use-cases\" style=\"position:relative;\"><a href=\"#use-cases\" aria-label=\"use cases permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use cases</h2>\n<h3 id=\"search-engines\" style=\"position:relative;\"><a href=\"#search-engines\" aria-label=\"search engines permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Search engines</h3>\n<p>When search engines will link to pages which contain relevant information to the query, they will scroll and highlight that so the user will find it easier.</p>\n<h3 id=\"citation--reference-links\" style=\"position:relative;\"><a href=\"#citation--reference-links\" aria-label=\"citation  reference links permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Citation / Reference links</h3>\n<p>Links are sometimes used as citations on some pages where the author wishes to enforce a claim by pointing to a reference such as <em>Wikipedia</em>. These reference pages can contains large chunks of text and finding the information can be difficult. So having the ability to scroll to that section and highlight it is really encouraging to readers to follow the links.</p>\n<h3 id=\"sharing-a-specific-text-or-paragraph\" style=\"position:relative;\"><a href=\"#sharing-a-specific-text-or-paragraph\" aria-label=\"sharing a specific text or paragraph permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Sharing a specific text or paragraph</h3>\n<p>Sometimes you want to share a piece of text to someone for example by email or on social media, it’s really helpful to be able to link directly to the specific section rather than the whole page.</p>\n<p>Nowadays people share short snippets directly in tweets or create screenshots which can contain more text and post those. With this they won’t need to do that since links can be more meaningful.</p>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>And that’s how the web is moving towards a better place where users are the centre of attention for us developers and the more we know how to make their lives easier the more they trust us to take care of it for them.</p>\n<p>Enjoy your evening and till next time 👋🏽.</p>","timeToRead":2,"frontmatter":{"title":"Did you know about scroll to text? 📜","unformattedDate":"2020-06-14T00:00:00.000Z","date":"Jun 14, 2020","path":"/scroll-to-text/","author":"Yaser Adel Mehraban","tags":["showdev","webdev","javascript","scrolltotext"],"thumbnail":null}}},{"node":{"id":"9507529c-b56d-5ca8-a624-65ac967832a6","html":"<p>I see some occasions where web developers who are a bit less familiar with accessibility use links incorrectly in different forms. So I thought let’s write a quick guide on <em>Links</em> in JavaScript apps.</p>\n<!--more-->\n<h2 id=\"background\" style=\"position:relative;\"><a href=\"#background\" aria-label=\"background permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Background</h2>\n<p>It’s safe to assume that anyone who’s used the web have seen them. They take users from one page to another either within the same application or to an external website. There were really hot when they were introduced back in <strong>1966</strong> by a team led by <a href=\"https://en.wikipedia.org/wiki/Douglas_Engelbart\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Douglas Engelbart</a>.</p>\n<p>In the early 1980s <em>Ben Shneiderman</em>, a computer scientist at the University of Maryland, with his graduate student <em>Dan Ostroff</em> were preparing a videodisk exhibit for a museum. It was then where they used a caption that had all the information and users could click on it to see the photo associated with it.</p>\n<p>At first, this discovery was called <code class=\"language-text\">embedded menu</code> but that term was quickly replaced by <code class=\"language-text\">hyperlink</code>. <a href=\"https://en.wikipedia.org/wiki/Tim_Berners-Lee\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Tim Berners-Lee</a> cited Shneiderman’s hyperlink work in his spring 1989 manifesto for what would become the World Wide Web.</p>\n<p>Back then, this feature was super hot and it still is. People use it without even thinking about it and it’s become a natural part of the web.</p>\n<h2 id=\"enough-background-tell-me-whats-this-all-about\" style=\"position:relative;\"><a href=\"#enough-background-tell-me-whats-this-all-about\" aria-label=\"enough background tell me whats this all about permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Enough background, tell me what’s this all about</h2>\n<p>Let’s review what they do:</p>\n<ul>\n<li>As mentioned before they allow users to navigate between pages within a site or to an external one.</li>\n<li>And more importantly they help search engines and bots to find resources and going from one page to another by following the links. Think of this as a giant maze where they need to blindly follow the links to discover stuff.</li>\n<li>And last, the crawlers use them to understand the architecture of a site and the type of content they have. It’s a critical part of their decision making when assigning a page to a specific topic.</li>\n</ul>\n<h2 id=\"but-how-do-you-make-a-link\" style=\"position:relative;\"><a href=\"#but-how-do-you-make-a-link\" aria-label=\"but how do you make a link permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>But how do you make a link?</h2>\n<h3 id=\"code-classlanguage-textacode-tag-with-code-classlanguage-texthrefcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textacode-tag-with-code-classlanguage-texthrefcode\" aria-label=\"code classlanguage textacode tag with code classlanguage texthrefcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">a</code> tag with <code class=\"language-text\">href</code></h3>\n<p>Turns out it’s not as straightforward as one might think. The simplest way to put a link on your site is to use a good old <code class=\"language-text\">&lt;a></code> tag with a <code class=\"language-text\">URL</code> which it points to in the <code class=\"language-text\">href</code> attribute.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"79777914997817570000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<a href=&quot;/elsewhere&quot;>A good link, YES ╰(*°▽°*)╯</a>`, `79777914997817570000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>a</span> <span class=\"token attr-name\">href</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>/elsewhere<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>A good link, YES ╰(*°▽°*)╯<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>a</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textacode-tag-with-code-classlanguage-texthrefcode-and-event-handler\" style=\"position:relative;\"><a href=\"#code-classlanguage-textacode-tag-with-code-classlanguage-texthrefcode-and-event-handler\" aria-label=\"code classlanguage textacode tag with code classlanguage texthrefcode and event handler permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">a</code> tag with <code class=\"language-text\">href</code> and event handler</h3>\n<p>For those with special circumstances, they might sprinkle a little bit of JavaScript on top and use an event handler to navigate the user programmatically:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"79637347631245260000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<a href=&quot;/elsewhere&quot; onclick=&quot;goTo('elsewhere')&quot;>Okay ¯\\_(ツ)_/¯</a>`, `79637347631245260000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>a</span> <span class=\"token attr-name\">href</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>/elsewhere<span class=\"token punctuation\">\"</span></span> <span class=\"token special-attr\"><span class=\"token attr-name\">onclick</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span><span class=\"token value javascript language-javascript\"><span class=\"token function\">goTo</span><span class=\"token punctuation\">(</span><span class=\"token string\">'elsewhere'</span><span class=\"token punctuation\">)</span></span><span class=\"token punctuation\">\"</span></span></span><span class=\"token punctuation\">></span></span>Okay ¯\\_(ツ)_/¯<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>a</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>This is fine as the link stays functional and you can upgrade the functionality using JavaScript.</p>\n<h3 id=\"code-classlanguage-textacode-tag-without-code-classlanguage-texthrefcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textacode-tag-without-code-classlanguage-texthrefcode\" aria-label=\"code classlanguage textacode tag without code classlanguage texthrefcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">a</code> tag without <code class=\"language-text\">href</code></h3>\n<p>But some people decide to remove the <code class=\"language-text\">href</code> attribute. This is not a good idea because you now prevented crawlers to find out where this link goes to:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"37229267911415120000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<a onclick=&quot;goTo('elsewhere')&quot;>Please don't ╯︿╰</a>`, `37229267911415120000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>a</span> <span class=\"token special-attr\"><span class=\"token attr-name\">onclick</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span><span class=\"token value javascript language-javascript\"><span class=\"token function\">goTo</span><span class=\"token punctuation\">(</span><span class=\"token string\">'elsewhere'</span><span class=\"token punctuation\">)</span></span><span class=\"token punctuation\">\"</span></span></span><span class=\"token punctuation\">></span></span>Please don't ╯︿╰<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>a</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textacode-tag-with-code-classlanguage-texthrefcode-but-no-link\" style=\"position:relative;\"><a href=\"#code-classlanguage-textacode-tag-with-code-classlanguage-texthrefcode-but-no-link\" aria-label=\"code classlanguage textacode tag with code classlanguage texthrefcode but no link permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">a</code> tag with <code class=\"language-text\">href</code> but no link</h3>\n<p>In other occasions, people use the <code class=\"language-text\">href</code> attribute without a URL or with a pseudo URL:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"28093931985923314000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<a href=&quot;javascript:goTo('elsewhere')&quot;>Seriously? (⊙_⊙)？</a>`, `28093931985923314000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>a</span> <span class=\"token attr-name\">href</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>javascript:goTo('elsewhere')<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>Seriously? (⊙_⊙)？<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>a</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>This is even worse than the previous case as it just confuses the hell out of every crawler out there.</p>\n<h3 id=\"using-code-classlanguage-textbuttoncode\" style=\"position:relative;\"><a href=\"#using-code-classlanguage-textbuttoncode\" aria-label=\"using code classlanguage textbuttoncode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>using <code class=\"language-text\">button</code></h3>\n<p>You might think it’s a good idea to use a button to navigate the user, but let me tell you, that’s not a good idea either. The rule of thumb is that if the purpose is to do something on the current page, it could be a button. But if it’s takes users to another page or to an external site, it should be a link.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"51488823975258230000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<button onclick=&quot;goTo('elsewhere')&quot;>But why? <( _ _ )> </button>`, `51488823975258230000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>button</span> <span class=\"token special-attr\"><span class=\"token attr-name\">onclick</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span><span class=\"token value javascript language-javascript\"><span class=\"token function\">goTo</span><span class=\"token punctuation\">(</span><span class=\"token string\">'elsewhere'</span><span class=\"token punctuation\">)</span></span><span class=\"token punctuation\">\"</span></span></span><span class=\"token punctuation\">></span></span>But why? <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>(</span> <span class=\"token attr-name\">_</span> <span class=\"token attr-name\">_</span> <span class=\"token attr-name\">)</span><span class=\"token punctuation\">></span></span> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>button</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<h3 id=\"any-other-html-element\" style=\"position:relative;\"><a href=\"#any-other-html-element\" aria-label=\"any other html element permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>any other HTML element</h3>\n<p>It’s worth noting that you shouldn’t simulate a link using any other HTML element and add a click handler with JavaScript:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"76669263857467210000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<div onclick=&quot;goTo('elsewhere')&quot;>NOOOOOOOOOOOOOOOO!!!!!!!!!!!! (#\\`O′)</div>`, `76669263857467210000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span> <span class=\"token special-attr\"><span class=\"token attr-name\">onclick</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span><span class=\"token value javascript language-javascript\"><span class=\"token function\">goTo</span><span class=\"token punctuation\">(</span><span class=\"token string\">'elsewhere'</span><span class=\"token punctuation\">)</span></span><span class=\"token punctuation\">\"</span></span></span><span class=\"token punctuation\">></span></span>NOOOOOOOOOOOOOOOO!!!!!!!!!!!! (#`O′)<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>This not only wrong for crawlers, but also messes up the screen readers and causes a lot of trouble for people with any vision impairment. So please just use a link with a proper URL.</p>\n<h2 id=\"what-is-a-proper-url-i-hear-you-ask\" style=\"position:relative;\"><a href=\"#what-is-a-proper-url-i-hear-you-ask\" aria-label=\"what is a proper url i hear you ask permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is a proper URL? I hear you ask</h2>\n<p>You saw we ruled out the pseudo URLs like <code class=\"language-text\">javascript:doSomething()</code>, but let’s take a look at URLs more closely.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"50245902276091160000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`http://example.com/old-school-url\r\n\r\nexample.com/page#section`, `50245902276091160000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-text line-numbers\"><code class=\"language-text\">http://example.com/old-school-url\r\n\r\nexample.com/page#section</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>A URL contains these elements:</p>\n<ul>\n<li>A protocol (optional), which defines the protocol to be used for navigation such as <code class=\"language-text\">http</code>, or <code class=\"language-text\">https</code>.</li>\n<li>It also has a host, <code class=\"language-text\">example.com</code> or <code class=\"language-text\">yashints.dev</code>. A host is a name that one or some computers respond to. Usually it points to an IP address.</li>\n<li>It has a path to specific asset on that computer such as an HTML file, an image, CSS file or a font. But that’s not always the case, sometimes the path is handled via JavaScript and is used to replace part of the content in a page. This technique is called Single Page Application or SPA.</li>\n<li>Some URLs might contain another part such the second example above. That part which comes after the <code class=\"language-text\">#</code> sign, is called a fragment identifier. By itself a fragment identifier is not a piece of content. It just points to a specific part in the content such as a header.</li>\n</ul>\n<blockquote>\n<p>💡 Because fragments are not suppose to point to different content, crawlers ignore them altogether. This means for single page applications where they use fragment like routing, crawlers don’t follow them and that’s why people use techniques such as server side rendering or SSR to allow crawlers become aware of the page’s content.</p>\n</blockquote>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>To sum it all up:</p>\n<ul>\n<li>Use proper link markup, <code class=\"language-text\">a</code> tag with <code class=\"language-text\">href</code> attribute.</li>\n<li>Do not use other HTML elements to navigate users.</li>\n<li>Do not omit the <code class=\"language-text\">href</code> attribute on an anchor tag.</li>\n<li>And last but not least, do not use fragment identifiers to load different content in SPAs if you can.</li>\n</ul>\n<p>Hope you’ve enjoyed reading this and thanks for drooping by. Until next article, Ta.</p>","timeToRead":4,"frontmatter":{"title":"Links and JavaScript 🔗","unformattedDate":"2020-06-03T00:00:00.000Z","date":"Jun 03, 2020","path":"/links/","author":"Yaser Adel Mehraban","tags":["showdev","webdev","javascript","links"],"thumbnail":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDBf/EABYBAQEBAAAAAAAAAAAAAAAAAAECA//aAAwDAQACEAMQAAAB2kolFjANj//EABsQAAIBBQAAAAAAAAAAAAAAAAECAwAQEiEx/9oACAEBAAEFAgAtRvlYdk1GWZj/AP/EABURAQEAAAAAAAAAAAAAAAAAAAAR/9oACAEDAQE/AVf/xAAYEQACAwAAAAAAAAAAAAAAAAAAEyExYf/aAAgBAgEBPwG4Eaf/xAAaEAEAAgMBAAAAAAAAAAAAAAABABECEDES/9oACAEBAAY/AnySnu8k7UtWf//EABsQAAICAwEAAAAAAAAAAAAAAAABESEQMXFB/9oACAEBAAE/IUsNN30tohMbB9f9CUE+n//aAAwDAQACAAMAAAAQ/wAf/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPxAf/8QAGBEBAQADAAAAAAAAAAAAAAAAAQARITH/2gAIAQIBAT8QNsusOf/EABsQAQEAAgMBAAAAAAAAAAAAAAERACEQMUGR/9oACAEBAAE/EFumQEFNY69vknBmx+40oTQG+scLE9Gf/9k="},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/ded954c9096452afa1ce0e02c9b7791f/84808/link.jpg","srcSet":"/static/ded954c9096452afa1ce0e02c9b7791f/9e0f0/link.jpg 750w,\n/static/ded954c9096452afa1ce0e02c9b7791f/84808/link.jpg 960w","sizes":"(min-width: 960px) 960px, 100vw"},"sources":[{"srcSet":"/static/ded954c9096452afa1ce0e02c9b7791f/cbeef/link.webp 750w,\n/static/ded954c9096452afa1ce0e02c9b7791f/f07e3/link.webp 960w","type":"image/webp","sizes":"(min-width: 960px) 960px, 100vw"}]},"width":960,"height":540}}}}}},{"node":{"id":"7dfe0f5f-240c-5739-80c3-b98d87efe202","html":"<p>There may be many different reasons why you’d want to swap two variables be it just changing two item’s location in an array or when sorting collections. The traditional way is just define a new variable, assign one value to it, put one of the items in the old place, then put the temp variable back in the new place. But my question is, is that the only way?</p>\n<!--more-->\n<h2 id=\"traditionally\" style=\"position:relative;\"><a href=\"#traditionally\" aria-label=\"traditionally permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Traditionally</h2>\n<p>The old way of swapping two variables is done like below:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"94304487280905680000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let x = &quot;Yas&quot;;\r\nlet y = &quot;Hints&quot;;\r\n\r\nlet temp = x;\r\nx = y;\r\ny = temp;\r\n\r\nconsole.log(x); // Hints\r\nconsole.log(y); // Yas`, `94304487280905680000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">let</span> x <span class=\"token operator\">=</span> <span class=\"token string\">\"Yas\"</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">let</span> y <span class=\"token operator\">=</span> <span class=\"token string\">\"Hints\"</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">let</span> temp <span class=\"token operator\">=</span> x<span class=\"token punctuation\">;</span>\r\nx <span class=\"token operator\">=</span> y<span class=\"token punctuation\">;</span>\r\ny <span class=\"token operator\">=</span> temp<span class=\"token punctuation\">;</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>x<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Hints</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>y<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Yas</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>There is nothing wrong with this approach unless you’re doing it frequently.</p>\n<h2 id=\"without-the-temp-variable\" style=\"position:relative;\"><a href=\"#without-the-temp-variable\" aria-label=\"without the temp variable permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Without the temp variable</h2>\n<p>There is another way you could swap two variables without any temp variable. But this only works with numbers:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"58796569137422240000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let x = 10;\r\nlet y = 20;\r\n\r\nx = x + y;\r\ny = x - y\r\nx = x - y;\r\n\r\nconsole.log(x); // 20\r\nconsole.log(y); // 10`, `58796569137422240000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">let</span> x <span class=\"token operator\">=</span> <span class=\"token number\">10</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">let</span> y <span class=\"token operator\">=</span> <span class=\"token number\">20</span><span class=\"token punctuation\">;</span>\r\n\r\nx <span class=\"token operator\">=</span> x <span class=\"token operator\">+</span> y<span class=\"token punctuation\">;</span>\r\ny <span class=\"token operator\">=</span> x <span class=\"token operator\">-</span> y\r\nx <span class=\"token operator\">=</span> x <span class=\"token operator\">-</span> y<span class=\"token punctuation\">;</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>x<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 20</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>y<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 10</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>This works two, but now we’re doing three additional operations to save some space, so you need to be careful when you use this one. Another thing to consider with this approach is the chance of having overflows with additions or subtractions (<code class=\"language-text\">sum</code> should be less than <code class=\"language-text\">Number.MAX_SAFE_INTEGER</code> which is <code class=\"language-text\">9007199254740991</code>).</p>\n<h2 id=\"bitwise-code-classlanguage-textxorcode\" style=\"position:relative;\"><a href=\"#bitwise-code-classlanguage-textxorcode\" aria-label=\"bitwise code classlanguage textxorcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Bitwise <code class=\"language-text\">XOR</code></h2>\n<p>Similar to above approach, you could use <code class=\"language-text\">XOR</code> to swap the two variables, but this also works only on numbers:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"17195597845959033000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let x = 3;\r\nlet y = 5;\r\n\r\nx = x ^ y;\r\ny = x ^ y;\r\nx = x ^ y;\r\n\r\nconsole.log(x); // 5\r\nconsole.log(y); // 3`, `17195597845959033000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">let</span> x <span class=\"token operator\">=</span> <span class=\"token number\">3</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">let</span> y <span class=\"token operator\">=</span> <span class=\"token number\">5</span><span class=\"token punctuation\">;</span>\r\n\r\nx <span class=\"token operator\">=</span> x <span class=\"token operator\">^</span> y<span class=\"token punctuation\">;</span>\r\ny <span class=\"token operator\">=</span> x <span class=\"token operator\">^</span> y<span class=\"token punctuation\">;</span>\r\nx <span class=\"token operator\">=</span> x <span class=\"token operator\">^</span> y<span class=\"token punctuation\">;</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>x<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 5</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>y<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 3</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>If you’re not familiar with <code class=\"language-text\">XOR</code>, it works on bits. When you perform <code class=\"language-text\">XOR</code> two bits, it evaluates to <code class=\"language-text\">1</code> if they are different, and evaluates to <code class=\"language-text\">0</code> if they’re the same.</p>\n<table>\n<thead>\n<tr>\n<th align=\"center\">x</th>\n<th align=\"center\">y</th>\n<th align=\"center\">XOR</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"center\">0</td>\n<td align=\"center\">0</td>\n<td align=\"center\">0</td>\n</tr>\n<tr>\n<td align=\"center\">1</td>\n<td align=\"center\">1</td>\n<td align=\"center\">0</td>\n</tr>\n<tr>\n<td align=\"center\">0</td>\n<td align=\"center\">1</td>\n<td align=\"center\">1</td>\n</tr>\n<tr>\n<td align=\"center\">1</td>\n<td align=\"center\">0</td>\n<td align=\"center\">1</td>\n</tr>\n</tbody>\n</table>\n<p>So let’s see why this works.</p>\n<ol>\n<li><code class=\"language-text\">x = x ^ y</code></li>\n<li><code class=\"language-text\">y = y ^ x</code> when <code class=\"language-text\">x = (x ^ y)</code>, so the <code class=\"language-text\">y = (x ^ y) ^ y</code> which equals to <code class=\"language-text\">x ^ (y ^ y) = x ^ 0 = x</code>. So now our <code class=\"language-text\">y</code> is the old <code class=\"language-text\">x</code>.</li>\n<li><code class=\"language-text\">x = x ^ y</code> when according to our first step <code class=\"language-text\">x</code> is not <code class=\"language-text\">x ^ y</code>, and so <code class=\"language-text\">x = (x ^ y) ^ x = y ^ (x ^ x) = y ^ 0 = y</code>.</li>\n</ol>\n<p>Is this better than the previous one, probably faster, but still limited to numbers only.</p>\n<h2 id=\"es6-destructuring\" style=\"position:relative;\"><a href=\"#es6-destructuring\" aria-label=\"es6 destructuring permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>ES6 destructuring</h2>\n<p>Destructuring is an <code class=\"language-text\">ES6</code> feature which is used a lot in many of the modern frameworks. In its core, it allows you to store array elements in variables.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"66771519949289340000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let x;\r\nlet y;\r\n\r\n[x, y] = [1, 2, 3];\r\n\r\nconsole.log(x); // 1\r\nconsole.log(y); // 2`, `66771519949289340000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">let</span> x<span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">let</span> y<span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token punctuation\">[</span>x<span class=\"token punctuation\">,</span> y<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>x<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 1</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>y<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 2</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Now considering how we can use this to swap the elements of an array:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"87167112241099670000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let x = &quot;Yas&quot;;\r\nlet y = &quot;Hints&quot;;\r\n\r\n[x, y] = [y , x];\r\n\r\nconsole.log(x); // Hints\r\nconsole.log(y); // Yas`, `87167112241099670000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">let</span> x <span class=\"token operator\">=</span> <span class=\"token string\">\"Yas\"</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">let</span> y <span class=\"token operator\">=</span> <span class=\"token string\">\"Hints\"</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token punctuation\">[</span>x<span class=\"token punctuation\">,</span> y<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span>y <span class=\"token punctuation\">,</span> x<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>x<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Hints</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>y<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Yas</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>This method is much elegant, but still creates two arrays to perform the swapping. So the efficiency might not be that good if you’re swapping many elements.</p>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>Just because a feature is available, doesn’t mean you should use it in every situation. Think about what is the most important factor in the solution you’re implementing. If it’s space, choose one which doesn’t take much, although it’s a bit slower.</p>\n<p>If the memory doesn’t matter but speed is important, choose accordingly. But definitely consider the situation before deciding on your approach.</p>","timeToRead":2,"frontmatter":{"title":"Swapping variables in JavaScript 🔁","unformattedDate":"2020-05-15T00:00:00.000Z","date":"May 15, 2020","path":"/swap/","author":"Yaser Adel Mehraban","tags":["showdev","webdev","javascript","swap"],"thumbnail":null}}},{"node":{"id":"3fabe53b-b745-5c26-8ed8-75d0debab547","html":"<p><code class=\"language-text\">String</code> is one of the primitive types in JavaScript and we use it in every project we work on no matter what. But how familiar are you with the methods available in JavaScript to work with a string variable? Let’s have a quick look at those in this article.</p>\n<!--more-->\n<h2 id=\"introduction\" style=\"position:relative;\"><a href=\"#introduction\" aria-label=\"introduction permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Introduction</h2>\n<p>A primitive value such as “Yas” doesn’t have any methods or properties, mainly because it’s not an object. But with JavaScript, methods and properties are available because it treats primitive values as objects.</p>\n<p>Let’s have a look at the simplest method you have most definitely used:</p>\n<h2 id=\"string-length\" style=\"position:relative;\"><a href=\"#string-length\" aria-label=\"string length permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>String length</h2>\n<p>The <code class=\"language-text\">length</code> property returns the length of a string:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"28483626680013607000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const alphabet = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;;\r\nconsole.log(alphabet.length); // 26`, `28483626680013607000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> alphabet <span class=\"token operator\">=</span> <span class=\"token string\">\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>alphabet<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 26</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>Pay attention on how <code class=\"language-text\">length</code> property is available on a primitive type. Not all languages are following the same principle when dealing with such behaviour though. In <code class=\"language-text\">PHP</code> we have helper functions:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"86995251580721870000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<?php\r\necho strlen(&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;);\r\n?>`, `86995251580721870000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"php\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-php line-numbers\"><code class=\"language-php\"><span class=\"token php language-php\"><span class=\"token delimiter important\">&lt;?php</span>\r\n<span class=\"token keyword\">echo</span> <span class=\"token function\">strlen</span><span class=\"token punctuation\">(</span><span class=\"token string double-quoted-string\">\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token delimiter important\">?></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"finding-a-text-in-string\" style=\"position:relative;\"><a href=\"#finding-a-text-in-string\" aria-label=\"finding a text in string permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Finding a text in string</h2>\n<p>There are a few methods to help you find a sub-string in a <code class=\"language-text\">String</code>. Let’s go through them and see what are their differences:</p>\n<h3 id=\"code-classlanguage-textindexofcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textindexofcode\" aria-label=\"code classlanguage textindexofcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">indexOf</code></h3>\n<p>The <code class=\"language-text\">indexOf</code> method returns the index of the <strong>first</strong> occurrence of a specified text in a string:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"15957888891774052000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;Can you find Yas in 'dryasdust'?&quot;;\r\nconsole.log(txt.indexOf('yas')); // 23`, `15957888891774052000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"Can you find Yas in 'dryasdust'?\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">indexOf</span><span class=\"token punctuation\">(</span><span class=\"token string\">'yas'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 23</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>There a few points you need to know here. First, JavaScript counts positions from zero, and second, <code class=\"language-text\">indexOf</code> is case sensitive.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"27015599016083325000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;Can you find Yas in 'dryasdust'?&quot;;\r\nconsole.log(txt.indexOf('Yas')); // 13`, `27015599016083325000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"Can you find Yas in 'dryasdust'?\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">indexOf</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Yas'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 13</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>This method returns <code class=\"language-text\">-1</code> if it can’t find the text:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"11566226949597125000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;There is no 0 in 11&quot;;\r\nconsole.log(txt.indexOf('zero')); // -1`, `11566226949597125000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"There is no 0 in 11\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">indexOf</span><span class=\"token punctuation\">(</span><span class=\"token string\">'zero'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// -1</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>You can pass a second argument to let the <code class=\"language-text\">indexOf</code> know where to start looking for the text:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"28268540693311173000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;We have SQL and no-SQL databases!&quot;;\r\nconsole.log(txt.indexOf('SQl', 10)); // 19`, `28268540693311173000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"We have SQL and no-SQL databases!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">indexOf</span><span class=\"token punctuation\">(</span><span class=\"token string\">'SQl'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">10</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 19</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textlastindexofcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textlastindexofcode\" aria-label=\"code classlanguage textlastindexofcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">lastIndexOf</code></h3>\n<p>As the name suggests, <code class=\"language-text\">lastIndexOf</code> is used to find the last occurrence of a text in a string.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"85524923317222460000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;Can you find Jam in 'JamStack'?&quot;;\r\nconsole.log(txt.indexOf('Jam')); // 21`, `85524923317222460000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"Can you find Jam in 'JamStack'?\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">indexOf</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Jam'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 21</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>This method also returns <code class=\"language-text\">-1</code> if it can’t find the text you’re looking for, and takes a second parameter to start the search. However, since this method starts the search backward, the second parameter acts as cutting the string from that position to the end:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"85537389308966830000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;Can you find Jam in 'JamStack'?&quot;;\r\nconsole.log(txt.lastIndexOf('Jam', 6)); // -1`, `85537389308966830000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"Can you find Jam in 'JamStack'?\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">lastIndexOf</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Jam'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">6</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// -1</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textsearchcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textsearchcode\" aria-label=\"code classlanguage textsearchcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">search</code></h3>\n<p>The <code class=\"language-text\">search</code> method also searches the string for a text and returns the <strong>first</strong> occurrence of the text:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"17872061477277067000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;Can you find Jam in 'JamStack'?&quot;;\r\nconsole.log(txt.search('Jam')); // 13`, `17872061477277067000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"Can you find Jam in 'JamStack'?\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">search</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Jam'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 13</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>You might think that <code class=\"language-text\">search</code> and <code class=\"language-text\">indexOf</code> are the same. However, there are differences in these two:</p>\n<ul>\n<li><code class=\"language-text\">search</code> doesn’t accept any other parameter</li>\n<li><code class=\"language-text\">indexOf</code> cannot take powerful search values such as regular expressions</li>\n</ul>\n<p>That’s right, <code class=\"language-text\">search</code> will accept regex as argument as well, for example, to perform a case insensitive search you might want to use <code class=\"language-text\">search</code> instead of <code class=\"language-text\">indexOf</code>:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"43963498235639740000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;There is Jam in JamStack!&quot;;\r\nconsole.log(str.search(/jam/i)); // 9\r\n\r\nconsole.log(txt.indexOf('jam')); // -1`, `43963498235639740000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"There is Jam in JamStack!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>str<span class=\"token punctuation\">.</span><span class=\"token function\">search</span><span class=\"token punctuation\">(</span><span class=\"token regex\"><span class=\"token regex-delimiter\">/</span><span class=\"token regex-source language-regex\">jam</span><span class=\"token regex-delimiter\">/</span><span class=\"token regex-flags\">i</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 9</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">indexOf</span><span class=\"token punctuation\">(</span><span class=\"token string\">'jam'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// -1</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span></span></pre></div>\n<p>You can also search for non usual patterns, e.g. finding any character that is not a word or whitespace:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"42813374885244240000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;There is Jam in JamStack!&quot;;\r\nconsole.log(txt.search(/[^\\w\\s]/g)); // 24`, `42813374885244240000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"There is Jam in JamStack!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">search</span><span class=\"token punctuation\">(</span><span class=\"token regex\"><span class=\"token regex-delimiter\">/</span><span class=\"token regex-source language-regex\">[^\\w\\s]</span><span class=\"token regex-delimiter\">/</span><span class=\"token regex-flags\">g</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 24</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textendswithcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textendswithcode\" aria-label=\"code classlanguage textendswithcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">endsWith</code></h3>\n<p>The <code class=\"language-text\">endsWith</code> methods checks whether the string ends with the specified text. It returns <code class=\"language-text\">true</code> if it does, and <code class=\"language-text\">false</code> if it doesn’t:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"38562029584462460000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;There is Jam in JamStack!&quot;;\r\nconsole.log(txt.endsWith(&quot;Stack!&quot;)); // true`, `38562029584462460000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"There is Jam in JamStack!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">endsWith</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Stack!\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// true</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<blockquote>\n<p>💡 This method is case sensitive.</p>\n</blockquote>\n<h3 id=\"code-classlanguage-textstartswithcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textstartswithcode\" aria-label=\"code classlanguage textstartswithcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">startsWith</code></h3>\n<p>Similar to <code class=\"language-text\">endsWith</code>, this method checks whether a string starts with the specified text. This method is also case sensitive:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"2116656782349224400\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;JamStack's got Jam&quot;;\r\nconsole.log(txt.startsWith(&quot;JamStack&quot;)); // true`, `2116656782349224400`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"JamStack's got Jam\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">startsWith</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"JamStack\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// true</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textincludescode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textincludescode\" aria-label=\"code classlanguage textincludescode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">includes</code></h3>\n<p><code class=\"language-text\">includes</code> allows you to check whether or not a string contains a specified text and is case sensitive:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"27071704092426252000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;There is Jam in JamStack!&quot;;\r\nconsole.log(txt.includes(&quot;in&quot;)); // true\r\nconsole.log(txt.includes(&quot;Jam&quot;)); // true\r\nconsole.log(txt.includes(&quot;jam&quot;)); // false`, `27071704092426252000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"There is Jam in JamStack!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">includes</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"in\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// true</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">includes</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Jam\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// true</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">includes</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"jam\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// false</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textlocalecomparecode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textlocalecomparecode\" aria-label=\"code classlanguage textlocalecomparecode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">localeCompare</code></h3>\n<p><code class=\"language-text\">localeCompare</code> will compare two strings in the current locale. It returns a negative number indicating if the reference string occurs before the compare string, positive if it occurs after, and <code class=\"language-text\">0</code> if they are equivalent:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"35739840350433650000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const a = 'réservé'; \r\nconst b = 'RESERVE';\r\nconsole.log(a.localeCompare(b)); // 1\r\nconsole.log(a.localeCompare(b, 'en', { sensitivity: 'base' })); // 0`, `35739840350433650000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> a <span class=\"token operator\">=</span> <span class=\"token string\">'réservé'</span><span class=\"token punctuation\">;</span> \r\n<span class=\"token keyword\">const</span> b <span class=\"token operator\">=</span> <span class=\"token string\">'RESERVE'</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>a<span class=\"token punctuation\">.</span><span class=\"token function\">localeCompare</span><span class=\"token punctuation\">(</span>b<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 1</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>a<span class=\"token punctuation\">.</span><span class=\"token function\">localeCompare</span><span class=\"token punctuation\">(</span>b<span class=\"token punctuation\">,</span> <span class=\"token string\">'en'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">sensitivity</span><span class=\"token operator\">:</span> <span class=\"token string\">'base'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 0</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"extracting-sub-strings\" style=\"position:relative;\"><a href=\"#extracting-sub-strings\" aria-label=\"extracting sub strings permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Extracting sub-strings</h2>\n<p>There are three methods which allow you to extract part of a string.</p>\n<h3 id=\"code-classlanguage-textslicecode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textslicecode\" aria-label=\"code classlanguage textslicecode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">slice</code></h3>\n<p><code class=\"language-text\">slice</code> extracts part of a string and returns the extracted part in a new string. It takes two arguments, start position, and end position (the end position will not be included).</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"76567421583052050000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;There is Jam in JamStack!&quot;;\r\nconsole.log(txt.slice(9, 12)); // Jam`, `76567421583052050000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"There is Jam in JamStack!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">slice</span><span class=\"token punctuation\">(</span><span class=\"token number\">9</span><span class=\"token punctuation\">,</span> <span class=\"token number\">12</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Jam</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>If you pass a negative value, it will start from the end of the string:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"99374572984219450000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;There is Jam in JamStack!&quot;;\r\nconsole.log(txt.slice(-16, -13)); // Jam`, `99374572984219450000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"There is Jam in JamStack!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">slice</span><span class=\"token punctuation\">(</span><span class=\"token operator\">-</span><span class=\"token number\">16</span><span class=\"token punctuation\">,</span> <span class=\"token operator\">-</span><span class=\"token number\">13</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Jam</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>You can omit the second parameter, and it will extract from start to the end of the string:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"50920578476226580000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;There is Jam in JamStack!&quot;;\r\nconsole.log(txt.slice(16)); // JamStack!`, `50920578476226580000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"There is Jam in JamStack!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">slice</span><span class=\"token punctuation\">(</span><span class=\"token number\">16</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// JamStack!</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textsubstringcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textsubstringcode\" aria-label=\"code classlanguage textsubstringcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">substring</code></h3>\n<p>The <code class=\"language-text\">substring</code> method is similar to <code class=\"language-text\">slice</code> but it won’t accept negative indexes:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"13408748732559550000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;There is Jam in JamStack!&quot;;\r\nconsole.log(txt.substring(16)); // JamStack!\r\nconsole.log(txt.substring(9, 12)); // Jam`, `13408748732559550000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"There is Jam in JamStack!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">substring</span><span class=\"token punctuation\">(</span><span class=\"token number\">16</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// JamStack!</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">substring</span><span class=\"token punctuation\">(</span><span class=\"token number\">9</span><span class=\"token punctuation\">,</span> <span class=\"token number\">12</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Jam</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textsubstrcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textsubstrcode\" aria-label=\"code classlanguage textsubstrcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">substr</code></h3>\n<p><code class=\"language-text\">substr</code> method is similar to <code class=\"language-text\">slice</code> with one difference that the second parameter is the length of the text to be extracted and not the position:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"72488809824214640000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;There is Jam in JamStack!&quot;;\r\nconsole.log(txt.substr(9, 3)); // Jam`, `72488809824214640000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"There is Jam in JamStack!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">substr</span><span class=\"token punctuation\">(</span><span class=\"token number\">9</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Jam</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>And if you omit the second parameter, it will extract to the end of the string. Furthermore, if the index you pass is negative, it will count from the end:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"18849440454627643000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;There is Jam in JamStack!&quot;;\r\nconsole.log(txt.substr(-9)); // JamStack!`, `18849440454627643000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"There is Jam in JamStack!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">substr</span><span class=\"token punctuation\">(</span><span class=\"token operator\">-</span><span class=\"token number\">9</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// JamStack!</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textsplitcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textsplitcode\" aria-label=\"code classlanguage textsplitcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">split</code></h3>\n<p>Although this method is not directly used for extracting a text value, it’s good for splitting the string value by a character and return an array of substrings:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"22204347747354000000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;There is Jam in JamStack!&quot;;\r\nconst words = txt.split(' ');\r\nconsole.log(words[4]); // JamStack!`, `22204347747354000000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"There is Jam in JamStack!\"</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">const</span> words <span class=\"token operator\">=</span> txt<span class=\"token punctuation\">.</span><span class=\"token function\">split</span><span class=\"token punctuation\">(</span><span class=\"token string\">' '</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>words<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// JamStack!</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>A few points regarding this method:</p>\n<ul>\n<li>The simplest case is a single character, also referred to as delimiter. For example you can split a tab separated value (TSV) by using <code class=\"language-text\">str.split(\"\\t\")</code>.</li>\n<li>If the separator contains multiple characters, that entire string needs to be found.</li>\n<li>If the separator cannot be found, the return value is an array with one element containing the whole string.</li>\n<li>If the separator appears at the beginning or end of the array, it still counts. Meaning the return value is an array with a string value, and one empty string item either at the start or at the end of the array.</li>\n<li>If you pass an empty string <code class=\"language-text\">\"</code> as the separator, it splits the string into single UTF-16 characters.</li>\n</ul>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"20410242529571600000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;There is Jam in JamStack!&quot;;\r\nconsole.log(txt.split('Jam')); // [&quot;There is &quot;, &quot; in &quot;, &quot;Stack!&quot;]\r\nconsole.log(txt.split('test')); // [&quot;There is Jam in JamStack!&quot;]\r\nconsole.log(txt.split('There')); // [&quot;&quot;, &quot; is Jam in JamStack!&quot;]\r\nconsole.log(txt.split('')); // [&quot;T&quot;, &quot;h&quot;, &quot;e&quot;, &quot;r&quot;, &quot;e&quot;, &quot; &quot;, &quot;i&quot;, &quot;s&quot;, &quot; &quot;, &quot;J&quot;, &quot;a&quot;, &quot;m&quot;, &quot; &quot;, &quot;i&quot;, &quot;n&quot;, &quot; &quot;, &quot;J&quot;, &quot;a&quot;, &quot;m&quot;, &quot;S&quot;, &quot;t&quot;, &quot;a&quot;, &quot;c&quot;, &quot;k&quot;, &quot;!&quot;]`, `20410242529571600000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"There is Jam in JamStack!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">split</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Jam'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// [\"There is \", \" in \", \"Stack!\"]</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">split</span><span class=\"token punctuation\">(</span><span class=\"token string\">'test'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// [\"There is Jam in JamStack!\"]</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">split</span><span class=\"token punctuation\">(</span><span class=\"token string\">'There'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// [\"\", \" is Jam in JamStack!\"]</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">split</span><span class=\"token punctuation\">(</span><span class=\"token string\">''</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// [\"T\", \"h\", \"e\", \"r\", \"e\", \" \", \"i\", \"s\", \" \", \"J\", \"a\", \"m\", \" \", \"i\", \"n\", \" \", \"J\", \"a\", \"m\", \"S\", \"t\", \"a\", \"c\", \"k\", \"!\"]</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"replacing-string-content\" style=\"position:relative;\"><a href=\"#replacing-string-content\" aria-label=\"replacing string content permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Replacing string content</h2>\n<p>The <code class=\"language-text\">replace</code> method, as the name suggests, replaces a part of the string with the provided text:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"60934085744176824000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;Who's awesome!&quot;;\r\nconsole.log(txt.replace(&quot;Who's&quot;, &quot;You're&quot;)); // You're awesome!`, `60934085744176824000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"Who's awesome!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">replace</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Who's\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"You're\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// You're awesome!</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<blockquote>\n<p>💡 This method doesn’t change the original string, it returns a new one.</p>\n</blockquote>\n<p>By default, it’s <strong>case sensitive</strong> and just replaces the <strong>first</strong> match:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"12003918785099033000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;This 🐶 is a good 🐶!&quot;;\r\nconsole.log(txt.replace(&quot;This&quot;, &quot;That&quot;)); // This 🐶 is a good 🐶!\r\nconsole.log(txt.replace(&quot;🐶&quot;, &quot;🐕‍🦺&quot;)); // This 🐕‍🦺 is a good 🐶!`, `12003918785099033000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"This 🐶 is a good 🐶!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">replace</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"This\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"That\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// This 🐶 is a good 🐶!</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">replace</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"🐶\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"🐕‍🦺\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// This 🐕‍🦺 is a good 🐶!</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>To do a case insensitive replace or to replace all matches, you could use regex:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"293928642307061950\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;This 🐶 is a good 🐶!&quot;;\r\nconsole.log(txt.replace(/THIS/i, &quot;That&quot;)); // That 🐶 is a good 🐶!\r\nconsole.log(txt.replace(/🐶/g, &quot;🐩&quot;)); // This 🐩 is a good 🐩!`, `293928642307061950`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"This 🐶 is a good 🐶!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">replace</span><span class=\"token punctuation\">(</span><span class=\"token regex\"><span class=\"token regex-delimiter\">/</span><span class=\"token regex-source language-regex\">THIS</span><span class=\"token regex-delimiter\">/</span><span class=\"token regex-flags\">i</span></span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"That\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// That 🐶 is a good 🐶!</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">replace</span><span class=\"token punctuation\">(</span><span class=\"token regex\"><span class=\"token regex-delimiter\">/</span><span class=\"token regex-source language-regex\">🐶</span><span class=\"token regex-delimiter\">/</span><span class=\"token regex-flags\">g</span></span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"🐩\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// This 🐩 is a good 🐩!</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"case-conversion\" style=\"position:relative;\"><a href=\"#case-conversion\" aria-label=\"case conversion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Case conversion</h2>\n<p>To convert a string to uppercase or lowercase you can use <code class=\"language-text\">toUpperCase</code> and <code class=\"language-text\">toLowerCase</code> respectively:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"19324435992572140000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;What's up bro!&quot;;\r\nconsole.log(txt.toLowerCase()); // what's up bro!\r\nconsole.log(txt.toUpperCase()); // WHAT'S UP BRO!`, `19324435992572140000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"What's up bro!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">toLowerCase</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// what's up bro!</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">toUpperCase</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// WHAT'S UP BRO!</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>We also have <code class=\"language-text\">toLocaleLowerCase</code> and <code class=\"language-text\">toLocaleUpperCase</code> methods to convert according to user’s current locale:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"26722484352665820000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const dotted = 'İstanbul';\r\nconsole.log(\\`EN-US: \\${dotted.toLocaleLowerCase('en-US')}\\`); // &quot;i̇stanbul&quot;\r\nconsole.log(\\`TR: \\${dotted.toLocaleLowerCase('tr')}\\`); // &quot;İSTANBUL&quot;`, `26722484352665820000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> dotted <span class=\"token operator\">=</span> <span class=\"token string\">'İstanbul'</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">EN-US: </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>dotted<span class=\"token punctuation\">.</span><span class=\"token function\">toLocaleLowerCase</span><span class=\"token punctuation\">(</span><span class=\"token string\">'en-US'</span><span class=\"token punctuation\">)</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// \"i̇stanbul\"</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">TR: </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>dotted<span class=\"token punctuation\">.</span><span class=\"token function\">toLocaleLowerCase</span><span class=\"token punctuation\">(</span><span class=\"token string\">'tr'</span><span class=\"token punctuation\">)</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// \"İSTANBUL\"</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"concatenation\" style=\"position:relative;\"><a href=\"#concatenation\" aria-label=\"concatenation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Concatenation</h2>\n<p>You can use <code class=\"language-text\">concat</code> to join two strings together (like <code class=\"language-text\">+</code> operator):</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"25438549558745825000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let message = &quot;Hello&quot; + &quot; &quot; + &quot;World!&quot;;\r\nconsole.log(message); // Hello World!\r\nmessage = &quot;Hello&quot;.concat(&quot; &quot;, &quot;World!&quot;);\r\nconsole.log(message); // Hello World!`, `25438549558745825000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">let</span> message <span class=\"token operator\">=</span> <span class=\"token string\">\"Hello\"</span> <span class=\"token operator\">+</span> <span class=\"token string\">\" \"</span> <span class=\"token operator\">+</span> <span class=\"token string\">\"World!\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>message<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Hello World!</span>\r\nmessage <span class=\"token operator\">=</span> <span class=\"token string\">\"Hello\"</span><span class=\"token punctuation\">.</span><span class=\"token function\">concat</span><span class=\"token punctuation\">(</span><span class=\"token string\">\" \"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"World!\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>message<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Hello World!</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"trimming-and-padding\" style=\"position:relative;\"><a href=\"#trimming-and-padding\" aria-label=\"trimming and padding permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Trimming and padding</h2>\n<h3 id=\"code-classlanguage-texttrimcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-texttrimcode\" aria-label=\"code classlanguage texttrimcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">trim</code></h3>\n<p>To remove whitespace from both sides of a string value, you can use the <code class=\"language-text\">trim</code> function:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"2724063562081480000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let message = &quot;     Hello World!     &quot;;\r\nconsole.log(message.trim()); // &quot;Hello World!&quot;`, `2724063562081480000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">let</span> message <span class=\"token operator\">=</span> <span class=\"token string\">\"     Hello World!     \"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>message<span class=\"token punctuation\">.</span><span class=\"token function\">trim</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// \"Hello World!\"</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<blockquote>\n<p>💡 Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.).</p>\n</blockquote>\n<h3 id=\"code-classlanguage-textpadstartcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textpadstartcode\" aria-label=\"code classlanguage textpadstartcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">padStart</code></h3>\n<p>The <code class=\"language-text\">padStart</code> method adds a given string at the start of the original string (multiple times, if needed), until the resulting string reaches the given length.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"92363389375748830000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const str1 = '5';\r\nconsole.log(str1.padStart(6, '0')); // 000005`, `92363389375748830000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> str1 <span class=\"token operator\">=</span> <span class=\"token string\">'5'</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>str1<span class=\"token punctuation\">.</span><span class=\"token function\">padStart</span><span class=\"token punctuation\">(</span><span class=\"token number\">6</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'0'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 000005</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textpadendcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textpadendcode\" aria-label=\"code classlanguage textpadendcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">padEnd</code></h3>\n<p>The opposite of <code class=\"language-text\">padStart</code> is the <code class=\"language-text\">padEnd</code>.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"12379562353294027000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = 'OMG Jam';\r\nconsole.log(txt.padEnd(25, '.')); // OMG Jam..................\r\nconsole.log('OMG Jam'.padEnd(10)); // &quot;OMG Jam   &quot;`, `12379562353294027000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">'OMG Jam'</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">padEnd</span><span class=\"token punctuation\">(</span><span class=\"token number\">25</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'.'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// OMG Jam..................</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'OMG Jam'</span><span class=\"token punctuation\">.</span><span class=\"token function\">padEnd</span><span class=\"token punctuation\">(</span><span class=\"token number\">10</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// \"OMG Jam   \"</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"getting-the-string-value\" style=\"position:relative;\"><a href=\"#getting-the-string-value\" aria-label=\"getting the string value permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Getting the string value</h2>\n<p>There are two methods where you can get the value of a string in JavaScript. You might say, Yas are you crazy, we already have the value in the variable. But remember that I said JavaScript treats a string variable as an object under the hood, so these methods come from the <code class=\"language-text\">Object.prototype</code>.</p>\n<h3 id=\"code-classlanguage-textvalueofcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textvalueofcode\" aria-label=\"code classlanguage textvalueofcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">valueOf</code></h3>\n<p>The <code class=\"language-text\">valueOf</code> returns the primitive value of an object. For string values, JavaScript does it for you under the hood whenever you invoke a method which needs the primitive value. But you can also call this method to get it:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"69964161554818700000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;Yas&quot;;\r\nconsole.log(txt.valueOf()); // &quot;Yas&quot;`, `69964161554818700000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"Yas\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">valueOf</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// \"Yas\"</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-texttostringcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-texttostringcode\" aria-label=\"code classlanguage texttostringcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">toString</code></h3>\n<p>Similar to the above method, <code class=\"language-text\">toString</code> is used to return the value of a string.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"5380673772872280000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const stringObj = new String('Yas');\r\nconsole.log(stringObj); // String {&quot;Yas&quot;}\r\n\r\nconsole.log(stringObj.toString()); // &quot;Yas&quot;`, `5380673772872280000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> stringObj <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">String</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Yas'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>stringObj<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// String {\"Yas\"}</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>stringObj<span class=\"token punctuation\">.</span><span class=\"token function\">toString</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// \"Yas\"</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"code-classlanguage-textrepeatcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textrepeatcode\" aria-label=\"code classlanguage textrepeatcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">repeat</code></h2>\n<p>This method is my personal favourite. You can pass a number to the <code class=\"language-text\">repeat</code> method and it returns your string repeated by that number. It’s really good if you want to have some long text generated for testing purposes:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"50534251309647130000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;Lorem ipsum faked,&quot;;\r\nconsole.log(txt.repeat(5)); // Lorem ipsum faked,Lorem ipsum faked,Lorem ipsum faked,Lorem ipsum faked,Lorem ipsum faked,`, `50534251309647130000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"Lorem ipsum faked,\"</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">repeat</span><span class=\"token punctuation\">(</span><span class=\"token number\">5</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Lorem ipsum faked,Lorem ipsum faked,Lorem ipsum faked,Lorem ipsum faked,Lorem ipsum faked,</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<h2 id=\"character-methods\" style=\"position:relative;\"><a href=\"#character-methods\" aria-label=\"character methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Character methods</h2>\n<h3 id=\"code-classlanguage-textcharatcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textcharatcode\" aria-label=\"code classlanguage textcharatcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">charAt</code></h3>\n<p>The <code class=\"language-text\">charAt</code> returns a new string consisting of the single UTF-16 code unit which is located at the index specified:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"31338558934323335000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;There is Jam in JamStack!&quot;;\r\nconst index = 4;\r\nconsole.log(\\`The character at index \\${index} is \\${txt.charAt(index)}\\`); // &quot;The character at index 4 is r&quot;`, `31338558934323335000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"There is Jam in JamStack!\"</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">const</span> index <span class=\"token operator\">=</span> <span class=\"token number\">4</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">The character at index </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>index<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> is </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">charAt</span><span class=\"token punctuation\">(</span>index<span class=\"token punctuation\">)</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// \"The character at index 4 is r\"</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textcharcodeatcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textcharcodeatcode\" aria-label=\"code classlanguage textcharcodeatcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">charCodeAt</code></h3>\n<p>The <code class=\"language-text\">charCodeAt</code> returns an integer between <code class=\"language-text\">0</code> and <code class=\"language-text\">65535</code> representing the UTF-16 code unit at the given index:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"92142095906910320000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const txt = &quot;There is Jam in JamStack!&quot;;\r\nconst index = 4;\r\nconsole.log(\\`The character code at index \\${index} is \\${txt.charCodeAt(index)}\\`); //The character code at index 4 is 101`, `92142095906910320000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> txt <span class=\"token operator\">=</span> <span class=\"token string\">\"There is Jam in JamStack!\"</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">const</span> index <span class=\"token operator\">=</span> <span class=\"token number\">4</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">The character code at index </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>index<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> is </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>txt<span class=\"token punctuation\">.</span><span class=\"token function\">charCodeAt</span><span class=\"token punctuation\">(</span>index<span class=\"token punctuation\">)</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">//The character code at index 4 is 101</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textcodepointatcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textcodepointatcode\" aria-label=\"code classlanguage textcodepointatcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">codePointAt</code></h3>\n<p>The <code class=\"language-text\">codePointAt</code> method returns a non negative integer representing the Unicode point value of the specified index:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"99880960789157700000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const icons = '☃★♲';\r\nconsole.log(icons.codePointAt(1)); // &quot;9733&quot; \r\n'\\uD800\\uDC00'.codePointAt(0)  // 65536`, `99880960789157700000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> icons <span class=\"token operator\">=</span> <span class=\"token string\">'☃★♲'</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>icons<span class=\"token punctuation\">.</span><span class=\"token function\">codePointAt</span><span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// \"9733\" </span>\r\n<span class=\"token string\">'\\uD800\\uDC00'</span><span class=\"token punctuation\">.</span><span class=\"token function\">codePointAt</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">)</span>  <span class=\"token comment\">// 65536</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"code-classlanguage-textnormalizecode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textnormalizecode\" aria-label=\"code classlanguage textnormalizecode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">normalize</code></h2>\n<p>And last, but not least, the <code class=\"language-text\">normalize</code> method returns the Unicode normalisation form of a string:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"6381058880319923000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const myAlias = '\\u0059\\u0061\\u0073\\u0068\\u0069\\u006e\\u0074\\u0073';\r\nconsole.log(\\`\\${myAlias}\\`); // Yashints`, `6381058880319923000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> myAlias <span class=\"token operator\">=</span> <span class=\"token string\">'\\u0059\\u0061\\u0073\\u0068\\u0069\\u006e\\u0074\\u0073'</span><span class=\"token punctuation\">;</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>myAlias<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Yashints</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>Hope you enjoyed reading this and learnt a few tricks which could help you at what you do day to day. And let’s finish this article with a joke:</p>\n<blockquote>\n<p>What do you call it when Wilhelm II makes a string of bad puns? 👉🏽</p>\n</blockquote>\n<p>A Kaiser roll</p>","timeToRead":8,"frontmatter":{"title":"All you need to know about string in JavaScript 🧵","unformattedDate":"2020-05-11T00:00:00.000Z","date":"May 11, 2020","path":"/string/","author":"Yaser Adel Mehraban","tags":["showdev","webdev","javascript","string"],"thumbnail":null}}},{"node":{"id":"9b8890c9-10f7-509c-8612-814b703680c7","html":"<p>There are many different sorting algorithms out there such as quick sort, merge sort, insertion sort, bubble sort, etc., that could be useful in our day to day life, writing code which gets shipped to production. Knowing all of them is not necessary, but if you have a basic understanding of each one, you can decide on the most efficient one for your scenario.</p>\n<!--more-->\n<h2 id=\"introduction\" style=\"position:relative;\"><a href=\"#introduction\" aria-label=\"introduction permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Introduction</h2>\n<p>Choosing a suboptimal sort algorithm could lead to longer completion time, code complexity, or worse, a program that crashes half way through an operation.</p>\n<p>We use sorting algorithms everyday, <code class=\"language-text\">Array.sort</code> is one of the sort algorithms which is used to sort an array in ascending order. But that is not a solution for every scenario.</p>\n<p>When choosing a sorting algorithm, we need to consider the complexity, or the number of operations performed (usually mentioned as <code class=\"language-text\">O(x)</code>, which is read Big O of x) and also number of swaps along the way. So let’s review and implement some of the most used ones together and learn about their complexity.</p>\n<h2 id=\"bubble-sort\" style=\"position:relative;\"><a href=\"#bubble-sort\" aria-label=\"bubble sort permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Bubble sort</h2>\n<p>The way bubble sort works is very simple. You compare the first item of the collection with the second. If the first one is bigger, then swap the two. If not, move to the second item, and repeat the same. We keep repeating this until we reach the end of the list.</p>\n<p>So far we have bubbled up the biggest item of the list to the far right side in its position. Now we repeat this for the rest of the items again until the list is sorted.</p>\n<p>Let’s see this in action:</p>\n<p><img src=\"/d4c88b8cc620af6af67c33910899fcf7/bubblesort.gif\" alt=\"Bubble sort in action\"></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"6848666656666369000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function bubbleSort(list) {\r\n  let len = list.length;\r\n\r\n  for(let i = len - 1; i >= 0; i--) {\r\n    for(let j = 1; j <= i; j++) {\r\n      if(list[j - 1] > list[j]) {\r\n        let temp = list[j - 1];\r\n        list[j - 1] = list[j];\r\n        list[j] = temp;\r\n      }    \r\n    }  \r\n  }\r\n\r\n  return list;\r\n}\r\n\r\nbubbleSort([7, 5, 2, 3, 9, 6]); // [2, 3, 5, 6, 7, 9]`, `6848666656666369000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">bubbleSort</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">list</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">let</span> len <span class=\"token operator\">=</span> list<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">;</span>\r\n\r\n  <span class=\"token keyword\">for</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> i <span class=\"token operator\">=</span> len <span class=\"token operator\">-</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\">>=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> i<span class=\"token operator\">--</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">for</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> j <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span> j <span class=\"token operator\">&lt;=</span> i<span class=\"token punctuation\">;</span> j<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n      <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>list<span class=\"token punctuation\">[</span>j <span class=\"token operator\">-</span> <span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">></span> list<span class=\"token punctuation\">[</span>j<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n        <span class=\"token keyword\">let</span> temp <span class=\"token operator\">=</span> list<span class=\"token punctuation\">[</span>j <span class=\"token operator\">-</span> <span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n        list<span class=\"token punctuation\">[</span>j <span class=\"token operator\">-</span> <span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> list<span class=\"token punctuation\">[</span>j<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n        list<span class=\"token punctuation\">[</span>j<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> temp<span class=\"token punctuation\">;</span>\r\n      <span class=\"token punctuation\">}</span>    \r\n    <span class=\"token punctuation\">}</span>  \r\n  <span class=\"token punctuation\">}</span>\r\n\r\n  <span class=\"token keyword\">return</span> list<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token function\">bubbleSort</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">,</span> <span class=\"token number\">5</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token number\">9</span><span class=\"token punctuation\">,</span> <span class=\"token number\">6</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// [2, 3, 5, 6, 7, 9]</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>As you can see this algorithm is not optimal, in fact it is one of the heaviest in terms of number of operations in worst-case scenarios. But in terms of swaps, it’s one of the best since it sorts in place.</p>\n<p>The complexity of bubble sort in worst-case is <strong>O(n<sup>2</sup>)</strong>, read as <code class=\"language-text\">big O of n square</code>, where <code class=\"language-text\">n</code> is the number of items in the collection.</p>\n<p>However, in a best case scenario (already sorted collections), it will be <strong>O(n)</strong> with <strong>O(1)</strong> swaps.</p>\n<table>\n<thead>\n<tr>\n<th>Case</th>\n<th>Complexity</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Worst-case performance</td>\n<td>O(n<sup>2</sup>) comparisons</td>\n</tr>\n<tr>\n<td></td>\n<td>O(n<sup>2</sup>) swaps</td>\n</tr>\n<tr>\n<td>Best-case performance</td>\n<td>O(n) comparisons</td>\n</tr>\n<tr>\n<td></td>\n<td>O(1) swaps</td>\n</tr>\n<tr>\n<td>Average-case performance</td>\n<td>O(n<sup>2</sup>) comparisons</td>\n</tr>\n<tr>\n<td></td>\n<td>O(n<sup>2</sup>) swaps</td>\n</tr>\n</tbody>\n</table>\n<h2 id=\"selection-sort\" style=\"position:relative;\"><a href=\"#selection-sort\" aria-label=\"selection sort permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Selection sort</h2>\n<p>Selection sort is really simple like bubble sort. We go through the list, find the index of the lowest element, then swap the lowest element with the first one. Now that the first item is sorted, we repeat this for all remaining elements.</p>\n<p>Let’s see this in action:</p>\n<p><img src=\"/f20b8898585b3ca03843d93ce2c35a68/selectionsort.gif\" alt=\"Selection sort in action\"></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"19502107010841407000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function selectionSort(list) {\r\n  let minIndex, temp,\r\n      len = list.length;\r\n  \r\n  for(let i = 0; i < len; i++) {\r\n    minIndex = i;\r\n    for(let j = i+1; j < len; j++) {\r\n      if(list[j] < list[minIndex]) {\r\n        minIndex = j;\r\n      }\r\n    }\r\n\r\n    temp = list[i];\r\n    list[i] = list[minIndex];\r\n    list[minIndex] = temp;\r\n  }\r\n\r\n  return list;\r\n}\r\n\r\nselectionSort([11, 25, 12, 22, 64]); //[11, 12, 22, 25, 64]`, `19502107010841407000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">selectionSort</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">list</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">let</span> minIndex<span class=\"token punctuation\">,</span> temp<span class=\"token punctuation\">,</span>\r\n      len <span class=\"token operator\">=</span> list<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">;</span>\r\n  \r\n  <span class=\"token keyword\">for</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> i <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\">&lt;</span> len<span class=\"token punctuation\">;</span> i<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    minIndex <span class=\"token operator\">=</span> i<span class=\"token punctuation\">;</span>\r\n    <span class=\"token keyword\">for</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> j <span class=\"token operator\">=</span> i<span class=\"token operator\">+</span><span class=\"token number\">1</span><span class=\"token punctuation\">;</span> j <span class=\"token operator\">&lt;</span> len<span class=\"token punctuation\">;</span> j<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n      <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>list<span class=\"token punctuation\">[</span>j<span class=\"token punctuation\">]</span> <span class=\"token operator\">&lt;</span> list<span class=\"token punctuation\">[</span>minIndex<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n        minIndex <span class=\"token operator\">=</span> j<span class=\"token punctuation\">;</span>\r\n      <span class=\"token punctuation\">}</span>\r\n    <span class=\"token punctuation\">}</span>\r\n\r\n    temp <span class=\"token operator\">=</span> list<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n    list<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> list<span class=\"token punctuation\">[</span>minIndex<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n    list<span class=\"token punctuation\">[</span>minIndex<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> temp<span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n\r\n  <span class=\"token keyword\">return</span> list<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token function\">selectionSort</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">,</span> <span class=\"token number\">25</span><span class=\"token punctuation\">,</span> <span class=\"token number\">12</span><span class=\"token punctuation\">,</span> <span class=\"token number\">22</span><span class=\"token punctuation\">,</span> <span class=\"token number\">64</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">//[11, 12, 22, 25, 64]</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Let’s see how the list is sorted in each iteration in the above example:</p>\n<table>\n<thead>\n<tr>\n<th>Sorted list</th>\n<th>Unsorted sublist</th>\n<th>Lowest elements</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>[]</td>\n<td>[11, 25, 12, 22, 64]</td>\n<td>11</td>\n</tr>\n<tr>\n<td>[11]</td>\n<td>[25, 12, 22, 64]</td>\n<td>12</td>\n</tr>\n<tr>\n<td>[11, 12]</td>\n<td>[25, 22, 64]</td>\n<td>22</td>\n</tr>\n<tr>\n<td>[11, 12, 22]</td>\n<td>[25, 64]</td>\n<td>25</td>\n</tr>\n<tr>\n<td>[11, 12, 22, 25]</td>\n<td>[64]</td>\n<td>64</td>\n</tr>\n<tr>\n<td>[11, 12, 22, 25, 64]</td>\n<td>[]</td>\n<td></td>\n</tr>\n</tbody>\n</table>\n<p>In terms of complexity, this algorithm remains the same regardless of which scenario we’re facing. Which is <strong>O(n<sup>2</sup>)</strong> for comparisons, and <strong>O(n)</strong> swaps. But if you look at the code, it’s self explanatory and simple and sometimes we just want exactly that. In terms or swaps, it’s less than bubble sort.</p>\n<table>\n<thead>\n<tr>\n<th>Case</th>\n<th>Complexity</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Worst-case performance</td>\n<td>O(n<sup>2</sup>) comparisons</td>\n</tr>\n<tr>\n<td></td>\n<td>O(n) swaps</td>\n</tr>\n<tr>\n<td>Best-case performance</td>\n<td>O(n<sup>2</sup>) comparisons</td>\n</tr>\n<tr>\n<td></td>\n<td>O(n) swaps</td>\n</tr>\n<tr>\n<td>Average-case performance</td>\n<td>O(n<sup>2</sup>) comparisons</td>\n</tr>\n<tr>\n<td></td>\n<td>O(n) swaps</td>\n</tr>\n</tbody>\n</table>\n<h2 id=\"insertion-sort\" style=\"position:relative;\"><a href=\"#insertion-sort\" aria-label=\"insertion sort permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Insertion sort</h2>\n<p>This is like when I play cards and someone is handing me the them one by one. I usually put them in my hand in order as I receive them. Insertion sort builds the final list one item at a time. This means it’s less efficient for large lists relative to it’s competitors such as quick sort, or merge sort.</p>\n<p>However, it provides several advantages:</p>\n<ul>\n<li>Simple implementation (we’ll get there shortly).</li>\n<li>Efficient for small data sets.</li>\n<li>More efficient than bubble or selection sorts.</li>\n<li>Adaptive, i.e. efficient for already sorted collections.</li>\n<li>In place.</li>\n<li>Online, can sort a list as it receives it.</li>\n</ul>\n<p>Let’s see how it works in action:</p>\n<p><img src=\"/6e67d1c722106442b422ee53e98575b3/insertionsort.gif\" alt=\"selection sort in action\"></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"22618318792141447000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function insertionSort(list){\r\n  let i, len = list.length, item, j;\r\n\r\n  for(i = 1; i < len; i++){\r\n    item = list[i];\r\n    j = i;\r\n\r\n    while(j > 0 && list[j-1] > item) {\r\n      list[j] = list[j-1];\r\n      j--;\r\n   }\r\n\r\n   list[j] = item;\r\n  }\r\n\r\n  return list;\r\n}`, `22618318792141447000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">insertionSort</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">list</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">let</span> i<span class=\"token punctuation\">,</span> len <span class=\"token operator\">=</span> list<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">,</span> item<span class=\"token punctuation\">,</span> j<span class=\"token punctuation\">;</span>\r\n\r\n  <span class=\"token keyword\">for</span><span class=\"token punctuation\">(</span>i <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\">&lt;</span> len<span class=\"token punctuation\">;</span> i<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\r\n    item <span class=\"token operator\">=</span> list<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n    j <span class=\"token operator\">=</span> i<span class=\"token punctuation\">;</span>\r\n\r\n    <span class=\"token keyword\">while</span><span class=\"token punctuation\">(</span>j <span class=\"token operator\">></span> <span class=\"token number\">0</span> <span class=\"token operator\">&amp;&amp;</span> list<span class=\"token punctuation\">[</span>j<span class=\"token operator\">-</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">></span> item<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n      list<span class=\"token punctuation\">[</span>j<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> list<span class=\"token punctuation\">[</span>j<span class=\"token operator\">-</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n      j<span class=\"token operator\">--</span><span class=\"token punctuation\">;</span>\r\n   <span class=\"token punctuation\">}</span>\r\n\r\n   list<span class=\"token punctuation\">[</span>j<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> item<span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n\r\n  <span class=\"token keyword\">return</span> list<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>In terms of complexity, it’s similar to bubble sort in worst and average cases with <strong>O(n<sup>2</sup>)</strong> for both comparisons and swaps. But in best case, it’s really efficient with <strong>O(n)</strong> comparisons and <strong>O(1)</strong> swaps.</p>\n<table>\n<thead>\n<tr>\n<th>Case</th>\n<th>Complexity</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Worst-case performance</td>\n<td>O(n<sup>2</sup>) comparisons</td>\n</tr>\n<tr>\n<td></td>\n<td>O(n<sup>2</sup>) swaps</td>\n</tr>\n<tr>\n<td>Best-case performance</td>\n<td>O(n) comparisons</td>\n</tr>\n<tr>\n<td></td>\n<td>O(1) swaps</td>\n</tr>\n<tr>\n<td>Average-case performance</td>\n<td>O(n<sup>2</sup>) comparisons</td>\n</tr>\n<tr>\n<td></td>\n<td>O(n<sup>2</sup>) swaps</td>\n</tr>\n</tbody>\n</table>\n<h2 id=\"merge-sort\" style=\"position:relative;\"><a href=\"#merge-sort\" aria-label=\"merge sort permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Merge sort</h2>\n<p>Merge sort is in the divide and conquer algorithms and is implemented with a recursive pattern. We break down the list into small pieces until you have one item in each piece. Then we merge them back together but will compare them and put the items in order.</p>\n<p>It’s really easy to understand, but let’s see it in action:</p>\n<p><img src=\"/a29c0dd0186d1f8cef3c5ebdedf3e5a3/mergesort.gif\" alt=\"merge sort in action\"></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"6130290391282256000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function mergeSort(list) {\r\n   let len = list.length;\r\n   if(len < 2)\r\n      return list;\r\n   let mid = Math.floor(len/2),\r\n       left = list.slice(0,mid),\r\n       right =list.slice(mid);\r\n   \r\n   return merge(mergeSort(left),mergeSort(right));\r\n}\r\n\r\nfunction merge(left, right) {\r\n  let result = [],\r\n      lLen = left.length,\r\n      rLen = right.length,\r\n      l = 0,\r\n      r = 0;\r\n  while(l < lLen && r < rLen) {\r\n     if(left[l] < right[r]) {\r\n       result.push(left[l++]);\r\n     }\r\n     else{\r\n       result.push(right[r++]);\r\n    }\r\n  }  \r\n  \r\n  return result.concat(left.slice(l)).concat(right.slice(r));\r\n}`, `6130290391282256000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">mergeSort</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">list</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n   <span class=\"token keyword\">let</span> len <span class=\"token operator\">=</span> list<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">;</span>\r\n   <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>len <span class=\"token operator\">&lt;</span> <span class=\"token number\">2</span><span class=\"token punctuation\">)</span>\r\n      <span class=\"token keyword\">return</span> list<span class=\"token punctuation\">;</span>\r\n   <span class=\"token keyword\">let</span> mid <span class=\"token operator\">=</span> Math<span class=\"token punctuation\">.</span><span class=\"token function\">floor</span><span class=\"token punctuation\">(</span>len<span class=\"token operator\">/</span><span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\r\n       left <span class=\"token operator\">=</span> list<span class=\"token punctuation\">.</span><span class=\"token function\">slice</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span>mid<span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\r\n       right <span class=\"token operator\">=</span>list<span class=\"token punctuation\">.</span><span class=\"token function\">slice</span><span class=\"token punctuation\">(</span>mid<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n   \r\n   <span class=\"token keyword\">return</span> <span class=\"token function\">merge</span><span class=\"token punctuation\">(</span><span class=\"token function\">mergeSort</span><span class=\"token punctuation\">(</span>left<span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span><span class=\"token function\">mergeSort</span><span class=\"token punctuation\">(</span>right<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token keyword\">function</span> <span class=\"token function\">merge</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">left<span class=\"token punctuation\">,</span> right</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">let</span> result <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\r\n      lLen <span class=\"token operator\">=</span> left<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">,</span>\r\n      rLen <span class=\"token operator\">=</span> right<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">,</span>\r\n      l <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span>\r\n      r <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">while</span><span class=\"token punctuation\">(</span>l <span class=\"token operator\">&lt;</span> lLen <span class=\"token operator\">&amp;&amp;</span> r <span class=\"token operator\">&lt;</span> rLen<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n     <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>left<span class=\"token punctuation\">[</span>l<span class=\"token punctuation\">]</span> <span class=\"token operator\">&lt;</span> right<span class=\"token punctuation\">[</span>r<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n       result<span class=\"token punctuation\">.</span><span class=\"token function\">push</span><span class=\"token punctuation\">(</span>left<span class=\"token punctuation\">[</span>l<span class=\"token operator\">++</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n     <span class=\"token punctuation\">}</span>\r\n     <span class=\"token keyword\">else</span><span class=\"token punctuation\">{</span>\r\n       result<span class=\"token punctuation\">.</span><span class=\"token function\">push</span><span class=\"token punctuation\">(</span>right<span class=\"token punctuation\">[</span>r<span class=\"token operator\">++</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token punctuation\">}</span>\r\n  <span class=\"token punctuation\">}</span>  \r\n  \r\n  <span class=\"token keyword\">return</span> result<span class=\"token punctuation\">.</span><span class=\"token function\">concat</span><span class=\"token punctuation\">(</span>left<span class=\"token punctuation\">.</span><span class=\"token function\">slice</span><span class=\"token punctuation\">(</span>l<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">concat</span><span class=\"token punctuation\">(</span>right<span class=\"token punctuation\">.</span><span class=\"token function\">slice</span><span class=\"token punctuation\">(</span>r<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Merge sort is much better in terms of complexity from previous algorithms. It takes <strong>O(n log n)</strong> operations to sort an array. In terms of memory needed, it’s <strong>O(n)</strong> total with <strong>O(n)</strong> auxiliary if we use array and <strong>O(1)</strong> if we use a linked list.</p>\n<table>\n<thead>\n<tr>\n<th>Case</th>\n<th>Complexity</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Worst-case performance</td>\n<td>O(n log n)</td>\n</tr>\n<tr>\n<td>Best-case performance</td>\n<td>O(n log n)</td>\n</tr>\n<tr>\n<td>Average-case performance</td>\n<td>O(n log n)</td>\n</tr>\n<tr>\n<td>Worst-case space</td>\n<td>O(n) total, O(n) auxiliary with list, O(1) with linked list</td>\n</tr>\n</tbody>\n</table>\n<h2 id=\"quick-sort\" style=\"position:relative;\"><a href=\"#quick-sort\" aria-label=\"quick sort permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Quick sort</h2>\n<p>Quick sort is similar to merge sort, with the difference that the we don’t split the collection in half. We choose a pivot point and split from there. Once we have chosen the pivot point, we put all smaller items to the left and all the larger items to the right of that.</p>\n<p>That means that the pivot point itself is sorted now. We continue this for left and right side recursively until we have the full list sorted.</p>\n<p>Choosing the pivot could be random, middle point, first, or last item of the list. There are many ways to do this each with their own pros and cons.</p>\n<p>Let’s see this in action to better understand the difference:</p>\n<p><img src=\"/42561febe484d44ab4fd5faf2a6d6fb6/quicksort.gif\" alt=\"quick sort in action\"></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"21044969731652730000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function quickSort(list, left, right) {\r\n   let len = list.length, \r\n   pivot,\r\n   partitionIndex;\r\n\r\n\r\n  if(left < right) {\r\n    pivot = right;\r\n    partitionIndex = partition(list, pivot, left, right);\r\n    \r\n   //sort left and right\r\n   quickSort(list, left, partitionIndex - 1);\r\n   quickSort(list, partitionIndex + 1, right);\r\n  }\r\n  return list;\r\n}\r\n\r\nfunction partition(list, pivot, left, right) {\r\n   let pivotValue = list[pivot],\r\n       partitionIndex = left;\r\n\r\n   for(let i = left; i < right; i++) {\r\n    if(list[i] < pivotValue) {\r\n      swap(list, i, partitionIndex);\r\n      partitionIndex++;\r\n    }\r\n  }\r\n  swap(list, right, partitionIndex);\r\n  return partitionIndex;\r\n}\r\n\r\nfunction swap(list, i, j) {\r\n   let temp = list[i];\r\n   list[i] = list[j];\r\n   list[j] = temp;\r\n}\r\n\r\nquickSort([11,8,14,3,6,2,7],0,6); \r\n//[2, 3, 6, 7, 8, 11, 14]`, `21044969731652730000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">quickSort</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">list<span class=\"token punctuation\">,</span> left<span class=\"token punctuation\">,</span> right</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n   <span class=\"token keyword\">let</span> len <span class=\"token operator\">=</span> list<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">,</span> \r\n   pivot<span class=\"token punctuation\">,</span>\r\n   partitionIndex<span class=\"token punctuation\">;</span>\r\n\r\n\r\n  <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>left <span class=\"token operator\">&lt;</span> right<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    pivot <span class=\"token operator\">=</span> right<span class=\"token punctuation\">;</span>\r\n    partitionIndex <span class=\"token operator\">=</span> <span class=\"token function\">partition</span><span class=\"token punctuation\">(</span>list<span class=\"token punctuation\">,</span> pivot<span class=\"token punctuation\">,</span> left<span class=\"token punctuation\">,</span> right<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    \r\n   <span class=\"token comment\">//sort left and right</span>\r\n   <span class=\"token function\">quickSort</span><span class=\"token punctuation\">(</span>list<span class=\"token punctuation\">,</span> left<span class=\"token punctuation\">,</span> partitionIndex <span class=\"token operator\">-</span> <span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n   <span class=\"token function\">quickSort</span><span class=\"token punctuation\">(</span>list<span class=\"token punctuation\">,</span> partitionIndex <span class=\"token operator\">+</span> <span class=\"token number\">1</span><span class=\"token punctuation\">,</span> right<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n  <span class=\"token keyword\">return</span> list<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token keyword\">function</span> <span class=\"token function\">partition</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">list<span class=\"token punctuation\">,</span> pivot<span class=\"token punctuation\">,</span> left<span class=\"token punctuation\">,</span> right</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n   <span class=\"token keyword\">let</span> pivotValue <span class=\"token operator\">=</span> list<span class=\"token punctuation\">[</span>pivot<span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\r\n       partitionIndex <span class=\"token operator\">=</span> left<span class=\"token punctuation\">;</span>\r\n\r\n   <span class=\"token keyword\">for</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> i <span class=\"token operator\">=</span> left<span class=\"token punctuation\">;</span> i <span class=\"token operator\">&lt;</span> right<span class=\"token punctuation\">;</span> i<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>list<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span> <span class=\"token operator\">&lt;</span> pivotValue<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n      <span class=\"token function\">swap</span><span class=\"token punctuation\">(</span>list<span class=\"token punctuation\">,</span> i<span class=\"token punctuation\">,</span> partitionIndex<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n      partitionIndex<span class=\"token operator\">++</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token punctuation\">}</span>\r\n  <span class=\"token punctuation\">}</span>\r\n  <span class=\"token function\">swap</span><span class=\"token punctuation\">(</span>list<span class=\"token punctuation\">,</span> right<span class=\"token punctuation\">,</span> partitionIndex<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">return</span> partitionIndex<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token keyword\">function</span> <span class=\"token function\">swap</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">list<span class=\"token punctuation\">,</span> i<span class=\"token punctuation\">,</span> j</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n   <span class=\"token keyword\">let</span> temp <span class=\"token operator\">=</span> list<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n   list<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> list<span class=\"token punctuation\">[</span>j<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n   list<span class=\"token punctuation\">[</span>j<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> temp<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token function\">quickSort</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">,</span><span class=\"token number\">8</span><span class=\"token punctuation\">,</span><span class=\"token number\">14</span><span class=\"token punctuation\">,</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span><span class=\"token number\">6</span><span class=\"token punctuation\">,</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span><span class=\"token number\">6</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> \r\n<span class=\"token comment\">//[2, 3, 6, 7, 8, 11, 14]</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>As you can see, the more efficient the algorithm gets, the more complex the implementation will be. In terms of complexity, it’s worst than merge sort in worst-case, and equal in average and best.</p>\n<table>\n<thead>\n<tr>\n<th>Case</th>\n<th>Complexity</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Worst-case performance</td>\n<td>O(n<sup>2</sup>)</td>\n</tr>\n<tr>\n<td>Best-case performance</td>\n<td>O(n log n) with simple partition, O(n) with <a href=\"https://en.wikipedia.org/wiki/Quicksort\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">three-way partition</a></td>\n</tr>\n<tr>\n<td>Average-case performance</td>\n<td>O(n log n)</td>\n</tr>\n<tr>\n<td>Worst-case space</td>\n<td>O(n) auxiliary</td>\n</tr>\n</tbody>\n</table>\n<h2 id=\"heap-sort\" style=\"position:relative;\"><a href=\"#heap-sort\" aria-label=\"heap sort permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Heap sort</h2>\n<p>Heap sort is a comparison based sort, you can think of it as an improved version of selection sort. It divides it’s input into a sorted and an unsorted region, then iteratively shrinks the unsorted region by extracting the largest item and inserting it into the sorted region.</p>\n<p>The unsorted region is kept in <a href=\"https://en.wikipedia.org/wiki/Heap_(data_structure)\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">heap data structure</a> to more quickly find the largest item in each step.</p>\n<blockquote>\n<p>💡 Since there is no heap data structure in JavaScript, we can use an array to represent it.</p>\n</blockquote>\n<p>That was a mouthful, so let’s see it in action:</p>\n<p><img src=\"/2d571b4de2faccf2df0c7890db0dc9b7/heapsort.gif\" alt=\"heap sort in action\"></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"26728647865362952000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function heapSort(list) {\r\n  let len = list.length;\r\n  let i = Math.floor(len / 2 - 1);\r\n  let j = len - 1;\r\n\r\n  while(i >= 0) {\r\n    heapify(list, len, i);\r\n\r\n    i--;\r\n  }\r\n\r\n  while(k >= 0) {\r\n    [list[0], list[k]] = [list[k], list[0]];\r\n\r\n    heapify(list, k, 0);\r\n\r\n    k--;\r\n  }\r\n\r\n  return list;\r\n}\r\n\r\nfunction heapify(list, len, i){   \r\n  let largest = i;\r\n  let left = i * 2 + 1;\r\n  let right = left + 1;\r\n\r\n  if(left < len && > list[left] > list[largest]) {\r\n    largest = left;\r\n  }\r\n\r\n  if(right < len && list[right] > list[largest]) {\r\n    largest = right;\r\n  }\r\n\r\n  if(largest != i) {\r\n    [list[i], list[largest]] = [list[largest], list[i]];\r\n\r\n    heapify(list, len, largest);\r\n  }\r\n\r\n  return list;\r\n}`, `26728647865362952000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">heapSort</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">list</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">let</span> len <span class=\"token operator\">=</span> list<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">let</span> i <span class=\"token operator\">=</span> Math<span class=\"token punctuation\">.</span><span class=\"token function\">floor</span><span class=\"token punctuation\">(</span>len <span class=\"token operator\">/</span> <span class=\"token number\">2</span> <span class=\"token operator\">-</span> <span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">let</span> j <span class=\"token operator\">=</span> len <span class=\"token operator\">-</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span>\r\n\r\n  <span class=\"token keyword\">while</span><span class=\"token punctuation\">(</span>i <span class=\"token operator\">>=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token function\">heapify</span><span class=\"token punctuation\">(</span>list<span class=\"token punctuation\">,</span> len<span class=\"token punctuation\">,</span> i<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n    i<span class=\"token operator\">--</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n\r\n  <span class=\"token keyword\">while</span><span class=\"token punctuation\">(</span>k <span class=\"token operator\">>=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token punctuation\">[</span>list<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> list<span class=\"token punctuation\">[</span>k<span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span>list<span class=\"token punctuation\">[</span>k<span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> list<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n\r\n    <span class=\"token function\">heapify</span><span class=\"token punctuation\">(</span>list<span class=\"token punctuation\">,</span> k<span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n    k<span class=\"token operator\">--</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n\r\n  <span class=\"token keyword\">return</span> list<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token keyword\">function</span> <span class=\"token function\">heapify</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">list<span class=\"token punctuation\">,</span> len<span class=\"token punctuation\">,</span> i</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>   \r\n  <span class=\"token keyword\">let</span> largest <span class=\"token operator\">=</span> i<span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">let</span> left <span class=\"token operator\">=</span> i <span class=\"token operator\">*</span> <span class=\"token number\">2</span> <span class=\"token operator\">+</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">let</span> right <span class=\"token operator\">=</span> left <span class=\"token operator\">+</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span>\r\n\r\n  <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>left <span class=\"token operator\">&lt;</span> len <span class=\"token operator\">&amp;&amp;</span> <span class=\"token operator\">></span> list<span class=\"token punctuation\">[</span>left<span class=\"token punctuation\">]</span> <span class=\"token operator\">></span> list<span class=\"token punctuation\">[</span>largest<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    largest <span class=\"token operator\">=</span> left<span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n\r\n  <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>right <span class=\"token operator\">&lt;</span> len <span class=\"token operator\">&amp;&amp;</span> list<span class=\"token punctuation\">[</span>right<span class=\"token punctuation\">]</span> <span class=\"token operator\">></span> list<span class=\"token punctuation\">[</span>largest<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    largest <span class=\"token operator\">=</span> right<span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n\r\n  <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>largest <span class=\"token operator\">!=</span> i<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token punctuation\">[</span>list<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> list<span class=\"token punctuation\">[</span>largest<span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span>list<span class=\"token punctuation\">[</span>largest<span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> list<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n\r\n    <span class=\"token function\">heapify</span><span class=\"token punctuation\">(</span>list<span class=\"token punctuation\">,</span> len<span class=\"token punctuation\">,</span> largest<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n\r\n  <span class=\"token keyword\">return</span> list<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>In the above code snippet, <code class=\"language-text\">heapify</code> function compares three elements, the parent, and two children. It then makes sure that they are in the correct order for a <strong>max heap</strong> since we’re building the heap from bottom up.</p>\n<table>\n<thead>\n<tr>\n<th>Case</th>\n<th>Complexity</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Worst-case performance</td>\n<td>O(n log n)</td>\n</tr>\n<tr>\n<td>Best-case performance</td>\n<td>O(n log n) distinct keys, O(n) with equal keys</td>\n</tr>\n<tr>\n<td>Average-case performance</td>\n<td>O(n log n)</td>\n</tr>\n<tr>\n<td>Worst-case space</td>\n<td>O(n) total, O(1) auxiliary</td>\n</tr>\n</tbody>\n</table>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>You should have a good understanding of these sort algorithms by now. If not, I recommend going through them again and try to write a few examples with a pen and paper. Don’t worry if you have trouble understanding the more complex ones like heap sort. It’s completely OK as I had the same trouble initially. But with practice and trying to implement them I learnt them at the end.</p>\n<p>There are many other sort algorithms out there, so feel free to explore them and compare the way they work with what you’ve learnt so far.</p>\n<p>Thanks for reading and enjoy sorting your collections.</p>\n<blockquote>\n<p>⚡ All the images in this post are pulled from the algorithm’s Wikipedia pages.</p>\n</blockquote>","timeToRead":7,"frontmatter":{"title":"Let's sort with JavaScript 🔢","unformattedDate":"2020-05-03T00:00:00.000Z","date":"May 03, 2020","path":"/sorting/","author":"Yaser Adel Mehraban","tags":["showdev","webdev","javascript","sorting"],"thumbnail":null}}},{"node":{"id":"a1f94a5d-1909-5e69-914d-89aefa4369ea","html":"<p>Using a loop is almost a must in our day to day life. But have you ever thought what kind of loop should you use? Do you know the difference between enumerables and iterables? This article sheds some light in this space, so read on if you’re interested.</p>\n<!--more-->\n<h2 id=\"background\" style=\"position:relative;\"><a href=\"#background\" aria-label=\"background permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Background</h2>\n<p>Looping has seen quite a few changes from when I started programming. I remember using while loops and thinking wow, this is cool (I was printing starts on console using MS-DOS).</p>\n<p>For loop is another way to iterate through a data structure which contains many items. But non of these methods iterate over the actual structure. They use a sequence, usually named <code class=\"language-text\">i</code> which mirrors the identifier for you.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"17264531853744970000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const fruits = ['🍉', '🍎', '🍌'];\r\n\r\nfor (let i = 0; i < fruits.length; i++) {\r\n  console.log(fruits[i]);\r\n}\r\n\r\n// 🍉, 🍎, 🍌`, `17264531853744970000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> fruits <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'🍉'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'🍎'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'🍌'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> i <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\">&lt;</span> fruits<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">;</span> i<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>fruits<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token comment\">// 🍉, 🍎, 🍌</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Here <code class=\"language-text\">i</code> is an index which allows you to access the elements of fruits array and is not part of the array itself. But with progress on <strong>JavaScript</strong>  towards more modern ways of programming, things have changes now. We have <code class=\"language-text\">for...in</code>, and <code class=\"language-text\">for...of</code> loops which use a different mechanism for going through items in a collection.</p>\n<h2 id=\"concepts\" style=\"position:relative;\"><a href=\"#concepts\" aria-label=\"concepts permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Concepts</h2>\n<p>Before we delve into these newer ways of iteration, we need to be on the same page around some concepts. So let’s go through them now:</p>\n<ul>\n<li><strong>Iterables</strong>: This is a kind of loop where we are performing a repetition. Meaning the same set of actions are performed on each item.</li>\n<li><strong>Enumerables</strong>: This is a kind of loop where we making mention of, or counting items one at a time. This means the collection’s elements can be distinctly identified and referenced.</li>\n</ul>\n<p>That didn’t click for me at first, but after going through some examples, it finally made sense. If you consider a <em>full pencil case</em>, that’s an enumerable. When you <em>line up at Aldi</em> to pay, that line is an iterable. A <em>wad of cash</em> is an enumerable, whereas a <em>row of keys on your keyboard</em> is an iterable.</p>\n<blockquote>\n<p>💡 In order for an object to be <code class=\"language-text\">iterable</code>, it MUST implement an <code class=\"language-text\">@@iterator</code> property which returns an iterator. An iterator object is one which has a <code class=\"language-text\">next</code> method which returns the next item in the collection. That’s why an object is not an iterable.</p>\n</blockquote>\n<p>By now you should have started to see the pattern here. The best analogy I came across is:</p>\n<blockquote>\n<p>😍 <strong>Enumerables</strong> are like rectangles whereas <strong>iterables</strong> are squares. This means all <code class=\"language-text\">iterables</code> are <code class=\"language-text\">enumerables</code>, however, not all <code class=\"language-text\">enumerables</code> are <code class=\"language-text\">iterables</code>.</p>\n</blockquote>\n<h2 id=\"code-classlanguage-textforincode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textforincode\" aria-label=\"code classlanguage textforincode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">for...in</code></h2>\n<p>So let’s start from <code class=\"language-text\">enumerables</code>. You can go through enumerables using <code class=\"language-text\">for...in</code>. The use case is mainly to go through key-value pairs in an object:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"91993184911439900000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const superCar = {\r\n  make: 'Lamborghini',\r\n  model: 'Veneno',\r\n  year: '2020'\r\n};\r\n\r\nfor (let key in superCar) {\r\n  console.log(\\`\\${key} --> \\${superCar[key]}\\`);\r\n}\r\n\r\n// make --> Lamborghini\r\n// model --> Veneno\r\n// year --> 2020`, `91993184911439900000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> superCar <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token literal-property property\">make</span><span class=\"token operator\">:</span> <span class=\"token string\">'Lamborghini'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token literal-property property\">model</span><span class=\"token operator\">:</span> <span class=\"token string\">'Veneno'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token literal-property property\">year</span><span class=\"token operator\">:</span> <span class=\"token string\">'2020'</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> key <span class=\"token keyword\">in</span> superCar<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>key<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> --> </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>superCar<span class=\"token punctuation\">[</span>key<span class=\"token punctuation\">]</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token comment\">// make --> Lamborghini</span>\r\n<span class=\"token comment\">// model --> Veneno</span>\r\n<span class=\"token comment\">// year --> 2020</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>You can also use <code class=\"language-text\">for...in</code> to go through index values of an iterable like an array or a string:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"72419494336356745000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let fact = 'Lamborghini is the best!';\r\n\r\nfor (let index in fact) {\r\n  console.log(\\`Index of \\${fact[index]}: \\${index}\\`);\r\n}\r\n\r\n// Index of L: 0\r\n// Index of a: 1\r\n// Index of m: 2\r\n// Index of b: 3\r\n// ...`, `72419494336356745000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">let</span> fact <span class=\"token operator\">=</span> <span class=\"token string\">'Lamborghini is the best!'</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> index <span class=\"token keyword\">in</span> fact<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Index of </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>fact<span class=\"token punctuation\">[</span>index<span class=\"token punctuation\">]</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\">: </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>index<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token comment\">// Index of L: 0</span>\r\n<span class=\"token comment\">// Index of a: 1</span>\r\n<span class=\"token comment\">// Index of m: 2</span>\r\n<span class=\"token comment\">// Index of b: 3</span>\r\n<span class=\"token comment\">// ...</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<blockquote>\n<p>💡 Beware that you can’t iterate on <code class=\"language-text\">Symbol</code> properties with <code class=\"language-text\">for...in</code> and that’s because <code class=\"language-text\">Symbols</code> are primitive data types that are <em>always</em> unique. They are generally used as private properties to avoid name clashes when inheritance is used.</p>\n</blockquote>\n<h2 id=\"using-code-classlanguage-textforofcode\" style=\"position:relative;\"><a href=\"#using-code-classlanguage-textforofcode\" aria-label=\"using code classlanguage textforofcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Using <code class=\"language-text\">for...of</code></h2>\n<p>This kind of loop is applicable to “iterable objects” meaning the item after <code class=\"language-text\">of</code> MUST be an <code class=\"language-text\">iterable</code>:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"98770471580507750000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const fruits = ['🍉', '🍎', '🍌'];\r\n\r\nfor (let fruit of fruits) {\r\n  console.log(\\`\\${fruit} is delicious.\\`);\r\n}\r\n\r\n// 🍉 is delicious.\r\n// 🍎 is delicious.\r\n// 🍌 is delicious.`, `98770471580507750000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> fruits <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'🍉'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'🍎'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'🍌'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> fruit <span class=\"token keyword\">of</span> fruits<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>fruit<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> is delicious.</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token comment\">// 🍉 is delicious.</span>\r\n<span class=\"token comment\">// 🍎 is delicious.</span>\r\n<span class=\"token comment\">// 🍌 is delicious.</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>And again we can use it on strings, but with a slight difference:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"71855282145736960000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let fact = 'Yas';\r\n\r\nfor (let char of fact) {\r\n  console.log(char);\r\n}\r\n\r\n// Y\r\n// a\r\n// s`, `71855282145736960000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">let</span> fact <span class=\"token operator\">=</span> <span class=\"token string\">'Yas'</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> char <span class=\"token keyword\">of</span> fact<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>char<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token comment\">// Y</span>\r\n<span class=\"token comment\">// a</span>\r\n<span class=\"token comment\">// s</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>We saw the difference between these two modern ways of looping through collections, let’s make more informed decisions as to use what where and write cleaner, more readable code 👊🏽.</p>","timeToRead":3,"frontmatter":{"title":"Have you ever thought about for loops? ➰","unformattedDate":"2020-04-24T00:00:00.000Z","date":"Apr 24, 2020","path":"/iterators/","author":"Yaser Adel Mehraban","tags":["showdev","webdev","javascript","forloops"],"thumbnail":null}}},{"node":{"id":"f6f677e2-1b27-5eb6-b4ab-5a13e47a66ed","html":"<p>There are many situations where we want to see what’s wrong with out code without the trouble of changing source code and push the changes again, regardless of whether we’re using local environment or production. Most folks start by writing <code class=\"language-text\">console.log</code> statements throughout their code base and go step by step to find the place where the bug is happening. That’s OK for beginners and where you have access to source code, but what if you don’t want to waste so much time or you don’t even have access to source code?</p>\n<!--more-->\n<h2 id=\"devtools\" style=\"position:relative;\"><a href=\"#devtools\" aria-label=\"devtools permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>DevTools</h2>\n<p>All major browsers have developer tools (aka DevTools) nowadays. It’s important for us web developers to know them well not just because we use them as our daily drivers to test our applications and see how they look like once deployed, but also for times when something is wrong as we want to find them efficiently.</p>\n<p>That’s when the DevTools comes very handy if you know the ropes. DevTools have a lot of functionalities; refer to my articles <a href=\"https://yashints.dev/blog/2019/01/13/chromedevtools\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Chrome DevTools can do that?</a> and <a href=\"https://yashints.dev/blog/2019/08/08/firefox-dev-tools\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">FireFox DevTools can do that?</a> to have a glimpse of some of these. However, in this article we only focus on debugging experience.</p>\n<h2 id=\"the-bug\" style=\"position:relative;\"><a href=\"#the-bug\" aria-label=\"the bug permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The bug</h2>\n<p>In order to be able to have a common ground, we need a bug to fix. I’ve created <a href=\"https://glitch.com/edit/#!/debugger-demo\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">this demo</a> which will be working on in <a href=\"https://glitch.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Glitch</a>. In this demo, you would select a date and press the calculate age button. It will calculate your age and show it in the bottom section.</p>\n<p>However, there is a bug where sometimes it calculates the age by one year higher than actual. We will be looking into how to fix this.</p>\n<h2 id=\"devtoolss-debugger\" style=\"position:relative;\"><a href=\"#devtoolss-debugger\" aria-label=\"devtoolss debugger permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>DevTools’s debugger</h2>\n<p>Both Chrome and Firefox have a debugger section we will be using in this article. I won’t go through Edge because it’s the same as Chrome.</p>\n<h3 id=\"chrome\" style=\"position:relative;\"><a href=\"#chrome\" aria-label=\"chrome permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Chrome</h3>\n<p>You can open your DevTools in Chrome using <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>J</kbd> on Windows or <kbd>Ctrl</kbd>+<kbd>Option</kbd>+<kbd>J</kbd>.</p>\n<p>Once opened, navigate to the <strong>Sources</strong> tab. You will see a file navigator pane on the left where you can inspect the files that the page uses. Then there is the editor pane in the middle where you can click on the file from the navigator pane and see the content and edit them locally in the browser. And last you will see the JavaScript debugging pane where you have a set of features which you’ll learn about shortly.</p>\n<blockquote>\n<p>💡 If you resize the DevTools you might see these panes in different positions.</p>\n</blockquote>\n<p><img src=\"/747a9023138d663f0ee47ceecf64bddf/chromedevtools.jpg\" alt=\"Chrome DevTools Sources tab\"></p>\n<h3 id=\"firefox\" style=\"position:relative;\"><a href=\"#firefox\" aria-label=\"firefox permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Firefox</h3>\n<p>In Firefox you need to use <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>I</kbd> on Windows or <kbd>Ctrl</kbd>+<kbd>Option</kbd>+<kbd>I</kbd> to open up the web developer tools.</p>\n<p>When opened, click on debugger tab which is very similar to what you saw before. On the left, you’ll see the navigation pane, next to it is editor pane and to the left (or below depending on how wide you’ve got your DevTools open) is the debug pane.</p>\n<p><img src=\"/b62cf79f3cab1f58d2a5b86faa600faa/firefoxdevtools.jpg\" alt=\"Firefox DevTools debugger tab\"></p>\n<h2 id=\"setting-a-breakpoint\" style=\"position:relative;\"><a href=\"#setting-a-breakpoint\" aria-label=\"setting a breakpoint permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Setting a breakpoint</h2>\n<p>When it comes to debugging, a common method is to use <code class=\"language-text\">console.log</code> statements throughout the code base which sometimes is abused.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"92384555309282510000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const yo = document.querySelector('#yo');\r\n\r\nfunction getAge() {\r\n    console.log('Getting the date of birth value');\r\n    const dateString = document.querySelector('#age').value;\r\n    console.log(\\`date of birth is \\${dateString}\\`);\r\n    var today = new Date();\r\n    var birthDate = new Date(dateString);\r\n    var age = today.getFullYear() - birthDate.getFullYear();\r\n    console.log(\\`age is \\${age}\\`);\r\n    var m = today.getMonth() - birthDate.getMonth();\r\n    console.log(\\`Birth month is \\${m}\\`);\r\n    if (m < 0 || (m = 0 && today.getDate() < birthDate.getDate())) {\r\n      console.log('The birth month is negative or is zero, we need to reduce the age by one year');\r\n      age--;\r\n      console.log(\\`Real age is \\${age}\\`);\r\n    }\r\n   yo.textContent = age;\r\n}`, `92384555309282510000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> yo <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">querySelector</span><span class=\"token punctuation\">(</span><span class=\"token string\">'#yo'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">function</span> <span class=\"token function\">getAge</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Getting the date of birth value'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token keyword\">const</span> dateString <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">querySelector</span><span class=\"token punctuation\">(</span><span class=\"token string\">'#age'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span>value<span class=\"token punctuation\">;</span>\r\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">date of birth is </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>dateString<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token keyword\">var</span> today <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Date</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token keyword\">var</span> birthDate <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Date</span><span class=\"token punctuation\">(</span>dateString<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token keyword\">var</span> age <span class=\"token operator\">=</span> today<span class=\"token punctuation\">.</span><span class=\"token function\">getFullYear</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">-</span> birthDate<span class=\"token punctuation\">.</span><span class=\"token function\">getFullYear</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">age is </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>age<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token keyword\">var</span> m <span class=\"token operator\">=</span> today<span class=\"token punctuation\">.</span><span class=\"token function\">getMonth</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">-</span> birthDate<span class=\"token punctuation\">.</span><span class=\"token function\">getMonth</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Birth month is </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>m<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>m <span class=\"token operator\">&lt;</span> <span class=\"token number\">0</span> <span class=\"token operator\">||</span> <span class=\"token punctuation\">(</span>m <span class=\"token operator\">=</span> <span class=\"token number\">0</span> <span class=\"token operator\">&amp;&amp;</span> today<span class=\"token punctuation\">.</span><span class=\"token function\">getDate</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">&lt;</span> birthDate<span class=\"token punctuation\">.</span><span class=\"token function\">getDate</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n      console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'The birth month is negative or is zero, we need to reduce the age by one year'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n      age<span class=\"token operator\">--</span><span class=\"token punctuation\">;</span>\r\n      console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Real age is </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>age<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token punctuation\">}</span>\r\n   yo<span class=\"token punctuation\">.</span>textContent <span class=\"token operator\">=</span> age<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>And then you look at the console to see where the bug might be. But this process is painstakingly slow which impacts your productivity a lot. So let’s see how breakpoints help us get to the point real quick.</p>\n<p>Breakpoints have the advantage of being in real time compared to <code class=\"language-text\">console.log</code> statements where you need to wait for the code to get deployed and inspect the console. Apart from that, with <code class=\"language-text\">console.log</code> statements, you need to explicitly mention which values you want to expect whereas in a breakpoint debugger shows you all of them.</p>\n<p>Now let’s take one step back and see how we can go about finding where to set our breakpoint to. In some cases like this you will think OK, the age is correct sometimes and not others depending on the month. So you can find your file and set your breakpoint right where the <code class=\"language-text\">if</code> condition is.</p>\n<p>In some other cases where the code base might be larger, or you might be new to the team, it makes sense to follow the flow of the page. In our case user selects a date and then clicks on the calculate age button.</p>\n<p>The logic behind happens under that click, you the best thing for us is to set a breakpoint on the click event listener. <strong>Event Listener Breakpoints</strong> are designed for this.</p>\n<h3 id=\"chrome-1\" style=\"position:relative;\"><a href=\"#chrome-1\" aria-label=\"chrome 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Chrome</h3>\n<p>In Chrome, in the debug page, click on the <strong>Event Listener Breakpoints</strong> and expand the section. Then find the <em>Mouse</em> category and select the click event.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 383px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/bffd8e2f8f9a2ca1a51cc884534f6f3e/411a4/chromeeventbreakpoint.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 202.22222222222223%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAoABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAMBAgX/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIQAxAAAAH3NA0SplDh2J0AD//EABcQAAMBAAAAAAAAAAAAAAAAAAEQQTD/2gAIAQEAAQUCGNVX/8QAFBEBAAAAAAAAAAAAAAAAAAAAIP/aAAgBAwEBPwFf/8QAFBEBAAAAAAAAAAAAAAAAAAAAIP/aAAgBAgEBPwFf/8QAFhAAAwAAAAAAAAAAAAAAAAAAEDBB/9oACAEBAAY/AjWf/8QAGxAAAgIDAQAAAAAAAAAAAAAAAAERIRBRYZH/2gAIAQEAAT8hh0rRQgj0hyQ9EMWarP8A/9oADAMBAAIAAwAAABAzwDCAD//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQMBAT8QX//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8QX//EAB0QAAICAgMBAAAAAAAAAAAAAAERADFBYRAhcaH/2gAIAQEAAT8QLuEReYndp4MIsseipkvk7hyonTgRYac0QlZY5IEBuuf/2Q=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Event listener break points for mouse click in Chrome\"\n        title=\"\"\n        src=\"/static/bffd8e2f8f9a2ca1a51cc884534f6f3e/411a4/chromeeventbreakpoint.jpg\"\n        srcset=\"/static/bffd8e2f8f9a2ca1a51cc884534f6f3e/6f81f/chromeeventbreakpoint.jpg 270w,\n/static/bffd8e2f8f9a2ca1a51cc884534f6f3e/411a4/chromeeventbreakpoint.jpg 383w\"\n        sizes=\"(max-width: 383px) 100vw, 383px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>Now click on the calculate age button and DevTools will pause when the event listener executes. You might need to click on the <strong>Resume Script Execution</strong> <span class=\"image-inline\"> <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 26px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/270133261d0c3a18a5824163cb358f02/d93cc/resume-script-execution.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 76.92307692307692%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAIAAABr+ngCAAAACXBIWXMAAAsTAAALEwEAmpwYAAABrElEQVR42pWTSyhEURjHb8l4zJAiCxsskOys7JSSlZVSYmWehDyamhIlhYWIspAdWYgizyjFQsojjwXGIxlmMRPGPefce+695sznXGRhGNdZnU7n9/37f9//EwJPeHKbDq0qo+vKyJpy4SNhBfmD2OuT+EUiOMYRvPckr50J1RBXB0INzO1SAHR4JZX3s6FlNSRiKuv/EMIoGr7xk5IeZnFBRjOkN8LKgQ6f3JA8NzNZoWr4deNIfnzGhOCXKF649pPiLhZvg5QGsDhgaV+Hj6+lIg8z2SC+HnI7Ip0z2umtTGVeAYvIAFzo+XxMsOvvFQNsakt5DBGFYlH8LPEX7ALuKNkJ3EJWC1jHtT2vrFKkUWQINjvB4tRLJNkhyQYF7kj/gsI7xVtoCP7gU12Q1qi7EOrAPa2h/yk7INEG2a2R7ln18l4ypPzlObMJasfCO+cyN6wpxjwnOsBsh9JeNrGpBp+JSvVW84b/PWcumNMW6Zh6PbiSJQl/n/NvCct/T1jlYHhxn3JBng8xOmG/Zbusj/XNa4EnIr8L/pztH7fqIUjO7iRuj5MxtuoN0eyZdm4n410AAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"resume button\"\n        title=\"\"\n        src=\"/static/270133261d0c3a18a5824163cb358f02/d93cc/resume-script-execution.png\"\n        srcset=\"/static/270133261d0c3a18a5824163cb358f02/d93cc/resume-script-execution.png 26w\"\n        sizes=\"(max-width: 26px) 100vw, 26px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span> </span> if you’re using a platform like Glitch, but for your own app that won’t be needed. When the breakpoint reaches the button in your HTML, press Step into function <span class=\"image-inline\"> <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 18px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/65a9018e3c40b516ffecc298e9762b79/428de/step-into.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 144.44444444444443%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAdCAIAAAAl5NuSAAAACXBIWXMAAAsTAAALEwEAmpwYAAACEUlEQVR42u1Uy+thcRy9/56Vn7zf5JVneRcLZedVyG6kZCULG2WhPEopimw8ig0ljzwySNSYM787ZLh+Zjs1Z3Hvt+89p3u+53zuJb4/4XQ62Ww2FosluILD4ej1+sPh8MAk/ov/GfHxeAT7crn8uAJrh8PxIDYYDA+c8/lM1Ov1XC4XjUa/XZFIJFQqFZfLvYl5PJ5cLo/H4zcO+Nlslmg2m7jpdDoajfbxCTqdDrbgT/D5/I8rwFSr1el0moDnzWZTqVRgFfZAErwGnoJjsVgKhcJqtfoVGJLY7XatVsvpdAqFwld6ch9Z1mo1UvU77d0nBoOBy+WSSCSUYpFIBHedTockU1Q1mUy8Xq9YLL5/P9bYcbvdo9HoTc/L5TIcDkulUjI2XOElEAjMZrP3QwJLi8UimUwqlUoGg4GS0M2zklpMhjGfzzOZjMfjSaVS0+n0ebxeigFMHs7f7XbH4zHWlBxqMZxDQI4hgDUZ71+dGSb7/X6pVMrn88ViEfXs9/tnPbW40WiEQiGFQoF5QmA+nw+Dsd1u34uHw6HZbGYymfg20DCqYrPZGP5er/dGjNfGYrFbybchQdXwgq/g3jzxoATsdvttku9hNBrX6/UbsdVqpRTjZ/KVmNRHIhGYfLCN2UZsX9kmgWJMJtMtMFzxS9JoNO12+yFw6qqq1WowGIRPmUym1Wr9fn+5XH6u6ifkg/drOVvsKQAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Step into function button\"\n        title=\"\"\n        src=\"/static/65a9018e3c40b516ffecc298e9762b79/428de/step-into.png\"\n        srcset=\"/static/65a9018e3c40b516ffecc298e9762b79/428de/step-into.png 18w\"\n        sizes=\"(max-width: 18px) 100vw, 18px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span> </span> and you’ll end up in our <code class=\"language-text\">getAge</code> function. Once you’ve stepped into the function you wanted, you can go one line at a time by clicking on the step over button or pressing <kbd>F10</kbd>.</p>\n<p><img src=\"/2f49357f26e80756caa7edb7cdf77fbc/breakpointchrome.gif\" alt=\"Mouse click breakpoint in Chrome\"></p>\n<h3 id=\"firefox-1\" style=\"position:relative;\"><a href=\"#firefox-1\" aria-label=\"firefox 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Firefox</h3>\n<p>You can use the same approach to enable a mouse click event listener breakpoint in Firefox, simply expand the <strong>Event Listener Breakpoints</strong> section in the debugger pane, then expand Mouse and select the click event.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 600px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/a06ab13b3e8c1d082551f8bd5a632a91/b4294/firefox-mouseclickbreakpoint.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 132.5925925925926%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAbABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAIDBAX/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIQAxAAAAHtUVyAoWojGcA//8QAGxAAAQQDAAAAAAAAAAAAAAAAEAECAzEAIUL/2gAIAQEAAQUCDl2JLQSX1j7/AP/EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQMBAT8BH//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8BH//EABQQAQAAAAAAAAAAAAAAAAAAADD/2gAIAQEABj8CT//EABoQAAEFAQAAAAAAAAAAAAAAAAABECExsRH/2gAIAQEAAT8h7JAkEuWxL22IlmzP/9oADAMBAAIAAwAAABCw2Q3/xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAEDAQE/EB//xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAECAQE/EB//xAAeEAEAAgEFAQEAAAAAAAAAAAABABEhEDFBUfBxgf/aAAgBAQABPxAIsv7tBRY38gjQuOEjGtKlHUFGvMzJVgPGm35zNj1ocY//2Q=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Firefox mouse click breakpoint\"\n        title=\"\"\n        src=\"/static/a06ab13b3e8c1d082551f8bd5a632a91/b4294/firefox-mouseclickbreakpoint.jpg\"\n        srcset=\"/static/a06ab13b3e8c1d082551f8bd5a632a91/6f81f/firefox-mouseclickbreakpoint.jpg 270w,\n/static/a06ab13b3e8c1d082551f8bd5a632a91/09d21/firefox-mouseclickbreakpoint.jpg 540w,\n/static/a06ab13b3e8c1d082551f8bd5a632a91/b4294/firefox-mouseclickbreakpoint.jpg 600w\"\n        sizes=\"(max-width: 600px) 100vw, 600px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>Once enables, you could follow the same procedure we described before. First click on the calculate age button and the debugger automatically pauses on the first function call. In case of using a platform like Glitch or CodePen, that might not be what you’re after, so simply press the <strong>Resume</strong> <span class=\"image-inline\"> <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 40px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/501c68561b6f855c6cf1de6ce7ace252/92935/firefoxplaypause.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 105%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAVABQDASIAAhEBAxEB/8QAGAABAQADAAAAAAAAAAAAAAAAAAEDBAX/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIQAxAAAAHtXFDZBKAH/8QAGhAAAgIDAAAAAAAAAAAAAAAAAQIAEgMRIP/aAAgBAQABBQJ2qFYMJlOgBd+P/8QAFBEBAAAAAAAAAAAAAAAAAAAAIP/aAAgBAwEBPwEf/8QAFBEBAAAAAAAAAAAAAAAAAAAAIP/aAAgBAgEBPwEf/8QAHhAAAQIHAQAAAAAAAAAAAAAAAQACEBESICExUrH/2gAIAQEABj8C1NYg0npB4FI9t//EABwQAQACAgMBAAAAAAAAAAAAAAEQEQBBMWFx4f/aAAgBAQABPyGmbJaAyxP3qCQUAtxKQN7+YQeZ/9oADAMBAAIAAwAAABBjDzz/xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAEDAQE/EB//xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAECAQE/EB//xAAcEAEBAAEFAQAAAAAAAAAAAAABERAAIUFRcTH/2gAIAQEAAT8QlxW0Cr7rfkjAkV0nDgtDtGAb6aACWQ+k49b+YAgE+xLn/9k='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Firefox resume button\"\n        title=\"\"\n        src=\"/static/501c68561b6f855c6cf1de6ce7ace252/92935/firefoxplaypause.jpg\"\n        srcset=\"/static/501c68561b6f855c6cf1de6ce7ace252/92935/firefoxplaypause.jpg 40w\"\n        sizes=\"(max-width: 40px) 100vw, 40px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span> </span> button until you see the breakpoint stop at the <code class=\"language-text\">getAge</code> function. Then click on the <strong>Step In</strong> <span class=\"image-inline\"> <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 24px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/b79eefda6281883132a3243493b5955e/0bf61/firefoxstepin.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 83.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAARABQDASIAAhEBAxEB/8QAGAABAAMBAAAAAAAAAAAAAAAAAAECBAX/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIQAxAAAAHuVZjagWBAP//EABoQAQEAAgMAAAAAAAAAAAAAAAECABAREiD/2gAIAQEAAQUCXjJrtq0CSpfH/8QAFBEBAAAAAAAAAAAAAAAAAAAAIP/aAAgBAwEBPwEf/8QAFBEBAAAAAAAAAAAAAAAAAAAAIP/aAAgBAgEBPwEf/8QAHBAAAgICAwAAAAAAAAAAAAAAAREAIQJBECAy/9oACAEBAAY/AvJMNELiyo8zT1vr/8QAGxABAQEAAgMAAAAAAAAAAAAAAREAEDFRcZH/2gAIAQEAAT8hbp6Mx9wUR4mUrpDIz1RD9YaXzjHH/9oADAMBAAIAAwAAABATwDz/xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAEDAQE/EB//xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAECAQE/EB//xAAdEAEAAgICAwAAAAAAAAAAAAABESEAMRCBQWGR/9oACAEBAAE/EDy63On1McJUAzPS8UT2yUTzQ4DtGGS6UesAg0JJzZzbj//Z'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Firefox step in button\"\n        title=\"\"\n        src=\"/static/b79eefda6281883132a3243493b5955e/0bf61/firefoxstepin.jpg\"\n        srcset=\"/static/b79eefda6281883132a3243493b5955e/0bf61/firefoxstepin.jpg 24w\"\n        sizes=\"(max-width: 24px) 100vw, 24px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span> </span> button or press <kbd>F11</kbd> to go inside the function. Once you’re inside the function simply press <strong>Step over</strong> <span class=\"image-inline\"> <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 18px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/28df88b8fcd63653687b9d1528a2c4a1/156d6/firefoxstepover.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 88.88888888888889%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAASABQDASIAAhEBAxEB/8QAGQABAAMBAQAAAAAAAAAAAAAAAAECAwQF/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAAB9yKYnYoIzDoB/8QAHBAAAgICAwAAAAAAAAAAAAAAAQIAAxASETIz/9oACAEBAAEFAiQIGDY15st2iDVX6UeE/8QAFBEBAAAAAAAAAAAAAAAAAAAAIP/aAAgBAwEBPwEf/8QAFBEBAAAAAAAAAAAAAAAAAAAAIP/aAAgBAgEBPwEf/8QAHBAAAgIDAQEAAAAAAAAAAAAAAAECIRARcUGB/9oACAEBAAY/Areinht/DcYX40JEuEOY/8QAHBABAAEEAwAAAAAAAAAAAAAAAQAQETFhIaHh/9oACAEBAAE/ITbkNzHTTiO4FpBcK+yacneVD//aAAwDAQACAAMAAAAQ4A88/8QAFBEBAAAAAAAAAAAAAAAAAAAAIP/aAAgBAwEBPxAf/8QAFBEBAAAAAAAAAAAAAAAAAAAAIP/aAAgBAgEBPxAf/8QAHRABAAICAgMAAAAAAAAAAAAAAREhADFRgRBB8P/aAAgBAQABPxCxwRKjJsXLQbOsEdM4ImCBsOWOdY8BCVHYGK3qcvVUWvtyrn2ONUVWFvj/2Q=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Firefox step over button\"\n        title=\"\"\n        src=\"/static/28df88b8fcd63653687b9d1528a2c4a1/156d6/firefoxstepover.jpg\"\n        srcset=\"/static/28df88b8fcd63653687b9d1528a2c4a1/156d6/firefoxstepover.jpg 18w\"\n        sizes=\"(max-width: 18px) 100vw, 18px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span>  </span> button or <kbd>F11</kbd> to go line by line from thereon.</p>\n<p><img src=\"/252ca5c56aa00b316ffccba44665a81b/firefoxclickbreakpoint.gif\" alt=\"Firefox mouse click breakpoint\"></p>\n<h2 id=\"line-breakpoint\" style=\"position:relative;\"><a href=\"#line-breakpoint\" aria-label=\"line breakpoint permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Line breakpoint</h2>\n<p>Line breakpoints are mainly used when you’ve narrowed down where the bug might actually be. In our case when we stepped through the <code class=\"language-text\">getAge</code> function, we saw that the age is calculated based on the year, then there is an if condition which will be responsible to reduce the age by one if the month is less than the current month minus the month of birth.</p>\n<p>So we roughly know that if the age is calculated right in some cases and wrong by one year in others, the if condition is where we should set our line breakpoint on.</p>\n<p>There are two ways to do this in DevTools, one is to follow the event listener breakpoint flow explained above. But if you know the filename beforehand, you can simply open the file in the editor pane and scroll through until you reach the line you want.</p>\n<p>Once you’re there, simply click the line number and it will put a <strong>Line Breakpoint</strong> <span class=\"image-inline\">  <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 34px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/18dd708cb3542c6fb2b4bd9d923f3f88/fec30/linebreakpoint.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 47.05882352941176%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAJABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAMCBf/EABUBAQEAAAAAAAAAAAAAAAAAAAQF/9oADAMBAAIQAxAAAAGe4qIO6snu/8QAGRAAAgMBAAAAAAAAAAAAAAAAAAIBAxAy/9oACAEBAAEFAhSiWmrF5//EABYRAQEBAAAAAAAAAAAAAAAAAAABMf/aAAgBAwEBPwFMf//EABYRAQEBAAAAAAAAAAAAAAAAAAEAMf/aAAgBAgEBPwFJ2//EABcQAQEBAQAAAAAAAAAAAAAAAAEAERD/2gAIAQEABj8CnVjel//EABwQAAEEAwEAAAAAAAAAAAAAAAEAESFREHGRof/aAAgBAQABPyFzZ6mEMERKKg6N58y//9oADAMBAAIAAwAAABDM7//EABYRAQEBAAAAAAAAAAAAAAAAAAEAEf/aAAgBAwEBPxAUY4C//8QAFhEBAQEAAAAAAAAAAAAAAAAAAQAR/9oACAECAQE/EBSepy//xAAeEAACAQMFAAAAAAAAAAAAAAAAASEQEUFRcZGh8f/aAAgBAQABPxD3xKIVza+di/mcpjlMOmUdGf/Z'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Line Breakpoint icon\"\n        title=\"\"\n        src=\"/static/18dd708cb3542c6fb2b4bd9d923f3f88/fec30/linebreakpoint.jpg\"\n        srcset=\"/static/18dd708cb3542c6fb2b4bd9d923f3f88/fec30/linebreakpoint.jpg 34w\"\n        sizes=\"(max-width: 34px) 100vw, 34px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span> </span> icon on that line so that you’d know where the breakpoint is set. You can remove the breakpoint by simply clicking on it again.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 478px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/f26cdc68eb50c273efdd2c7472a3219d/0c439/linebreakpointcode.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 48.888888888888886%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAKABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAECBf/EABUBAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAHuVtIpf//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEAAQUCX//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEABj8CX//EABkQAQACAwAAAAAAAAAAAAAAAAEAIREgUf/aAAgBAQABPyG2whnmv//aAAwDAQACAAMAAAAQzw//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/ED//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAaEAACAwEBAAAAAAAAAAAAAAABEQAhMRDR/9oACAEBAAE/EDUQGGiMifCgzI77/9k='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Line breakpoint\"\n        title=\"\"\n        src=\"/static/f26cdc68eb50c273efdd2c7472a3219d/0c439/linebreakpointcode.jpg\"\n        srcset=\"/static/f26cdc68eb50c273efdd2c7472a3219d/6f81f/linebreakpointcode.jpg 270w,\n/static/f26cdc68eb50c273efdd2c7472a3219d/0c439/linebreakpointcode.jpg 478w\"\n        sizes=\"(max-width: 478px) 100vw, 478px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>Now if you click the calculate age button, the program stops on the if condition. This part is exactly the same in both Chrome and Firefox.</p>\n<h2 id=\"checking-variable-values\" style=\"position:relative;\"><a href=\"#checking-variable-values\" aria-label=\"checking variable values permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Checking variable values</h2>\n<p>When you’re stopped at the if condition, if you want to see what is the value of a variable, simply have a look at the debugger pane’s <strong>Scope</strong> section in Chrome or <strong>Scopes</strong> section in Firefox.</p>\n<h3 id=\"chrome-2\" style=\"position:relative;\"><a href=\"#chrome-2\" aria-label=\"chrome 2 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Chrome</h3>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 948px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/49ae1e69c058eab1ab6c6609c5be18be/64e16/chromescope.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 38.51851851851852%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAIABQDASIAAhEBAxEB/8QAFwABAAMAAAAAAAAAAAAAAAAAAAECBf/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhADEAAAAd2AsD//xAAWEAEBAQAAAAAAAAAAAAAAAAAAEUH/2gAIAQEAAQUCjX//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/AT//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAUEAEAAAAAAAAAAAAAAAAAAAAQ/9oACAEBAAY/An//xAAZEAABBQAAAAAAAAAAAAAAAAARABAhQVH/2gAIAQEAAT8hLULS3//aAAwDAQACAAMAAAAQcA//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/ED//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAaEAADAAMBAAAAAAAAAAAAAAAAAREhQVGB/9oACAEBAAE/EMmvgpam0Gl2H//Z'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Scope section in Chrome&#39;s debug pane\"\n        title=\"\"\n        src=\"/static/49ae1e69c058eab1ab6c6609c5be18be/64e16/chromescope.jpg\"\n        srcset=\"/static/49ae1e69c058eab1ab6c6609c5be18be/6f81f/chromescope.jpg 270w,\n/static/49ae1e69c058eab1ab6c6609c5be18be/09d21/chromescope.jpg 540w,\n/static/49ae1e69c058eab1ab6c6609c5be18be/64e16/chromescope.jpg 948w\"\n        sizes=\"(max-width: 948px) 100vw, 948px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<h3 id=\"firefox-2\" style=\"position:relative;\"><a href=\"#firefox-2\" aria-label=\"firefox 2 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Firefox</h3>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 886px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/0a4db696d8d23420ec7288a50cc025a8/72f09/firefoxscopes.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 53.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAIBBf/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhADEAAAAe6uSWj/xAAXEAADAQAAAAAAAAAAAAAAAAAAARAR/9oACAEBAAEFAsHXP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABYQAAMAAAAAAAAAAAAAAAAAABAgMf/aAAgBAQAGPwIRP//EABsQAAICAwEAAAAAAAAAAAAAAAABEXEQMUFR/9oACAEBAAE/IbEoXaJfvGgj/9oADAMBAAIAAwAAABCb7//EABcRAQADAAAAAAAAAAAAAAAAAAABEVH/2gAIAQMBAT8QVOv/xAAWEQEBAQAAAAAAAAAAAAAAAAAAIVH/2gAIAQIBAT8QTH//xAAcEAEAAgEFAAAAAAAAAAAAAAABABExEFGRodH/2gAIAQEAAT8QENnSVWGewlmec91FRP/Z'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Scopes section in Chrome&#39;s debug pane\"\n        title=\"\"\n        src=\"/static/0a4db696d8d23420ec7288a50cc025a8/72f09/firefoxscopes.jpg\"\n        srcset=\"/static/0a4db696d8d23420ec7288a50cc025a8/6f81f/firefoxscopes.jpg 270w,\n/static/0a4db696d8d23420ec7288a50cc025a8/09d21/firefoxscopes.jpg 540w,\n/static/0a4db696d8d23420ec7288a50cc025a8/72f09/firefoxscopes.jpg 886w\"\n        sizes=\"(max-width: 886px) 100vw, 886px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<h2 id=\"spotting-the-bug\" style=\"position:relative;\"><a href=\"#spotting-the-bug\" aria-label=\"spotting the bug permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Spotting the bug</h2>\n<p>Now let’s see where the bug is and how to fix it. If you have a look at the if condition more closely, you’ll see that there are two main parts in there. First one is <code class=\"language-text\">m &lt; 0</code> to check whether the month is less than zero, and the second is <code class=\"language-text\">m = 0 &amp;&amp; today.getDate() &lt; birthDate.getDate()</code> to check if the month is zero and today is less than birthday.</p>\n<p>Now we know that someone’s used an equal sign instead of equality operator which has caused this bug. So let’s replace the <code class=\"language-text\">=</code> with <code class=\"language-text\">===</code> and then press <kbd>Ctrl</kbd>+<kbd>S</kbd> to save the changes. You can leave the breakpoint or remove it test with some dates to see if the bug is fixed or not. But after some tests we know that the bug is fixed. It’s time for you to go and write a test for this function so you won’t face the same bug again 😁.</p>\n<h2 id=\"console-window\" style=\"position:relative;\"><a href=\"#console-window\" aria-label=\"console window permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Console window</h2>\n<p>Don’t forget that you can evaluate your expressions in the console window of the browser. As in you could simply put your condition in the console and see whether it’s returning <code class=\"language-text\">true</code> or <code class=\"language-text\">false</code>.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 410px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/f797b0fd457c2e9bb9dbbedf0fcafcca/0d3fb/consoleexpressionevaluation.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 21.85185185185185%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAEABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAEF/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAAB3gUH/8QAFBABAAAAAAAAAAAAAAAAAAAAEP/aAAgBAQABBQJ//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAFBABAAAAAAAAAAAAAAAAAAAAEP/aAAgBAQAGPwJ//8QAFhABAQEAAAAAAAAAAAAAAAAAAAER/9oACAEBAAE/IYxH/9oADAMBAAIAAwAAABAAD//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8QP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8QP//EABcQAAMBAAAAAAAAAAAAAAAAAAABEeH/2gAIAQEAAT8QSrCZiEih/9k='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Evaluating expressions in console window\"\n        title=\"\"\n        src=\"/static/f797b0fd457c2e9bb9dbbedf0fcafcca/0d3fb/consoleexpressionevaluation.jpg\"\n        srcset=\"/static/f797b0fd457c2e9bb9dbbedf0fcafcca/6f81f/consoleexpressionevaluation.jpg 270w,\n/static/f797b0fd457c2e9bb9dbbedf0fcafcca/0d3fb/consoleexpressionevaluation.jpg 410w\"\n        sizes=\"(max-width: 410px) 100vw, 410px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<h2 id=\"what-else\" style=\"position:relative;\"><a href=\"#what-else\" aria-label=\"what else permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What else?</h2>\n<p>There are heaps of other breakpoints you could use such as conditional breakpoints which are triggered only if a condition you provide is <code class=\"language-text\">true</code>, breakpoints on caught and uncaught exceptions, and XHR breakpoints which are triggered when a URL matches a substring you’ve set. Let’s try a XHR breakpoint in , open up your DevTools, head over to debug pane and open the <strong>XHR Breakpoints</strong> section. Click in the box to add a line and enter <code class=\"language-text\">raw</code> and press <kbd>Enter</kbd>. Now refresh the page and you’ll see the breakpoint hit when the request is made. You can now use the same technique we saw earlier to step in the code and set a like breakpoint to debug further.</p>\n<h2 id=\"small-catch\" style=\"position:relative;\"><a href=\"#small-catch\" aria-label=\"small catch permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Small catch</h2>\n<p>One catch is that you need to make sure that the function you’re debugging is in the scope which is tricky if you’re using DevTools. In order to get it working you need to set a line breakpoint somewhere where the function is in scope, then trigger the breakpoint and call <code class=\"language-text\">debug()</code> in the DevTools console while the code is still paused on the line breakpoint.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"37435533102641050000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`(function () {\r\n  function hello() {\r\n    console.log('hello');\r\n  }\r\n  function world() {\r\n    console.log(' world');\r\n  }\r\n  hello(yo); // This works.\r\n  world();\r\n})();\r\ndebug(hello); // This doesn't work. hey() is out of scope.`, `37435533102641050000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token punctuation\">(</span><span class=\"token keyword\">function</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">function</span> <span class=\"token function\">hello</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'hello'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n  <span class=\"token keyword\">function</span> <span class=\"token function\">world</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">' world'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n  <span class=\"token function\">hello</span><span class=\"token punctuation\">(</span>yo<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// This works.</span>\r\n  <span class=\"token function\">world</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token function\">debug</span><span class=\"token punctuation\">(</span>hello<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// This doesn't work. hey() is out of scope.</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>We saw how powerful the DevTools are and how much they can help us to find and fix bugs in our code. So stop using <code class=\"language-text\">console.log</code> and get to know your DevTools better. Happy coding from home ❤️.</p>","timeToRead":7,"frontmatter":{"title":"Stop using `console.log`, start using your DevTools 🛠️","unformattedDate":"2020-03-19T00:00:00.000Z","date":"Mar 19, 2020","path":"/debug-devtools/","author":"Yaser Adel Mehraban","tags":["webdev","showdev","debugging","javascript"],"thumbnail":null}}},{"node":{"id":"9cd5ea9a-ed6b-5a7d-bae5-f81e9ccd3c33","html":"<p>There are many situations where we’d like to see our web page in fullscreen mode. Be it games, online courses, video tutorials, or simply wanting more reading space while reading a book. What we usually do in these sort of scenarios is we manually set our browser’s tab/window in fullscreen mode (<kbd>F11</kbd>), but the result is not always what we expect, because some content is not designed to be viewed in fullscreen mode. Besides, what if we help our users to go fullscreen automatically instead, again it all comes to having a better user experience.</p>\n<!--more-->\n<h2 id=\"intro\" style=\"position:relative;\"><a href=\"#intro\" aria-label=\"intro permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Intro</h2>\n<p><a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Full Screen API</a> adds methods to the HTML <code class=\"language-text\">Element</code> to enable us to programmatically turn any content on a page to enter in fullscreen mode via JavaScript. <code class=\"language-text\">WebGL</code>, <code class=\"language-text\">canvas</code>, and <code class=\"language-text\">video</code> elements are the usual targets when it comes to going fullscreen. Online newspapers and magazines are other areas where this can be helpful.</p>\n<p>So let’s see this in action first and then go through some details:</p>\n<iframe height=\"265\" style=\"width: 100%;\" scrolling=\"no\" title=\"Full Screen API Demo\" src=\"https://codepen.io/yashints/embed/preview/mdJpqYx?height=265&theme-id=light&default-tab=js,result\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\">\r\n  See the Pen <a href='https://codepen.io/yashints/pen/mdJpqYx'>Full Screen API Demo</a> by Yaser Adel Mehraban\r\n  (<a href='https://codepen.io/yashints'>@yashints</a>) on <a href='https://codepen.io'>CodePen</a>.\r\n</iframe>\n<p>All we’re doing here is to call a method which is added by the Full Screen API:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"22729507716533748000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const vid = document.getElementById('shuttle');\r\n\r\nfunction toggleFullScreen() {\r\n  if (!document.fullscreenElement && vid.requestFullscreen) {\r\n    vid.requestFullscreen();\r\n  } else {\r\n    if (document.exitFullscreen) {\r\n      document.exitFullscreen(); \r\n    }\r\n  }\r\n}\r\n\r\ndocument.addEventListener(&quot;keydown&quot;, function(e) {\r\n  if (e.keyCode == 13) {\r\n    toggleFullScreen();\r\n  }\r\n}, false);`, `22729507716533748000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> vid <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">getElementById</span><span class=\"token punctuation\">(</span><span class=\"token string\">'shuttle'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">function</span> <span class=\"token function\">toggleFullScreen</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>document<span class=\"token punctuation\">.</span>fullscreenElement <span class=\"token operator\">&amp;&amp;</span> vid<span class=\"token punctuation\">.</span>requestFullscreen<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    vid<span class=\"token punctuation\">.</span><span class=\"token function\">requestFullscreen</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>document<span class=\"token punctuation\">.</span>exitFullscreen<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n      document<span class=\"token punctuation\">.</span><span class=\"token function\">exitFullscreen</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> \r\n    <span class=\"token punctuation\">}</span>\r\n  <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\ndocument<span class=\"token punctuation\">.</span><span class=\"token function\">addEventListener</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"keydown\"</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">e</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>e<span class=\"token punctuation\">.</span>keyCode <span class=\"token operator\">==</span> <span class=\"token number\">13</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token function\">toggleFullScreen</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"exposed-properties\" style=\"position:relative;\"><a href=\"#exposed-properties\" aria-label=\"exposed properties permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Exposed properties</h2>\n<p>There are two properties exposed on the <code class=\"language-text\">document</code> object:</p>\n<p>💡 <strong><code class=\"language-text\">DocumentOrShadowRoot.fullscreenElement</code></strong></p>\n<p>The <code class=\"language-text\">fullscreenElement</code> property will give you the element that is displayed in fullscreen mode at the moment. It’s a way to check whether we’re in full screen mode or not.</p>\n<p>💡 <strong><code class=\"language-text\">Document.fullscreenEnabled</code></strong></p>\n<p>The <code class=\"language-text\">fullscreenEnabled</code> property tells you whether or not the document is currently in a state that would allow fullscreen mode to be requested.</p>\n<h2 id=\"exiting-fullscreen\" style=\"position:relative;\"><a href=\"#exiting-fullscreen\" aria-label=\"exiting fullscreen permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Exiting fullscreen</h2>\n<p>You’ll want to be sure to let your users know that they can press the <kbd>Esc</kbd> key (or <kbd>F11</kbd>) to exit fullscreen mode.</p>\n<p>In addition, navigating to another page, changing tabs, or switching to another application (using, for example, <kbd>Alt</kbd>-<kbd>Tab</kbd>) while in fullscreen mode exits fullscreen mode as well.</p>\n<p>As you saw in the example above, exiting the fullscreen mode can be done using <code class=\"language-text\">document.exitFullscreen</code>:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"96053432520295240000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`if (document.exitFullscreen) {\r\n  document.exitFullscreen(); \r\n}`, `96053432520295240000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>document<span class=\"token punctuation\">.</span>exitFullscreen<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  document<span class=\"token punctuation\">.</span><span class=\"token function\">exitFullscreen</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> \r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"feature-detection\" style=\"position:relative;\"><a href=\"#feature-detection\" aria-label=\"feature detection permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Feature detection</h2>\n<p>You can simply detect whether the feature is supported or not using either <code class=\"language-text\">document.requestFullscreen</code> or <code class=\"language-text\">Element.requestFullscreen</code>:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"61390558769400760000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`if(element.requestFullscreen) {\r\n  element.requestFullscreen();\r\n}`, `61390558769400760000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>element<span class=\"token punctuation\">.</span>requestFullscreen<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  element<span class=\"token punctuation\">.</span><span class=\"token function\">requestFullscreen</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>or:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"27647473571824845000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`if(document.requestFullscreen) {\r\n  // enter fullscreen mode\r\n}`, `27647473571824845000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>document<span class=\"token punctuation\">.</span>requestFullscreen<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token comment\">// enter fullscreen mode</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"finding-whether-or-not-youre-in-fullscreen-mode\" style=\"position:relative;\"><a href=\"#finding-whether-or-not-youre-in-fullscreen-mode\" aria-label=\"finding whether or not youre in fullscreen mode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Finding whether or not you’re in fullscreen mode</h2>\n<p>As mentioned earlier, the <code class=\"language-text\">document.fullscreenElement</code> is a good way to check whether the browser is in full screen mode or not. The value of this property will be <code class=\"language-text\">null</code> if we’re not in fullscreen, and the current fullscreen element otherwise.</p>\n<div class=\"custom-block warning\"><div class=\"custom-block-body\"> ⚠️ There is also a <code class=\"language-text\">boolean</code> property called <code class=\"language-text\">fullscreen</code> which is deprecated and should not be used. Although most of the browsers still support it.</div></div>\n<h2 id=\"presentation\" style=\"position:relative;\"><a href=\"#presentation\" aria-label=\"presentation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Presentation</h2>\n<p>When toggling fullscreen mode programmatically, you’re responsible for how the elements will look like and what appears on the screen. It’s obvious you will need to change the width and height of the element to <code class=\"language-text\">%100</code>, and fortunately, there is a CSS pseudo-selector for you to use:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"79290295442521050000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`#shuttle:fullscreen {\r\n  width: 100%;\r\n  height: 100%;\r\n}`, `79290295442521050000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"css\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-css line-numbers\"><code class=\"language-css\"><span class=\"token selector\">#shuttle:fullscreen</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">width</span><span class=\"token punctuation\">:</span> 100%<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">height</span><span class=\"token punctuation\">:</span> 100%<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Also you might want to hide some other elements on the screen to reduce clutter:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"83949105200753750000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`:fullscreen .hide {\r\n  display: none;\r\n}`, `83949105200753750000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"css\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-css line-numbers\"><code class=\"language-css\"><span class=\"token selector\">:fullscreen .hide</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">display</span><span class=\"token punctuation\">:</span> none<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"browser-support\" style=\"position:relative;\"><a href=\"#browser-support\" aria-label=\"browser support permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Browser support</h2>\n<p>The browser support is really good at this point, almost all major browsers fully support it. Only <strong>IE11</strong> and <strong>Safari</strong> have partial support. Opera mini and Android Browser do not support it at this point.</p>\n<p><img src=\"/2f6bdf83bea61460d60ed9d65117439e/caniuse.png\" alt=\"Browser support for Full Screen API\"></p>\n<p>Hope you’ve learned a new trick if you didn’t know about this API and happy fullscreening (just made up the word 😁).</p>","timeToRead":2,"frontmatter":{"title":"What you need to know about Full screen API 💻","unformattedDate":"2020-03-10T00:00:00.000Z","date":"Mar 10, 2020","path":"/fullscreen-api/","author":"Yaser Adel Mehraban","tags":["webdev","showdev","fullscreenapi","javascript"],"thumbnail":null}}},{"node":{"id":"f661f4c6-0573-512d-9b8f-6bfdad856cd1","html":"<p>I’ve been writing web applications for more than a decade. From <em>classic ASP</em> to <em>PHP</em>, <em>ASP.Net web forms</em> and the list goes on and on. However, something that’s been common between all those years for me has been to care about how performing the site is. One important part of that has been to look out for memory leaks, because they can cause the page to go super slow or even crashing in more serious scenarios.</p>\n<!--more-->\n<h2 id=\"intro\" style=\"position:relative;\"><a href=\"#intro\" aria-label=\"intro permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Intro</h2>\n<p>Memory leaks are a common problem in software development, regardless of whether the language you’re using is memory managed or not. By that I mean languages which come with a garbage collector. Memory leaks happen when a piece of memory is allocated, but never freed by the application, and is not returned to the container app or the operating system.</p>\n<p>I remember going through the concept in uni, but I can’t remember anything apart from the fact that usually there is a tree made up of all the occupied memory locations. Every time the garbage collector is looking into the memory parses that tree, and if a node is not connected to any branch, it gets recollected and returned to the main program.</p>\n<p>Most of us web developers are likely to use one of the major frameworks or libraries to write our applications. Some maybe using a bit older languages like PHP or Ruby, but no matter what we use, there will be a high chance that we come face to face with this problem one way or another.</p>\n<h2 id=\"consequences\" style=\"position:relative;\"><a href=\"#consequences\" aria-label=\"consequences permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Consequences</h2>\n<p>So what happens when there is a memory leak in our applications 🤔?</p>\n<p>In some cases the memory consumption just keeps going up. If the user is using a decent spec machine, they might not even realise it. Not everyone is obsessed like us developers checking their task manager often to see how much memory is consumed.</p>\n<p>Regardless, it slows down the page, makes interactions not responsive, and might even cause the tab or the whole window to crash.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 454px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/cdb290cc2bda873f8fddf9f864018bbb/b3c1d/memoryleak.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 63.70370370370371%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAABU0lEQVR42qVT2U7DMBDMv/MR/AAgIcQv9AmJSjxUPKRtAki5KCKHczmNkjhexm56qkKQWhrtOrbH49mNEQTBSxzHLvCeJMnHGODsW5Zlq/l8/mCs1+uQNkPSyNFjqAhxU4Mx9u15HjmO07dtK8egaRqhCMHxbIA1XC6XBLkS0klKSdhEXdf9GSDsB8KpAZIwiiLyfV9yzv9FdJawLMtw8EEqdcqOiwjjOAp936PPr5W0bFsp1c9WG4UQZ9GL/mgOi/aEKHcIlcTSVEZxRAljVNc1VVW1AzoBke9yrnM+5BXh/DGhWkS1ZVEUtEWe5xpYR8woRcQe/Y2lbJMXOaWIqMPvHupnIW4hRH80P/0Oe/aEuH3FealublHlDn50DdCeAMbv8/Yw12uNInRd98mAL1qhatCh4S/7U2az12vbtu8ty7pFg9+ZpjkGN4vF4nEymVz9AHP3234WL8NyAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Memory leak in google chrome\"\n        title=\"\"\n        src=\"/static/cdb290cc2bda873f8fddf9f864018bbb/b3c1d/memoryleak.png\"\n        srcset=\"/static/cdb290cc2bda873f8fddf9f864018bbb/01bf6/memoryleak.png 270w,\n/static/cdb290cc2bda873f8fddf9f864018bbb/b3c1d/memoryleak.png 454w\"\n        sizes=\"(max-width: 454px) 100vw, 454px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<h2 id=\"memory-leak-in-javascript\" style=\"position:relative;\"><a href=\"#memory-leak-in-javascript\" aria-label=\"memory leak in javascript permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Memory leak in JavaScript</h2>\n<p>It’s way easy in JavaScript to allocate some memory and forget about it. Even if you’re not writing plain JavaScript, it’s still possible that a memory leak happen, without you noticing it.</p>\n<h3 id=\"but-how-does-it-happen\" style=\"position:relative;\"><a href=\"#but-how-does-it-happen\" aria-label=\"but how does it happen permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>But how does it happen?</h3>\n<p>In JavaScript there are a few possible ways a memory leak can happen.</p>\n<ul>\n<li>Unintentionally creating global variables</li>\n<li>Timers and callbacks</li>\n<li>Out of DOM references</li>\n<li>Closures</li>\n<li>Event listeners</li>\n</ul>\n<h3 id=\"global-variables\" style=\"position:relative;\"><a href=\"#global-variables\" aria-label=\"global variables permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Global variables</h3>\n<p>It’s very simple to create an unwanted global variable in JavaScript. Consider below code:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"3708681455702045000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function helloWorld() {\r\n  name = 'Yas';\r\n  console.log(\\`Hello \\${name}\\`);\r\n}`, `3708681455702045000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">helloWorld</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  name <span class=\"token operator\">=</span> <span class=\"token string\">'Yas'</span><span class=\"token punctuation\">;</span>\r\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Hello </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>name<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span></span></pre></div>\n<p>In this simple function we have created a global variable called name. We didn’t want to, but we did it anyway.</p>\n<blockquote>\n<p>💡  A reference to an undeclared variable creates a new variable inside the global object.</p>\n</blockquote>\n<p>Same thing can happen if you use <code class=\"language-text\">this</code>:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"34425122813459860000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function helloWorld(args) {\r\n  this.name = 'Yas';\r\n  console.log(\\`Hello \\${name}\\`);\r\n}`, `34425122813459860000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">helloWorld</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">args</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>name <span class=\"token operator\">=</span> <span class=\"token string\">'Yas'</span><span class=\"token punctuation\">;</span>\r\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Hello </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>name<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span></span></pre></div>\n<div class=\"custom-block danger\"><div class=\"custom-block-body\"> To prevent these sorts of leaks, use JavaScript in <code class=\"language-text\">strict</code> mode. You can do it by adding <code class=\"language-text\">use strinct;</code> at the top of your JavaScript file.</div></div>\n<p>Even thought we consider accidental global variables one of the memory leak sources, there are still many global variables defined by the framework we use, or even the ones we intend to be there. Remember these are non collectable variables and unless nulled or reassigned, garbage collectors can’t do anything about them.</p>\n<h3 id=\"timers-and-callbacks\" style=\"position:relative;\"><a href=\"#timers-and-callbacks\" aria-label=\"timers and callbacks permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Timers and callbacks</h3>\n<p>The use of <code class=\"language-text\">setInternal</code> and <code class=\"language-text\">setTimeout</code> has gotten less and less popular as we’ve moved towards more modern concepts like <code class=\"language-text\">async/await</code>. Furthermore, some libraries and frameworks provide observables to facilitate callbacks. In which case they’re responsible for making sure the callbacks are unreachable once their own instance gets destroyed.</p>\n<p>However, there are quite a number of scenarios where we need to use it in order to call a function at a later time or on a schedule.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"92963084421760930000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let data = fetchData();\r\nsetInternal(function() {\r\n  let node = document.querySelector('#list');\r\n  // loop through data and create the html\r\n  node.innerHTML = transform(data);\r\n}, 1000)`, `92963084421760930000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">let</span> data <span class=\"token operator\">=</span> <span class=\"token function\">fetchData</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token function\">setInternal</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">let</span> node <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">querySelector</span><span class=\"token punctuation\">(</span><span class=\"token string\">'#list'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token comment\">// loop through data and create the html</span>\r\n  node<span class=\"token punctuation\">.</span>innerHTML <span class=\"token operator\">=</span> <span class=\"token function\">transform</span><span class=\"token punctuation\">(</span>data<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1000</span><span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>This example shows how a timer can turn into something that cannot be collected. Even if the list node is removed from the DOM, the reference inside the handler remains active and cannot be collected. That causes their dependencies to be non collectable as well. So the data variable which might be really large, would sit in memory long after its need has ended.</p>\n<p>Now let’s see how we can improve this to avoid a memory leak:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"83475188931065500000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let node = document.querySelector('#list');\r\nlet data = fetchData();\r\n\r\nfunction handler(data) {\r\n  if(node) {\r\n    // do stuff with data and create the list\r\n    node.innerHTML = transform(data);\r\n  }\r\n};\r\n\r\nsetInterval(handler, 1000);`, `83475188931065500000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">let</span> node <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">querySelector</span><span class=\"token punctuation\">(</span><span class=\"token string\">'#list'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">let</span> data <span class=\"token operator\">=</span> <span class=\"token function\">fetchData</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">function</span> <span class=\"token function\">handler</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">data</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span>node<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token comment\">// do stuff with data and create the list</span>\r\n    node<span class=\"token punctuation\">.</span>innerHTML <span class=\"token operator\">=</span> <span class=\"token function\">transform</span><span class=\"token punctuation\">(</span>data<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token function\">setInterval</span><span class=\"token punctuation\">(</span>handler<span class=\"token punctuation\">,</span> <span class=\"token number\">1000</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"out-of-dom-references-detached-dom\" style=\"position:relative;\"><a href=\"#out-of-dom-references-detached-dom\" aria-label=\"out of dom references detached dom permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Out of DOM references (detached DOM)</h3>\n<p>When some nodes are removed from the DOM but still exists in memory through JavaScript, we have out of DOM references or detached DOM. Usually it means there is a reference to a variable which was referencing that node.</p>\n<p>DOM is a doubly linked tree, meaning any reference to any node would mean the entire tree would not be garbage collected.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 700px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/9d443868683b68fba2c69fac2697b125/8c557/doublylinkedtree.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 67.4074074074074%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAAsTAAALEwEAmpwYAAABsklEQVR42lVSh5acMAzk/38ue5e8e3k5WExZsxQbFwwGY8hQtpwA4yJ5RiMF67oueHYbnR0mY6ce4zhbrpqquWvdSSnVYVIvy+kMC/DN3ikrxcCN1f3Y4TVjZ92QVUl+zzhjDYM1nPMw/ad6+cQLnHetaaZ5XF83rsfcCNnWTWcMILXWpjO0zpmuwOtElkMLto+Q5XwXvwWzZhDtfvB+8dr2bANDsOjZ+gP0NLClcUKTFBMsvffLbps0swXkM/il2Tb33jJWxeQj/PUn+iij2ArxrpNfvBw4dhDMz+D9GBlSQrKvv2VbQgvW1RUvye/PsiiQ9p7B5iZ7DpnP4BPTTTQK6Y2GWRTTEDvILS/Ta3bNsryIwnV2BwHQ3oLbvsHPjlN9L8jloqUECTVIbeVxI8qR8RibWsjkcmF13Q+DOJDVINwyFRWNrt+ExLS6bTqj2o96gCdwMOIoTUgKo4R3tV/mAMSQABaTGyEpMCGG2PV4poO2AUHZt/M8wwel7aw6Owzg3aiers5PoHTW9lH4yU9H8d8teDXU7m2lFEmq8lzmeRvHLSGYqNvNVPX6owO39vwPbwPtUDZoATEAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Doubly linked tree\"\n        title=\"\"\n        src=\"/static/9d443868683b68fba2c69fac2697b125/8c557/doublylinkedtree.png\"\n        srcset=\"/static/9d443868683b68fba2c69fac2697b125/01bf6/doublylinkedtree.png 270w,\n/static/9d443868683b68fba2c69fac2697b125/07484/doublylinkedtree.png 540w,\n/static/9d443868683b68fba2c69fac2697b125/8c557/doublylinkedtree.png 700w\"\n        sizes=\"(max-width: 700px) 100vw, 700px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>Let’s go through an example to make this a bit more clear:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"71561438566385990000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function create() {\r\n  let ul = document.createElement('ul');\r\n  ul.id = 'list';\r\n  for (var i = 0; i < 10; i++) {\r\n    var li = document.createElement('li');\r\n    li.textContent = \\`Item # \\${i}\\`;\r\n    ul.appendChild(li);\r\n  }\r\n  return ul;\r\n}\r\n\r\nconst list = create();\r\n\r\ndocument.body.appendChild(list);\r\n\r\nfunction deleteList() {\r\n  document.body.removeChild(document.getElementById('list'));\r\n}\r\n\r\ndocument.getElementById('delete').addEventListener('click', deleteList);`, `71561438566385990000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">create</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">let</span> ul <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">createElement</span><span class=\"token punctuation\">(</span><span class=\"token string\">'ul'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  ul<span class=\"token punctuation\">.</span>id <span class=\"token operator\">=</span> <span class=\"token string\">'list'</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> i <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\">&lt;</span> <span class=\"token number\">10</span><span class=\"token punctuation\">;</span> i<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">var</span> li <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">createElement</span><span class=\"token punctuation\">(</span><span class=\"token string\">'li'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    li<span class=\"token punctuation\">.</span>textContent <span class=\"token operator\">=</span> <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Item # </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>i<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">;</span>\r\n    ul<span class=\"token punctuation\">.</span><span class=\"token function\">appendChild</span><span class=\"token punctuation\">(</span>li<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n  <span class=\"token keyword\">return</span> ul<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token keyword\">const</span> list <span class=\"token operator\">=</span> <span class=\"token function\">create</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\ndocument<span class=\"token punctuation\">.</span>body<span class=\"token punctuation\">.</span><span class=\"token function\">appendChild</span><span class=\"token punctuation\">(</span>list<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">function</span> <span class=\"token function\">deleteList</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  document<span class=\"token punctuation\">.</span>body<span class=\"token punctuation\">.</span><span class=\"token function\">removeChild</span><span class=\"token punctuation\">(</span>document<span class=\"token punctuation\">.</span><span class=\"token function\">getElementById</span><span class=\"token punctuation\">(</span><span class=\"token string\">'list'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\ndocument<span class=\"token punctuation\">.</span><span class=\"token function\">getElementById</span><span class=\"token punctuation\">(</span><span class=\"token string\">'delete'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">addEventListener</span><span class=\"token punctuation\">(</span><span class=\"token string\">'click'</span><span class=\"token punctuation\">,</span> deleteList<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Clicking the delete button, will remove the list from the DOM, but there is a reference in JavaScript, so the list never gets garbage collected. We could identify detached node using heap snapshots in your browser DevTools. I am using Chrome here, but you can use Edge (similar to Chrome), and <a href=\"https://developer.mozilla.org/en-US/docs/Tools/Memory/Basic_operations#Taking_a_heap_snapshot\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Firefox</a> too.</p>\n<p><img src=\"/7159b04df500677056bf34af6120b1a0/heapsnapshot.jpg\" alt=\"taking a heap snapshot\"></p>\n<p>And once the snapshot is taken, type detached in the filter text box and you’ll see the detached DOM nodes.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1080px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/71db567c4424ae973a3e36f47b4d2484/00d7d/detachednodes.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 50.74074074074074%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAKABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAgADBf/EABUBAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAHtHUqJR//EABgQAAMBAQAAAAAAAAAAAAAAAAABAhIg/9oACAEBAAEFAnJiRyuP/8QAFhEBAQEAAAAAAAAAAAAAAAAAABEB/9oACAEDAQE/Aauv/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAGRABAAIDAAAAAAAAAAAAAAAAAQAyICGR/9oACAEBAAY/AtgypyVMP//EABwQAQEAAAcAAAAAAAAAAAAAAAEAECExQXHB0f/aAAgBAQABPyEE4DKEehD8CdLbD//aAAwDAQACAAMAAAAQ4D//xAAYEQACAwAAAAAAAAAAAAAAAAAAARFBYf/aAAgBAwEBPxCSs0P/xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAYEAEBAQEBAAAAAAAAAAAAAAABADEhEf/aAAgBAQABPxD36w6PCSCcLAZ0mZxO3//Z'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"See detached DOM nodes from heap snapshot\"\n        title=\"\"\n        src=\"/static/71db567c4424ae973a3e36f47b4d2484/47311/detachednodes.jpg\"\n        srcset=\"/static/71db567c4424ae973a3e36f47b4d2484/6f81f/detachednodes.jpg 270w,\n/static/71db567c4424ae973a3e36f47b4d2484/09d21/detachednodes.jpg 540w,\n/static/71db567c4424ae973a3e36f47b4d2484/47311/detachednodes.jpg 1080w,\n/static/71db567c4424ae973a3e36f47b4d2484/00d7d/detachednodes.jpg 1218w\"\n        sizes=\"(max-width: 1080px) 100vw, 1080px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>The way to fix these kind of issues is to always use local variables so that the reference is destroyed once the function execution is done.</p>\n<h3 id=\"closures\" style=\"position:relative;\"><a href=\"#closures\" aria-label=\"closures permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Closures</h3>\n<p>Closures are one of the JavaScript’s features where most beginners struggle with. But once you’ve got it, it’s really easy to follow. In it’s core, a closure gives you access to an outer function’s scope from an inner function.</p>\n<p>A more technical definition is that a closure is a combination of a function bundled together with references to it’s surrounding state (the lexical environment).</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"18560973416039526000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function init() {\r\n  var hello = 'Hello'; // hello is a local variable created by init\r\n  function helloWorld() { // helloWorld() is the inner function, a closure\r\n    console.log(\\`\\${hello} world!\\`); // use variable declared in the parent function\r\n  }\r\n  helloWorld();\r\n}\r\ninit();`, `18560973416039526000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">init</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">var</span> hello <span class=\"token operator\">=</span> <span class=\"token string\">'Hello'</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// hello is a local variable created by init</span>\r\n  <span class=\"token keyword\">function</span> <span class=\"token function\">helloWorld</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span> <span class=\"token comment\">// helloWorld() is the inner function, a closure</span>\r\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>hello<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> world!</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// use variable declared in the parent function</span>\r\n  <span class=\"token punctuation\">}</span>\r\n  <span class=\"token function\">helloWorld</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n<span class=\"token function\">init</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Now that we know what’s a closure, let’s see how they can cause memory leaks. Imagine the following code:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"35313221943224218000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var newElem;\r\n \r\nfunction outer() {\r\n    var someText = new Array(1000000);\r\n    var elem = newElem;\r\n\r\n    function inner() {\r\n        if (elem) return someText;\r\n    }\r\n\r\n    return function () {};\r\n}\r\n\r\nsetInterval(function () {\r\n    newElem = outer();\r\n}, 5);`, `35313221943224218000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">var</span> newElem<span class=\"token punctuation\">;</span>\r\n \r\n<span class=\"token keyword\">function</span> <span class=\"token function\">outer</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">var</span> someText <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Array</span><span class=\"token punctuation\">(</span><span class=\"token number\">1000000</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token keyword\">var</span> elem <span class=\"token operator\">=</span> newElem<span class=\"token punctuation\">;</span>\r\n\r\n    <span class=\"token keyword\">function</span> <span class=\"token function\">inner</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n        <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>elem<span class=\"token punctuation\">)</span> <span class=\"token keyword\">return</span> someText<span class=\"token punctuation\">;</span>\r\n    <span class=\"token punctuation\">}</span>\r\n\r\n    <span class=\"token keyword\">return</span> <span class=\"token keyword\">function</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token function\">setInterval</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">function</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    newElem <span class=\"token operator\">=</span> <span class=\"token function\">outer</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token number\">5</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>In the above code, the <code class=\"language-text\">inner</code> function is never called, but it holds a reference to the <code class=\"language-text\">elem</code>. Keep in mind the scope of the inner is the same as <code class=\"language-text\">function () {}</code> which is returned by the outer function. Since <code class=\"language-text\">newElem</code> is a global variable, as long as a reference is pointing to <code class=\"language-text\">function () {}</code>, the shared context is preserved and <code class=\"language-text\">someText</code> is kept. Each call will then result in a remaining reference and as time goes by, we run out of memory eventually.</p>\n<p>So what we can we do about these type of scenarios? First we need to stop using <code class=\"language-text\">var</code>. And furthermore, if we call the inner function like <code class=\"language-text\">outer()()</code>, then there won’t be any references left.</p>\n<h3 id=\"event-listeners\" style=\"position:relative;\"><a href=\"#event-listeners\" aria-label=\"event listeners permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Event listeners</h3>\n<p>Every time you attach an event handler to a specific element, you need to keep a reference and remove it when you’re finished. So instead of:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"26586058080564777000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function deleteList() {}\r\n\r\ndocument.getElementById('delete').addEventListener('click', deleteList);`, `26586058080564777000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">deleteList</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span>\r\n\r\ndocument<span class=\"token punctuation\">.</span><span class=\"token function\">getElementById</span><span class=\"token punctuation\">(</span><span class=\"token string\">'delete'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">addEventListener</span><span class=\"token punctuation\">(</span><span class=\"token string\">'click'</span><span class=\"token punctuation\">,</span> deleteList<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>We should be doing:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"91286432363421830000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function deleteList() {}\r\n\r\ndocument.getElementById('delete').addEventListener('click', deleteList);\r\n\r\n// do stuff\r\n\r\ndocument.getElementById('delete').removeEventListener('click', deleteList);`, `91286432363421830000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">deleteList</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span>\r\n\r\ndocument<span class=\"token punctuation\">.</span><span class=\"token function\">getElementById</span><span class=\"token punctuation\">(</span><span class=\"token string\">'delete'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">addEventListener</span><span class=\"token punctuation\">(</span><span class=\"token string\">'click'</span><span class=\"token punctuation\">,</span> deleteList<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token comment\">// do stuff</span>\r\n\r\ndocument<span class=\"token punctuation\">.</span><span class=\"token function\">getElementById</span><span class=\"token punctuation\">(</span><span class=\"token string\">'delete'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">removeEventListener</span><span class=\"token punctuation\">(</span><span class=\"token string\">'click'</span><span class=\"token punctuation\">,</span> deleteList<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>We saw what will cause a potential memory leak in JavaScript and how to fix those issues, however, keep in mind that in most cases if you’re using a framework or library, these things are handled for you. If you’re using a library which you might suspect is causing memory leaks, you could easily find out using your browser DevTools memory profiler.</p>\n<p>Hope this article has raised some awareness so that you can write more performing code and make user experience much better. No one wants to have their browser chew up memory like cheesecake right 😁?</p>\n<h2 id=\"resources\" style=\"position:relative;\"><a href=\"#resources\" aria-label=\"resources permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Resources</h2>\n<ul>\n<li>More about <a href=\"https://developers.google.com/web/tools/chrome-devtools/memory-problems\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Chrome DevTools memory profiler</a>.</li>\n<li>Find out about <a href=\"https://developer.mozilla.org/en-US/docs/Tools/Memory\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Firefox DevTools memory profiling</a>.</li>\n</ul>","timeToRead":6,"frontmatter":{"title":"Stop gobbling up that memory 🛑","unformattedDate":"2020-02-28T00:00:00.000Z","date":"Feb 28, 2020","path":"/mem-leak/","author":"Yaser Adel Mehraban","tags":["webdev","showdev","memoryleak","javascript"],"thumbnail":null}}},{"node":{"id":"46dd7d3e-d489-5c9a-86c6-7991ff30045f","html":"<p>Everybody likes a fast loading web page. In fact, Google has an <a href=\"https://developers.google.com/web/fundamentals/performance/why-performance-matters/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">entire section</a> dedicated to performance and how companies are moving towards a faster web. Wouldn’t it be good if we could measure some critical metrics like page load time in our production environment and constantly monitor them to find out where can be improved?</p>\n<!--more-->\n<h2 id=\"navigation-timing-api\" style=\"position:relative;\"><a href=\"#navigation-timing-api\" aria-label=\"navigation timing api permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Navigation Timing API</h2>\n<p><a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Navigation_timing_API\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Navigation Timing API</a> is a JavaScript API which can be used to accurately measure performance on the client side. This data can then be transmitted to server side to allow real time monitoring of performance metrics.</p>\n<p>The API provides a simple way to natively get accurate and detailed timing statistics for page navigation and load events. Metrics such as page load time, amount of time needed to unload the previous page, how long domain lookups take and so on can be measured by this API.</p>\n<h2 id=\"how-to-use-it\" style=\"position:relative;\"><a href=\"#how-to-use-it\" aria-label=\"how to use it permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How to use it?</h2>\n<p>This API has two interfaces, <code class=\"language-text\">Performance</code> and <code class=\"language-text\">PerformanceNavigationTiming</code>. The <code class=\"language-text\">window.performance</code> property returns a <code class=\"language-text\">Performance</code> interface which is defined by <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Performance_API\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">High Resolution API</a>, plus two additional properties:</p>\n<ul>\n<li><strong>timing</strong>: data for navigation and page load events.</li>\n<li><strong>navigation</strong>: how the user navigated to the page.</li>\n</ul>\n<p>You can try this API in the browser’s console (pressing <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>J</kbd> for Chrome on Windows or <kbd>CMD</kbd>+<kbd>Option</kbd>+<kbd>J</kbd> for Mac users):</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"35169619306513120000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`> performance`, `35169619306513120000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-bash line-numbers\"><code class=\"language-bash\"><span class=\"token operator\">></span> performance</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>And you will see an object like this:</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/972374f6a070878ffaa1b7167327969e/41099/performance.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 35.55555555555556%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAHABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAEF/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAABy4AH/8QAFBABAAAAAAAAAAAAAAAAAAAAEP/aAAgBAQABBQJ//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAFBABAAAAAAAAAAAAAAAAAAAAEP/aAAgBAQAGPwJ//8QAFxABAQEBAAAAAAAAAAAAAAAAAQAhYf/aAAgBAQABPyE0JeEt/9oADAMBAAIAAwAAABDwD//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8QP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8QP//EABoQAQACAwEAAAAAAAAAAAAAAAEAESExQYH/2gAIAQEAAT8QdBBo7A54TJon/9k='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Performance object in browser console\"\n        title=\"\"\n        src=\"/static/972374f6a070878ffaa1b7167327969e/41099/performance.jpg\"\n        srcset=\"/static/972374f6a070878ffaa1b7167327969e/6f81f/performance.jpg 270w,\n/static/972374f6a070878ffaa1b7167327969e/41099/performance.jpg 500w\"\n        sizes=\"(max-width: 500px) 100vw, 500px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<h2 id=\"better-alternative-to-code-classlanguage-textdatecode-object\" style=\"position:relative;\"><a href=\"#better-alternative-to-code-classlanguage-textdatecode-object\" aria-label=\"better alternative to code classlanguage textdatecode object permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Better alternative to <code class=\"language-text\">Date</code> object</h2>\n<p>Historically, we’ve used <code class=\"language-text\">Date</code> object for timing metrics like:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"23053409675622195000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let start = Date.now();`, `23053409675622195000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">let</span> start <span class=\"token operator\">=</span> Date<span class=\"token punctuation\">.</span><span class=\"token function\">now</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>And at the end of the page:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"16316274217696991000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`console.log(\\`It took: \\${Date.now() - start} ms for the page to load!\\`);`, `16316274217696991000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\">console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">It took: </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>Date<span class=\"token punctuation\">.</span><span class=\"token function\">now</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">-</span> start<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> ms for the page to load!</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>However, this is very inefficient because of a couple of reasons. First, the timing code is on the page which takes time itself and affects performance. Besides, you should know that JavaScript time is <a href=\"https://johnresig.com/blog/accuracy-of-javascript-time/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">not accurate</a>. Furthermore, it wouldn’t be close to what users experienced it, and worst of all, you can’t use <code class=\"language-text\">Date</code> object, to measure network latency before the page started loading.</p>\n<p>If you think of events which happen before the page even begins to load such as DNS resolution, redirects and server response time, technically you can’t measure these by inline code since your code hasn’t arrived yet.</p>\n<h2 id=\"navigation-timing-api-ftw\" style=\"position:relative;\"><a href=\"#navigation-timing-api-ftw\" aria-label=\"navigation timing api ftw permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Navigation Timing API FTW</h2>\n<p>Each of the properties of of <code class=\"language-text\">performance.timing</code> shows the time of navigation events such as when the page was requested 👉🏼 <code class=\"language-text\">requestStart</code> or page load events such as when the DOM began loading 👉🏼 <code class=\"language-text\">domLoading</code> in milliseconds since the midnight of Jan 1, 1970 UTC.</p>\n<p>💡 A <code class=\"language-text\">zero</code> value means that the event didn’t occur. Events such as <code class=\"language-text\">redirectStart</code> or <code class=\"language-text\">secureConnectionStart</code> might not happen at all.</p>\n<p>For more information on these events, you can have a look at the <a href=\"https://www.w3.org/TR/navigation-timing/#sec-navigation-timing-interface\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">W3C Recommendation</a>.</p>\n<p>You can see the order of these events from the picture below from the above document:</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1080px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/7e63b0174961f257d375e7f12ca790fe/0c156/timing-overview.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 59.62962962962963%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAABcSAAAXEgFnn9JSAAACqklEQVR42pWTf0hTURTHr23TKbOZPyr7IwoSZBAGwzAR/DMLM/95f5iIoDVlYqVlURH3j8AUEpNSm0utNpUeUUGohbops/ee+va2ptt7Ph22dKgxlYGQj4jXvVpRUKEXDvdxz7kfvufc7wPgt2UwsSoA9DGcd/aUk3OuCILwUZblYziH9l1gpyszrz42Pb82gYBkvJPzNPI8/02SpKHwxkYKAkajiMCxLVh2NlSmF9Um4MitNiXqCKhhOF9BcGFhfnFxUQ6FQr0Ipt622rTixrjMEqQQAfG33lCnBTpCc6eNPOzj+f6VUEheWlpyra6uXkJA5Q/w39Xq9SZVKmoVqVTrC+CmOtx6ftGDBAByY+w0f4geY9sDgcB6OByeX1tbK0Ww5H8qPVHespeoI7XHC+HutLMwTpdt1CDo/tSTN5PRHolKFBCN5PXo6AG3e+q23z/Xtv5FKl1eXtbg+yRJKux2uxIHaHn73tM65Bywjnkd5mE3ax7huDYbN4HO6CcMz3RNeH3d3Iyvh/UJndQk1e2eG+lxic5nzKTnpTfg6XYKsxba019FUtGb6u69shms7Ky51/9ZemRzV5sGmbJOii/vGJ08j4BlCHbFMi5efu7yV1nHhYoOh/eqZWz6loXha6zj0zeabR+uPaV9FwYo6uinmZkjm9D7b7iU1gFXYTHsVG8N4b/WwDkFAESk3mBSGY0PNYKXrxVFETvBAXDfEG4N1wBNMcR1k/bMRfO+nMr2JGwfIyQ1TX1iFCnLCvwIuL4AdiUCUKxmOFHnmfLakV/X0TyHUf4g+Pn8+MIfvoRQmUE0RCMV2ozShvisc3f3YFvh88ySx7Ev+oZO87wgBoPBja+S1IwYqh38Q3JETmVTVF5NfSxIMmoc7FQWTVPvkCoWgSp+VclyxHece32TjXQUigAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Order of navigation events\"\n        title=\"\"\n        src=\"/static/7e63b0174961f257d375e7f12ca790fe/302a4/timing-overview.png\"\n        srcset=\"/static/7e63b0174961f257d375e7f12ca790fe/01bf6/timing-overview.png 270w,\n/static/7e63b0174961f257d375e7f12ca790fe/07484/timing-overview.png 540w,\n/static/7e63b0174961f257d375e7f12ca790fe/302a4/timing-overview.png 1080w,\n/static/7e63b0174961f257d375e7f12ca790fe/0c156/timing-overview.png 1473w\"\n        sizes=\"(max-width: 1080px) 100vw, 1080px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<h2 id=\"some-use-cases\" style=\"position:relative;\"><a href=\"#some-use-cases\" aria-label=\"some use cases permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Some use cases</h2>\n<p>Now let’s have a look at how we can calculate some metrics using these useful properties.</p>\n<h3 id=\"total-page-load-time\" style=\"position:relative;\"><a href=\"#total-page-load-time\" aria-label=\"total page load time permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Total page load time</h3>\n<p>In order to calculate the total page load time, you can use the <code class=\"language-text\">loadEventEnd</code> and <code class=\"language-text\">navigationStart</code> properties:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"82978579712947550000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const perfObj = window.performance.timing;\r\n\r\nlet pageLoadTime = perfObj.loadEventEnd - perfObj.navigationStart;`, `82978579712947550000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> perfObj <span class=\"token operator\">=</span> window<span class=\"token punctuation\">.</span>performance<span class=\"token punctuation\">.</span>timing<span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">let</span> pageLoadTime <span class=\"token operator\">=</span> perfObj<span class=\"token punctuation\">.</span>loadEventEnd <span class=\"token operator\">-</span> perfObj<span class=\"token punctuation\">.</span>navigationStart<span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"page-render-time\" style=\"position:relative;\"><a href=\"#page-render-time\" aria-label=\"page render time permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Page render time</h3>\n<p>To calculate the total time taken to render the page, simply use <code class=\"language-text\">domComplete</code> and <code class=\"language-text\">domLoading</code> properties:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"57686095592459540000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const perfObj = window.performance.timing;\r\n\r\nlet renderTime = perfObj.domComplete - perfObj.domLoading;`, `57686095592459540000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> perfObj <span class=\"token operator\">=</span> window<span class=\"token punctuation\">.</span>performance<span class=\"token punctuation\">.</span>timing<span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">let</span> renderTime <span class=\"token operator\">=</span> perfObj<span class=\"token punctuation\">.</span>domComplete <span class=\"token operator\">-</span> perfObj<span class=\"token punctuation\">.</span>domLoading<span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"request-response-time\" style=\"position:relative;\"><a href=\"#request-response-time\" aria-label=\"request response time permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Request response time</h3>\n<p>To calculate the time between beginning of the request to the end of response retrieval, you can use:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"66729044032825360000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const perfObj = window.performance.timing;\r\n\r\nlet renderTime = perfObj.responseEnd - perfObj.requestStart;`, `66729044032825360000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> perfObj <span class=\"token operator\">=</span> window<span class=\"token punctuation\">.</span>performance<span class=\"token punctuation\">.</span>timing<span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">let</span> renderTime <span class=\"token operator\">=</span> perfObj<span class=\"token punctuation\">.</span>responseEnd <span class=\"token operator\">-</span> perfObj<span class=\"token punctuation\">.</span>requestStart<span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"network-latency\" style=\"position:relative;\"><a href=\"#network-latency\" aria-label=\"network latency permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Network latency</h3>\n<p>If you want to measure network latency:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"1101615824026591600\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const perfObj = window.performance.timing;\r\n\r\nlet renderTime = perfObj.responseEnd - perfObj.fetchStart;`, `1101615824026591600`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> perfObj <span class=\"token operator\">=</span> window<span class=\"token punctuation\">.</span>performance<span class=\"token punctuation\">.</span>timing<span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">let</span> renderTime <span class=\"token operator\">=</span> perfObj<span class=\"token punctuation\">.</span>responseEnd <span class=\"token operator\">-</span> perfObj<span class=\"token punctuation\">.</span>fetchStart<span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"navigation-and-page-load-together\" style=\"position:relative;\"><a href=\"#navigation-and-page-load-together\" aria-label=\"navigation and page load together permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Navigation and page load together</h3>\n<p>To have the sum of navigation and page load time:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"96724908036328160000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const perfObj = window.performance.timing;\r\n\r\nlet renderTime = perfObj.loadEventEnd - perfObj.navigationStart;`, `96724908036328160000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> perfObj <span class=\"token operator\">=</span> window<span class=\"token punctuation\">.</span>performance<span class=\"token punctuation\">.</span>timing<span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">let</span> renderTime <span class=\"token operator\">=</span> perfObj<span class=\"token punctuation\">.</span>loadEventEnd <span class=\"token operator\">-</span> perfObj<span class=\"token punctuation\">.</span>navigationStart<span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"pinpoint-redirect-problems\" style=\"position:relative;\"><a href=\"#pinpoint-redirect-problems\" aria-label=\"pinpoint redirect problems permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Pinpoint redirect problems</h3>\n<p>To find out about any issue in redirects:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"99538124148490860000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const perfObj = window.performance.timing;\r\n\r\nlet renderTime = perfObj.redirectEnd - perfObj.redirectStart;`, `99538124148490860000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> perfObj <span class=\"token operator\">=</span> window<span class=\"token punctuation\">.</span>performance<span class=\"token punctuation\">.</span>timing<span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">let</span> renderTime <span class=\"token operator\">=</span> perfObj<span class=\"token punctuation\">.</span>redirectEnd <span class=\"token operator\">-</span> perfObj<span class=\"token punctuation\">.</span>redirectStart<span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"code-classlanguage-textnavigationcode-property\" style=\"position:relative;\"><a href=\"#code-classlanguage-textnavigationcode-property\" aria-label=\"code classlanguage textnavigationcode property permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">navigation</code> property</h2>\n<p>There are many ways to end up on a page. If you want to know how your user ended up on your page, or how many redirects they’ve had before landing there, this property is your friend. <code class=\"language-text\">performance.navigation</code> has two properties:</p>\n<ul>\n<li><strong>redirectCount</strong>: the number of times the document request was redirected.</li>\n<li><strong>type</strong>: type of the navigation which lead to this page.</li>\n</ul>\n<p>The <code class=\"language-text\">type</code> property is an <code class=\"language-text\">enum</code> which can have three values:</p>\n<ul>\n<li><strong>0</strong>: action by the user such as clicking a link or entering a URL in the browser address bar.</li>\n<li><strong>1</strong>: page reload.</li>\n<li><strong>2</strong>: navigation through going back and forth from history of the browser.</li>\n</ul>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>We saw how we can use the Navigation Timing API to get performance metrics on client side which can be sent to server to monitor performance of users in real time regardless of where they are and how they got there. This API is really powerful and has helped me a lot to help customers find out where they need to focus their energy to improve performance on which pages 😍.</p>\n<p>Hope this has been helpful and till next time, <em>au revoir</em> 👋.</p>","timeToRead":3,"frontmatter":{"title":"Send performance metrics from client side with Navigation Timing API ⚡","unformattedDate":"2019-09-29T00:00:00.000Z","date":"Sep 29, 2019","path":"/navigation-timing/","author":"Yaser Adel Mehraban","tags":["showdev","webdev","javascript","navigationtiming"],"thumbnail":null}}},{"node":{"id":"9b5d9dcd-3483-5058-922e-b779ff165f12","html":"<p>Are you want of those people with 200 tabs open? Are you tired of carrying your laptop charger with you all the time? Well, I am here to let you know we can help users save battery, data, and time, if we know how to use the <strong>Page Visibility API</strong>.</p>\n<!--more-->\n<h2 id=\"the-why\" style=\"position:relative;\"><a href=\"#the-why\" aria-label=\"the why permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The why</h2>\n<p>Imagine your user has a few tabs open, one running a heavy animation, the other getting a lot of data from the server, and another running a video from YouTube. These all require resources from user’s computer and that’s why sometimes having a few tabs open you suddenly realise you’re running out of battery, or your data allowance is finishing.</p>\n<p>💡 What if you could pause the current operation on a tab/window if the user isn’t looking at it?</p>\n<p>With <code class=\"language-text\">Page Visibility API</code>, you can do that.</p>\n<h2 id=\"page-visibility-api\" style=\"position:relative;\"><a href=\"#page-visibility-api\" aria-label=\"page visibility api permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Page Visibility API</h2>\n<p>This API allows a developer to become aware if the user has lost focus on the page, or has returned to it again. When the user minimises, or switches to another tab/window, the API sends a <code class=\"language-text\">visibilitychange</code> event to let the listeners know the state of the page.</p>\n<p><strong>Page Visibility API</strong> is a very useful tool in your toolbox which gives you the power to not perform unnecessary operations when the page is not visible to user.</p>\n<p>This API adds the following properties to the <code class=\"language-text\">Document</code> interface:</p>\n<ul>\n<li><code class=\"language-text\">hidden</code>: which is a read-only attribute and returns true if the page is in a state to be considered hidden to user.</li>\n<li><code class=\"language-text\">visibilityState</code>: which is a <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/DOMString\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">DOMString</code></a> indicating the document’s current visibility state. It can have four values, <code class=\"language-text\">visible</code>, <code class=\"language-text\">hidden</code>, <code class=\"language-text\">prerender</code>, and <code class=\"language-text\">unloaded</code>.</li>\n</ul>\n<p>You can also listen to the <code class=\"language-text\">visibilitychange</code> event which will trigger your callback function whenever the visibility changes:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"40473059623515865000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function handleVisibilityChange () {\r\n    if (document.hidden) {\r\n        // stop that task 🛑\r\n    } else {\r\n        // you can start it again go ▶️\r\n    }\r\n}\r\n\r\ndocument.addEventListener('visibilitychange', handleVisibilityChange, false);`, `40473059623515865000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">handleVisibilityChange</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>document<span class=\"token punctuation\">.</span>hidden<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n        <span class=\"token comment\">// stop that task 🛑</span>\r\n    <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\r\n        <span class=\"token comment\">// you can start it again go ▶️</span>\r\n    <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\ndocument<span class=\"token punctuation\">.</span><span class=\"token function\">addEventListener</span><span class=\"token punctuation\">(</span><span class=\"token string\">'visibilitychange'</span><span class=\"token punctuation\">,</span> handleVisibilityChange<span class=\"token punctuation\">,</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"code-classlanguage-textvisibilitystatecode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textvisibilitystatecode\" aria-label=\"code classlanguage textvisibilitystatecode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">visibilityState</code></h2>\n<p>As mentioned before, this property can have four different values each of which representing a different tab/window state:</p>\n<ul>\n<li><strong>visible</strong>: This means the tab/window is visible or partially so. In other words, it means the page is the foreground tab of a non-minimised window.</li>\n<li><strong>hidden</strong>: Page is not visible due to being minimised or the device’s screen is off.</li>\n<li><strong>prerender</strong>: The page is now being prerendered and is not visible to user.</li>\n<li><strong>unloaded</strong>: This means the user is about to close the current page.</li>\n</ul>\n<div class=\"custom-block warning\"><div class=\"custom-block-body\"> ⚠️ You need to know that not all browsers support the last two states.</div></div>\n<h2 id=\"demo\" style=\"position:relative;\"><a href=\"#demo\" aria-label=\"demo permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Demo</h2>\n<p>The simplest scenario is playing video on the page, so we will be using that as an example. Let’s use a simple video element on the page which will be paused when the user focuses on another tab.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"69376605891990130000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<body>\r\n    <h1>Demo: Page Visibility API</h1>\r\n    \r\n    <main>\r\n        <video id=&quot;videoElement&quot; \r\n               poster=&quot;http://media.w3.org/2010/05/sintel/poster.png&quot; width=&quot;400&quot;\r\n               controls=&quot;&quot; >\r\n           <source id='mp4' src=&quot;http://media.w3.org/2010/05/sintel/trailer.mp4&quot; type='video/mp4'/>\r\n        <source id='webm' src=&quot;http://media.w3.org/2010/05/sintel/trailer.webm&quot; type='video/webm'/>\r\n        <source id='ogv' src=&quot;http://media.w3.org/2010/05/sintel/trailer.ogv&quot; type='video/ogg'/>\r\n            <p>Sorry, there's a problem playing this video. Please try using a different browser</p>\r\n        </video>\r\n    </main>\r\n    \r\n    \r\n    <script>    \r\n\r\n    (function() {\r\n        'use strict';\r\n        \r\n        // Set the name of the &quot;hidden&quot; property and the change event for visibility\r\n        var hidden, visibilityChange; \r\n        if (typeof document.hidden !== &quot;undefined&quot;) {\r\n          hidden = &quot;hidden&quot;;\r\n          visibilityChange = &quot;visibilitychange&quot;;\r\n        } else if (typeof document.mozHidden !== &quot;undefined&quot;) { // Firefox up to v17\r\n          hidden = &quot;mozHidden&quot;;\r\n          visibilityChange = &quot;mozvisibilitychange&quot;;\r\n        } else if (typeof document.webkitHidden !== &quot;undefined&quot;) { // Chrome up to v32, Android up to v4.4, Blackberry up to v10\r\n          hidden = &quot;webkitHidden&quot;;\r\n          visibilityChange = &quot;webkitvisibilitychange&quot;;\r\n        }\r\n        \r\n        var videoElement = document.getElementById(&quot;videoElement&quot;);\r\n\r\n        // If the page is hidden, pause the video;\r\n        // if the page is shown, play the video\r\n        function handleVisibilityChange() {\r\n          if (document[hidden]) {\r\n            videoElement.pause();\r\n          } else {\r\n            videoElement.play();\r\n          }\r\n        }\r\n\r\n        // Warn if the browser doesn't support addEventListener or the Page Visibility API\r\n        if (typeof document.addEventListener === &quot;undefined&quot; || typeof document[hidden] === &quot;undefined&quot;) {\r\n          alert(&quot;This demo requires a modern browser that supports the Page Visibility API.&quot;);\r\n        } else {\r\n          // Handle page visibility change   \r\n          document.addEventListener(visibilityChange, handleVisibilityChange, false);\r\n            \r\n          // When the video pauses and plays, change the title.\r\n          videoElement.addEventListener(&quot;pause&quot;, function(){\r\n            document.title = 'Paused';\r\n          }, false);\r\n            \r\n          videoElement.addEventListener(&quot;play&quot;, function(){\r\n            document.title = 'Playing'\r\n          }, false);\r\n        }\r\n\r\n    })();\r\n    </script>\r\n\r\n</body>`, `69376605891990130000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>body</span><span class=\"token punctuation\">></span></span>\r\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>h1</span><span class=\"token punctuation\">></span></span>Demo: Page Visibility API<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>h1</span><span class=\"token punctuation\">></span></span>\r\n    \r\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>main</span><span class=\"token punctuation\">></span></span>\r\n        <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>video</span> <span class=\"token attr-name\">id</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>videoElement<span class=\"token punctuation\">\"</span></span> \r\n               <span class=\"token attr-name\">poster</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>http://media.w3.org/2010/05/sintel/poster.png<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">width</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>400<span class=\"token punctuation\">\"</span></span>\r\n               <span class=\"token attr-name\">controls</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span><span class=\"token punctuation\">\"</span></span> <span class=\"token punctuation\">></span></span>\r\n           <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>source</span> <span class=\"token attr-name\">id</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">'</span>mp4<span class=\"token punctuation\">'</span></span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>http://media.w3.org/2010/05/sintel/trailer.mp4<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">type</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">'</span>video/mp4<span class=\"token punctuation\">'</span></span><span class=\"token punctuation\">/></span></span>\r\n        <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>source</span> <span class=\"token attr-name\">id</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">'</span>webm<span class=\"token punctuation\">'</span></span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>http://media.w3.org/2010/05/sintel/trailer.webm<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">type</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">'</span>video/webm<span class=\"token punctuation\">'</span></span><span class=\"token punctuation\">/></span></span>\r\n        <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>source</span> <span class=\"token attr-name\">id</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">'</span>ogv<span class=\"token punctuation\">'</span></span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>http://media.w3.org/2010/05/sintel/trailer.ogv<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">type</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">'</span>video/ogg<span class=\"token punctuation\">'</span></span><span class=\"token punctuation\">/></span></span>\r\n            <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>p</span><span class=\"token punctuation\">></span></span>Sorry, there's a problem playing this video. Please try using a different browser<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>p</span><span class=\"token punctuation\">></span></span>\r\n        <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>video</span><span class=\"token punctuation\">></span></span>\r\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>main</span><span class=\"token punctuation\">></span></span>\r\n    \r\n    \r\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>script</span><span class=\"token punctuation\">></span></span><span class=\"token script\"><span class=\"token language-javascript\">    \r\n\r\n    <span class=\"token punctuation\">(</span><span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n        <span class=\"token string\">'use strict'</span><span class=\"token punctuation\">;</span>\r\n        \r\n        <span class=\"token comment\">// Set the name of the \"hidden\" property and the change event for visibility</span>\r\n        <span class=\"token keyword\">var</span> hidden<span class=\"token punctuation\">,</span> visibilityChange<span class=\"token punctuation\">;</span> \r\n        <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">typeof</span> document<span class=\"token punctuation\">.</span>hidden <span class=\"token operator\">!==</span> <span class=\"token string\">\"undefined\"</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n          hidden <span class=\"token operator\">=</span> <span class=\"token string\">\"hidden\"</span><span class=\"token punctuation\">;</span>\r\n          visibilityChange <span class=\"token operator\">=</span> <span class=\"token string\">\"visibilitychange\"</span><span class=\"token punctuation\">;</span>\r\n        <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">typeof</span> document<span class=\"token punctuation\">.</span>mozHidden <span class=\"token operator\">!==</span> <span class=\"token string\">\"undefined\"</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span> <span class=\"token comment\">// Firefox up to v17</span>\r\n          hidden <span class=\"token operator\">=</span> <span class=\"token string\">\"mozHidden\"</span><span class=\"token punctuation\">;</span>\r\n          visibilityChange <span class=\"token operator\">=</span> <span class=\"token string\">\"mozvisibilitychange\"</span><span class=\"token punctuation\">;</span>\r\n        <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">typeof</span> document<span class=\"token punctuation\">.</span>webkitHidden <span class=\"token operator\">!==</span> <span class=\"token string\">\"undefined\"</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span> <span class=\"token comment\">// Chrome up to v32, Android up to v4.4, Blackberry up to v10</span>\r\n          hidden <span class=\"token operator\">=</span> <span class=\"token string\">\"webkitHidden\"</span><span class=\"token punctuation\">;</span>\r\n          visibilityChange <span class=\"token operator\">=</span> <span class=\"token string\">\"webkitvisibilitychange\"</span><span class=\"token punctuation\">;</span>\r\n        <span class=\"token punctuation\">}</span>\r\n        \r\n        <span class=\"token keyword\">var</span> videoElement <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">getElementById</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"videoElement\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n        <span class=\"token comment\">// If the page is hidden, pause the video;</span>\r\n        <span class=\"token comment\">// if the page is shown, play the video</span>\r\n        <span class=\"token keyword\">function</span> <span class=\"token function\">handleVisibilityChange</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n          <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>document<span class=\"token punctuation\">[</span>hidden<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n            videoElement<span class=\"token punctuation\">.</span><span class=\"token function\">pause</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n          <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\r\n            videoElement<span class=\"token punctuation\">.</span><span class=\"token function\">play</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n          <span class=\"token punctuation\">}</span>\r\n        <span class=\"token punctuation\">}</span>\r\n\r\n        <span class=\"token comment\">// Warn if the browser doesn't support addEventListener or the Page Visibility API</span>\r\n        <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">typeof</span> document<span class=\"token punctuation\">.</span>addEventListener <span class=\"token operator\">===</span> <span class=\"token string\">\"undefined\"</span> <span class=\"token operator\">||</span> <span class=\"token keyword\">typeof</span> document<span class=\"token punctuation\">[</span>hidden<span class=\"token punctuation\">]</span> <span class=\"token operator\">===</span> <span class=\"token string\">\"undefined\"</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n          <span class=\"token function\">alert</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"This demo requires a modern browser that supports the Page Visibility API.\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n        <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\r\n          <span class=\"token comment\">// Handle page visibility change   </span>\r\n          document<span class=\"token punctuation\">.</span><span class=\"token function\">addEventListener</span><span class=\"token punctuation\">(</span>visibilityChange<span class=\"token punctuation\">,</span> handleVisibilityChange<span class=\"token punctuation\">,</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n            \r\n          <span class=\"token comment\">// When the video pauses and plays, change the title.</span>\r\n          videoElement<span class=\"token punctuation\">.</span><span class=\"token function\">addEventListener</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"pause\"</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\r\n            document<span class=\"token punctuation\">.</span>title <span class=\"token operator\">=</span> <span class=\"token string\">'Paused'</span><span class=\"token punctuation\">;</span>\r\n          <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n            \r\n          videoElement<span class=\"token punctuation\">.</span><span class=\"token function\">addEventListener</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"play\"</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\r\n            document<span class=\"token punctuation\">.</span>title <span class=\"token operator\">=</span> <span class=\"token string\">'Playing'</span>\r\n          <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n        <span class=\"token punctuation\">}</span>\r\n\r\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    </span></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>script</span><span class=\"token punctuation\">></span></span>\r\n\r\n<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>body</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>All we’re doing is to get a reference to the video element and hook into the <code class=\"language-text\">visibilitychange</code> event. When the page is hidden, we simply pause the video. Once the focus is back in, we play it.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 403px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/3675698702ae0f0c46a1642df44a4db0/c80d3/simples.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 101.11111111111111%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGQABAQADAQAAAAAAAAAAAAAAAAQCAwUG/8QAFwEBAQEBAAAAAAAAAAAAAAAAAgABA//aAAwDAQACEAMQAAAB3yXckr0rJp5MxdLBH//EABoQAAMBAQEBAAAAAAAAAAAAAAECAwQSEwD/2gAIAQEAAQUC16jMppr3F/SeuXZkACnIXTMNbyH0UAX/xAAVEQEBAAAAAAAAAAAAAAAAAAAQAf/aAAgBAwEBPwEh/8QAFBEBAAAAAAAAAAAAAAAAAAAAIP/aAAgBAgEBPwEf/8QAHRAAAgEEAwAAAAAAAAAAAAAAAAERAhIhQTFRgf/aAAgBAQAGPwKyhZ7FsuiCU0nsiWQmhvJyz0//xAAaEAEAAwEBAQAAAAAAAAAAAAABABExIWHx/9oACAEBAAE/IVvA1Ssil24oK3Eh0eHF9Zy6tiAkHsQTgyfViEFj/9oADAMBAAIAAwAAABDQKLz/xAAXEQADAQAAAAAAAAAAAAAAAAAAATEQ/9oACAEDAQE/EFkDp//EABcRAQEBAQAAAAAAAAAAAAAAAAEAEBH/2gAIAQIBAT8QYuuf/8QAHRABAAICAwEBAAAAAAAAAAAAAQARUWEhMUGh0f/aAAgBAQABPxBOQoEyXR+xSPMgaiK1VPiNM7MSJMHyKr0C+h3cB2tWCBSKHKvJUIcneXzUdwLPNYNT/9k='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Simples\"\n        title=\"\"\n        src=\"/static/3675698702ae0f0c46a1642df44a4db0/c80d3/simples.jpg\"\n        srcset=\"/static/3675698702ae0f0c46a1642df44a4db0/6f81f/simples.jpg 270w,\n/static/3675698702ae0f0c46a1642df44a4db0/c80d3/simples.jpg 403w\"\n        sizes=\"(max-width: 403px) 100vw, 403px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>I’ve create a <a href=\"https://aluminum-argon.glitch.me\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">live version on Glitch</a> you can have a look. Play the video and click on another tab. The video will get paused and once you’re back, it plays again.</p>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>A good web application does not necessarily require hot features and impressive UX. Sometimes a performing web application would appeal to users like no other. Hope this little article helps you help your users in many ways.</p>\n<p>Happy exploring 😊.</p>","timeToRead":3,"frontmatter":{"title":"Page Visibility API, let's help users save their battery life 😀","unformattedDate":"2019-09-22T00:00:00.000Z","date":"Sep 22, 2019","path":"/page-visibility-api/","author":"Yaser Adel Mehraban","tags":["showdev","webdev","javascript","pagevisibilityapi"],"thumbnail":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAMABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDBf/EABYBAQEBAAAAAAAAAAAAAAAAAAEDBf/aAAwDAQACEAMQAAAB0HFz60Ig/wD/xAAZEAEAAgMAAAAAAAAAAAAAAAABAAIQESH/2gAIAQEAAQUC2wctasrw/8QAFREBAQAAAAAAAAAAAAAAAAAAARD/2gAIAQMBAT8BZ//EABkRAAIDAQAAAAAAAAAAAAAAAAABAhEhA//aAAgBAgEBPwHm1ekmrw//xAAZEAACAwEAAAAAAAAAAAAAAAAAARARITL/2gAIAQEABj8C5Hk6rMVH/8QAGhABAQEBAAMAAAAAAAAAAAAAAREAUTGx8f/aAAgBAQABPyFJntkYfEuDJfuRpx3RABw3/9oADAMBAAIAAwAAABDUD//EABcRAQADAAAAAAAAAAAAAAAAAAEAESH/2gAIAQMBAT8QLWQGtn//xAAXEQADAQAAAAAAAAAAAAAAAAAAESEB/9oACAECAQE/EMM2MEh//8QAHBABAQEBAAIDAAAAAAAAAAAAAREhADFBgbHh/9oACAEBAAE/EAlFWGJ+dkAn76blMfHK2gRnCm34RxpWM1nF1Yuk++//2Q=="},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/c21d381827e05a05f585f9f42724f243/3e920/bn.jpg","srcSet":"/static/c21d381827e05a05f585f9f42724f243/eb07a/bn.jpg 750w,\n/static/c21d381827e05a05f585f9f42724f243/3e920/bn.jpg 900w","sizes":"(min-width: 900px) 900px, 100vw"},"sources":[{"srcSet":"/static/c21d381827e05a05f585f9f42724f243/bb217/bn.webp 750w,\n/static/c21d381827e05a05f585f9f42724f243/44e18/bn.webp 900w","type":"image/webp","sizes":"(min-width: 900px) 900px, 100vw"}]},"width":900,"height":560}}}}}},{"node":{"id":"af97aff2-d64d-51d1-83a3-4b758a8b9681","html":"<p><a href=\"https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Resize Observer</a> API is another great JavaScript API which lets you get notified when the size of an element changes. Think <code class=\"language-text\">window.onresize</code> but on element level.</p>\n<!--more-->\n<h2 id=\"the-what\" style=\"position:relative;\"><a href=\"#the-what\" aria-label=\"the what permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The what</h2>\n<p>The most common case when the size of an element could change is when the viewport is resized. Or in terms of mobile device the orientation changes.</p>\n<p>Prior to <code class=\"language-text\">ResizeObserver</code> you would have to hook into <code class=\"language-text\">window.onresize</code> or use <code class=\"language-text\">window.resize</code> to be able to figure out whether an element’s size has changed or not. This was very inefficient since it would trigger so many events.</p>\n<p>Another scenario where this API can come handy is when you add/remove an element dynamically, in which case the size of the parent changes and not the viewport.</p>\n<blockquote>\n<p>The <code class=\"language-text\">ResizeObserver</code> interface reports changes to the dimensions of an <code class=\"language-text\">Element</code>’s content or border box, or the bounding box of an <code class=\"language-text\">SVGElement</code>.</p>\n</blockquote>\n<p>The content box is the box in which content can be placed, meaning the border box minus the padding and border width. The border box encompasses the content, padding, and border.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1080px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 38.148148148148145%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAAAsTAAALEwEAmpwYAAABkElEQVR42mNw840OiEz1CY1y8w3wDAzyCgjyhKDAIFe/YCDDKxAmAkWBzl4BVg5+QMRg5+K38Ujtnkud+86WHTibe+Bs3sFz+QfP5R04l7/9VPX+swVANhwdOJN34krJ0cslQF3mtj5Azf6Hz+R3dXX0d65urZ1dUzSjqhCEgIz+1qV1JTOrCqZDRKoLZ1QUzKyqrD52rsDJw9/MxofB3tVv87HyjpYZ8xa/mr/vw4xNb6eufzttw9sJa9/0rnvdt+bN9A1vp24AC25+O2XWm57WhdtPVTh4BJjbgG3ecbywpryvsfZac9+Nlt4brWCyqfdGz8w7zb0IkdYJN5obblSXTt59qsgRotnW2e/0+eJ5i0onT6meNrV6KhI5f2HX9Gm1U6dUwUWAambPL710rczR3R/kZ2CgTZybvnpH7pKNGUs3ZizZmA4nZ6wqWLwhE1lk8Yb0lVuzZyxJt3Hys7DzZbCw87FxiXDySrC0DzUw9TI08zEy94GQxubecDaENDL3tXWJtnYMs7D3tbT3BQC4keqkY52fFQAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Box model\"\n        title=\"\"\n        src=\"/static/50b7eadf4d9c73b344120ffabb2e09fc/302a4/box-model.png\"\n        srcset=\"/static/50b7eadf4d9c73b344120ffabb2e09fc/01bf6/box-model.png 270w,\n/static/50b7eadf4d9c73b344120ffabb2e09fc/07484/box-model.png 540w,\n/static/50b7eadf4d9c73b344120ffabb2e09fc/302a4/box-model.png 1080w,\n/static/50b7eadf4d9c73b344120ffabb2e09fc/cd138/box-model.png 1220w\"\n        sizes=\"(max-width: 1080px) 100vw, 1080px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n    </span></p>\n<h2 id=\"enough-talking-shoe-me-the-code-\" style=\"position:relative;\"><a href=\"#enough-talking-shoe-me-the-code-\" aria-label=\"enough talking shoe me the code  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Enough talking, shoe me the code 😉</h2>\n<p>In its simplest form, you can use <code class=\"language-text\">ResizeObserver</code> by just instantiating a new instance of the API and pass a call-back to it.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"86215617989471440000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const myObserver = new ResizeObserver(entries => {\r\n  // iterate over the entries, do something.\r\n});`, `86215617989471440000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> myObserver <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">ResizeObserver</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">entries</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token comment\">// iterate over the entries, do something.</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>By itself, this wouldn’t help much, however if you call the <code class=\"language-text\">observe</code> method on the instance and give it an element, it will start capturing it’s size changes and calls your call-back function.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"65944056393343200000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const myEl = document.querySelector('.my-element');\r\n\r\nmyObserver.observe(myEl);`, `65944056393343200000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> myEl <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">querySelector</span><span class=\"token punctuation\">(</span><span class=\"token string\">'.my-element'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\nmyObserver<span class=\"token punctuation\">.</span><span class=\"token function\">observe</span><span class=\"token punctuation\">(</span>myEl<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>Now whenever my element’s size changes, it will trigger the call-back and voila.</p>\n<h2 id=\"call-back-input-object\" style=\"position:relative;\"><a href=\"#call-back-input-object\" aria-label=\"call back input object permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>call-back input object</h2>\n<p>When you pass a call-back to the constructor of the API, what you get is an array of objects which look like below:</p>\n<p><img src=\"/3ac48867d1829098a61c99d61abc3c26/resizeobserverentry.jpg\" alt=\"ResizeObserver Entry\"></p>\n<p>As you can see, the object you get has two properties, <strong><code class=\"language-text\">contentRect</code></strong> and <code class=\"language-text\">target</code>. Target is obvious, it’s the DOM element itself. The <code class=\"language-text\">width</code> and <code class=\"language-text\">height</code> values on the <strong><code class=\"language-text\">contentRect</code></strong> don’t include the padding unlike the element’s <code class=\"language-text\">getBoundingClientRect</code>.</p>\n<h2 id=\"demo\" style=\"position:relative;\"><a href=\"#demo\" aria-label=\"demo permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Demo</h2>\n<p>You can do a lot with this API, from changing the font size based on the element’s size to change the styling when the size changes. Below is one where I’m changing the border radios based on element’s size. Resize your window and see what happens 😎.</p>\n<iframe height=\"265\" style=\"width: 100%;\" scrolling=\"no\" title=\"ResizeObserver\" src=\"//codepen.io/yashints/embed/QWLaMej/?height=265&theme-id=0&default-tab=js,result\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\">\r\n  See the Pen <a href='https://codepen.io/yashints/pen/QWLaMej/'>ResizeObserver</a> by Yaser Adel Mehraban\r\n  (<a href='https://codepen.io/yashints'>@yashints</a>) on <a href='https://codepen.io'>CodePen</a>.\r\n</iframe>\n<h2 id=\"demo-explained\" style=\"position:relative;\"><a href=\"#demo-explained\" aria-label=\"demo explained permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Demo explained</h2>\n<p>The demo is not that complex, we have a <code class=\"language-text\">div</code> which is got two children:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"92918426996109520000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<div class=&quot;boxes&quot;>\r\n  <div class=&quot;box&quot;></div>\r\n  <div class=&quot;box&quot;></div>\r\n</div>`, `92918426996109520000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span> <span class=\"token attr-name\">class</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>boxes<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>\r\n  <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span> <span class=\"token attr-name\">class</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>box<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span>\r\n  <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span> <span class=\"token attr-name\">class</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>box<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span>\r\n<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span></span></pre></div>\n<p>We add a bit of styling so we can see the changes:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"26492798647891825000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`.boxes {\r\n  display: flex;\r\n  flex-wrap: wrap;\r\n  width: 40vw;\r\n  margin-bottom: 50px;\r\n}\r\n.box {\r\n  height: 200px;\r\n  flex-shrink: 1;\r\n  flex-grow: 1;\r\n  margin: 10px;\r\n  box-sizing: border-box;\r\n  border: 3px solid black;\r\n}`, `26492798647891825000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"css\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-css line-numbers\"><code class=\"language-css\"><span class=\"token selector\">.boxes</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">display</span><span class=\"token punctuation\">:</span> flex<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">flex-wrap</span><span class=\"token punctuation\">:</span> wrap<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">width</span><span class=\"token punctuation\">:</span> 40vw<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">margin-bottom</span><span class=\"token punctuation\">:</span> 50px<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n<span class=\"token selector\">.box</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">height</span><span class=\"token punctuation\">:</span> 200px<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">flex-shrink</span><span class=\"token punctuation\">:</span> 1<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">flex-grow</span><span class=\"token punctuation\">:</span> 1<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">margin</span><span class=\"token punctuation\">:</span> 10px<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">box-sizing</span><span class=\"token punctuation\">:</span> border-box<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">border</span><span class=\"token punctuation\">:</span> 3px solid black<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Once we’re done, we add our little snippet to get the observer notify us of changes:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"88830653456726820000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const ro = new ResizeObserver(entries => {\r\n  for (let entry of entries) {\r\n    entry.target.style.borderRadius =\r\n      Math.max(0, 250 - entry.contentRect.width) +\r\n      'px'\r\n  }\r\n})\r\nconsole.log('horay');\r\n// Only observe the 2nd box\r\nro.observe(\r\n  document.querySelector('.box:nth-child(2)')\r\n)`, `88830653456726820000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> ro <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">ResizeObserver</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">entries</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> entry <span class=\"token keyword\">of</span> entries<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    entry<span class=\"token punctuation\">.</span>target<span class=\"token punctuation\">.</span>style<span class=\"token punctuation\">.</span>borderRadius <span class=\"token operator\">=</span>\r\n      Math<span class=\"token punctuation\">.</span><span class=\"token function\">max</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">250</span> <span class=\"token operator\">-</span> entry<span class=\"token punctuation\">.</span>contentRect<span class=\"token punctuation\">.</span>width<span class=\"token punctuation\">)</span> <span class=\"token operator\">+</span>\r\n      <span class=\"token string\">'px'</span>\r\n  <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'horay'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token comment\">// Only observe the 2nd box</span>\r\nro<span class=\"token punctuation\">.</span><span class=\"token function\">observe</span><span class=\"token punctuation\">(</span>\r\n  document<span class=\"token punctuation\">.</span><span class=\"token function\">querySelector</span><span class=\"token punctuation\">(</span><span class=\"token string\">'.box:nth-child(2)'</span><span class=\"token punctuation\">)</span>\r\n<span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>In the call-back, we simply change the border radios based on width of the element, and voila.</p>\n<h2 id=\"browser-support\" style=\"position:relative;\"><a href=\"#browser-support\" aria-label=\"browser support permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Browser support</h2>\n<p><a href=\"https://caniuse.com/#feat=resizeobserver\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Browser support</a> is there for most major browsers, we have Chrome 64+, Firefox 69+, Microsoft Edge 76, and Safari 13-TP supporting the API, but a poor support on mobile browsers.</p>\n<p>Have fun exploring the API and do some cool stuff with it 😍.</p>","timeToRead":2,"frontmatter":{"title":"Let's get to know the ResizeObserver 👋🏼","unformattedDate":"2019-09-05T00:00:00.000Z","date":"Sep 05, 2019","path":"/resize-observer/","author":"Yaser Adel Mehraban","tags":["showdev","webdev","resizeobserver","javascript"],"thumbnail":null}}},{"node":{"id":"3a2ef0c1-4853-57b8-9882-8f239761ed6f","html":"<p>If you ever stumbled upon a piece of vanilla JavaScript code and wanted to refactor it to a module, or have a <code class=\"language-text\">CommonJS</code> module and want to convert it to <a href=\"https://tc39.es/ecma262/#sec-modules\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">ES6 Modules</code></a>, you might have faced a couple of tricky situations. I had to go through one of those recently and stumbled upon some  differences/points that you need to be aware of when working with modules. As always, thought sharing these would help someone else, so here we go 😊.</p>\n<!--more-->\n<h2 id=\"commonjs--code-classlanguage-textrequirecode\" style=\"position:relative;\"><a href=\"#commonjs--code-classlanguage-textrequirecode\" aria-label=\"commonjs  code classlanguage textrequirecode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>CommonJS &#x26; <code class=\"language-text\">require</code></h2>\n<p>This is the most common type of code seen on many open source projects before ES6 was put on earth by the GODS 😁.</p>\n<h3 id=\"usage\" style=\"position:relative;\"><a href=\"#usage\" aria-label=\"usage permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Usage</h3>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"97797259928901580000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const _ = require('underscore'); // from an npm package \r\n\r\nconst reverseString = require('./reverseString.js'); // from internal module`, `97797259928901580000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> _ <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'underscore'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// from an npm package </span>\r\n\r\n<span class=\"token keyword\">const</span> reverseString <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'./reverseString.js'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// from internal module</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"definition\" style=\"position:relative;\"><a href=\"#definition\" aria-label=\"definition permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Definition</h3>\n<p>In the <code class=\"language-text\">reverseString.js</code> you would need to write something like this to get it working:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"87112583620739600000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const reverseString = (sentence) => sentence.split(&quot;&quot;).reverse().join(&quot;&quot;);\r\n\r\nmodule.exports = reverseString;`, `87112583620739600000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token function-variable function\">reverseString</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">sentence</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> sentence<span class=\"token punctuation\">.</span><span class=\"token function\">split</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">reverse</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">join</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\nmodule<span class=\"token punctuation\">.</span>exports <span class=\"token operator\">=</span> reverseString<span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>The only thing you need to pay attention to is that the value you assign to <code class=\"language-text\">module.exports</code> is the same you get when using <code class=\"language-text\">require</code>. And if you want to use the function you just exported:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"43049184019554200000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const reverseString = require('./reverseString.js');\r\n\r\nconsole.log(reverseString(&quot;madam&quot;)); // madam, gotcha 😂`, `43049184019554200000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> reverseString <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'./reverseString.js'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">reverseString</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"madam\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// madam, gotcha 😂</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"multi-export\" style=\"position:relative;\"><a href=\"#multi-export\" aria-label=\"multi export permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Multi export</h3>\n<p>In real life situations, we will need to export more than one function from our module. This is as easy as wrapping all of those in an object. Imagine you have a file called <code class=\"language-text\">stringHelpers.js</code>:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"7546110563202291000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const reverseString = (sentence) => {...};\r\nconst toUpperCase = (sentence) => {...};\r\nconst convertToCamelCase = (sentence) => {...};\r\n\r\n\r\nmodule.exports = {\r\n  reverseString: reverseString, \r\n  toUpperCase, // you can omit the assignment if the name is equal\r\n  toLowerCase: convertToLowerCase,\r\n};`, `7546110563202291000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token function-variable function\">reverseString</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">sentence</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">const</span> <span class=\"token function-variable function\">toUpperCase</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">sentence</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">const</span> <span class=\"token function-variable function\">convertToCamelCase</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">sentence</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\n\r\n\r\nmodule<span class=\"token punctuation\">.</span>exports <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token literal-property property\">reverseString</span><span class=\"token operator\">:</span> reverseString<span class=\"token punctuation\">,</span> \r\n  toUpperCase<span class=\"token punctuation\">,</span> <span class=\"token comment\">// you can omit the assignment if the name is equal</span>\r\n  <span class=\"token literal-property property\">toLowerCase</span><span class=\"token operator\">:</span> convertToLowerCase<span class=\"token punctuation\">,</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>As you can see, the value of the <code class=\"language-text\">module.exports</code> will be an object, which means when using it, you will have to use the properties on the object:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"59216116878494550000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const stringHelpers = require('./stringHelpers.js');\r\n\r\nconsole.log(stringHelpers.reverseString('racecar')); // racecar 🤣`, `59216116878494550000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> stringHelpers <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'./stringHelpers.js'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>stringHelpers<span class=\"token punctuation\">.</span><span class=\"token function\">reverseString</span><span class=\"token punctuation\">(</span><span class=\"token string\">'racecar'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// racecar 🤣</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>We can also rewrite our module in a different way:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"24208613915776045000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`module.exports = {};\r\n\r\nmodule.exports.reverseString = () => {...};\r\nmodule.exports.toUpperCase = () => {...};\r\nmodule.exports.toLowerCase = () => {...};`, `24208613915776045000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\">module<span class=\"token punctuation\">.</span>exports <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\n\r\nmodule<span class=\"token punctuation\">.</span>exports<span class=\"token punctuation\">.</span><span class=\"token function-variable function\">reverseString</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\nmodule<span class=\"token punctuation\">.</span>exports<span class=\"token punctuation\">.</span><span class=\"token function-variable function\">toUpperCase</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\nmodule<span class=\"token punctuation\">.</span>exports<span class=\"token punctuation\">.</span><span class=\"token function-variable function\">toLowerCase</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>These two ways of creating the module are exactly the same, it’s up to you which convention to follow.</p>\n<h2 id=\"es6-modules\" style=\"position:relative;\"><a href=\"#es6-modules\" aria-label=\"es6 modules permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>ES6 Modules</h2>\n<p><a href=\"https://tc39.es/ecma262/#sec-modules\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">ES6 Modules</a> is created to create a format which both <code class=\"language-text\">CommonJS</code> and <code class=\"language-text\">AMD</code> (Async Module Definition) users are happy with. In their simplest form compared to <code class=\"language-text\">CommonJS</code> approach, ES6 Modules <em><strong>always</strong></em> export an object.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"66089320339709160000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const reverseString = (sentence) => {...};\r\n\r\nexport default reverseString;`, `66089320339709160000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token function-variable function\">reverseString</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">sentence</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> reverseString<span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"default-export\" style=\"position:relative;\"><a href=\"#default-export\" aria-label=\"default export permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Default export</h3>\n<p>One of the major benefits of having modules are to hide the internal implementation details and expose only what’s needed. In this case I am just exporting one function, and in addition I am exporting it as <code class=\"language-text\">default</code>. When you export something as <code class=\"language-text\">default</code>, you get to import it with their original name or even an alias. Plus you get to omit the curly braces.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"57742560240779420000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import reverseString from './reverseString.js';\r\n\r\nimport defaultExport from './reverseString.js';\r\n\r\nconsole.log(reverseString('madam')); //madam\r\nconsole.log(defaultExport('madam')); //madam`, `57742560240779420000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">import</span> reverseString <span class=\"token keyword\">from</span> <span class=\"token string\">'./reverseString.js'</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">import</span> defaultExport <span class=\"token keyword\">from</span> <span class=\"token string\">'./reverseString.js'</span><span class=\"token punctuation\">;</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">reverseString</span><span class=\"token punctuation\">(</span><span class=\"token string\">'madam'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">//madam</span>\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">defaultExport</span><span class=\"token punctuation\">(</span><span class=\"token string\">'madam'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">//madam</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p><img src=\"/cf5441038e2c4e6740a3da2df16b9353/madeup.gif\" alt=\"Made up name gif\"></p>\n<p>If you look into the object which is exported from the file, you will see below object:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"79072593923678420000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`{\r\n  default: (sentence) => {...}\r\n}`, `79072593923678420000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token punctuation\">{</span>\r\n  <span class=\"token function-variable function\">default</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">sentence</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>You can also export the function directly:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"39320245249825420000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`export const reverseString = (sentence) => {...};`, `39320245249825420000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> <span class=\"token function-variable function\">reverseString</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">sentence</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>Which will result in:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"51429440706387706000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`{\r\n  reverseString: (sentence) => {...}\r\n}`, `51429440706387706000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token punctuation\">{</span>\r\n  <span class=\"token function-variable function\">reverseString</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">sentence</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>In which case you will need to use its name to be able to import it, plus you have to use curly braces:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"1975165403226420000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import { reverseString } from './reverseString.js';\r\n\r\nconsole.log(reverseString('madam')); //madam`, `1975165403226420000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> reverseString <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'./reverseString.js'</span><span class=\"token punctuation\">;</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">reverseString</span><span class=\"token punctuation\">(</span><span class=\"token string\">'madam'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">//madam</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"mixed-export\" style=\"position:relative;\"><a href=\"#mixed-export\" aria-label=\"mixed export permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Mixed export</h3>\n<p>You can also have a default export alongside named ones:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"11247761288117864000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`export const reverseString = (sentence) => {...};\r\nexport const toUpperCase = (sentence) => {...};\r\nconst convertToCamelCase = (sentence) => {...};\r\n\r\nexport default convertToCamelCase;`, `11247761288117864000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> <span class=\"token function-variable function\">reverseString</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">sentence</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> <span class=\"token function-variable function\">toUpperCase</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">sentence</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token keyword\">const</span> <span class=\"token function-variable function\">convertToCamelCase</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">sentence</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> convertToCamelCase<span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Which then will give you:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"19537403236813767000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`{\r\n  reverseString: (sentence) => {...},\r\n  toUpperCase: (sentence) => {...},\r\n  default: (sentence) => {...}\r\n}`, `19537403236813767000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token punctuation\">{</span>\r\n  <span class=\"token function-variable function\">reverseString</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">sentence</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token function-variable function\">toUpperCase</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">sentence</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token function-variable function\">default</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">sentence</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>And when importing, you can use their names, or import everything in one object:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"48941818788768400000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import convertToCamelCase, { reverseString, toUpperCase } from './stringHelpers.js';\r\n\r\n// or\r\n\r\nimport * as stringHelpers from './stringHelpers.js';`, `48941818788768400000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">import</span> convertToCamelCase<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span> reverseString<span class=\"token punctuation\">,</span> toUpperCase <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'./stringHelpers.js'</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token comment\">// or</span>\r\n\r\n<span class=\"token keyword\">import</span> <span class=\"token operator\">*</span> <span class=\"token keyword\">as</span> stringHelpers <span class=\"token keyword\">from</span> <span class=\"token string\">'./stringHelpers.js'</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Now to be fair, you can change the name of a named export after exporting too:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"64857148065933210000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import { reverseString as madeUpName } from './stringHelpers.js';`, `64857148065933210000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> reverseString <span class=\"token keyword\">as</span> madeUpName <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'./stringHelpers.js'</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<h3 id=\"importing-the-whole-module\" style=\"position:relative;\"><a href=\"#importing-the-whole-module\" aria-label=\"importing the whole module permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Importing the whole module</h3>\n<p>Sometimes you have a block of code which needs to be executed without the need to access any of the module’s internal values. In this case you can import the whole module just to have its global code executed:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"37684520856923270000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// other code or possible exports\r\n\r\nwindow.addEventListener(&quot;load&quot;, function() {\r\n    console.log(&quot;Window is loaded&quot;);\r\n});`, `37684520856923270000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token comment\">// other code or possible exports</span>\r\n\r\nwindow<span class=\"token punctuation\">.</span><span class=\"token function\">addEventListener</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"load\"</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Window is loaded\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Then import the whole module:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"76441729336616170000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import './loadEventListener.js';`, `76441729336616170000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">import</span> <span class=\"token string\">'./loadEventListener.js'</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<h2 id=\"why-should-you-use-modules-\" style=\"position:relative;\"><a href=\"#why-should-you-use-modules-\" aria-label=\"why should you use modules  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why should you use modules 😍?</h2>\n<p>There are many benefits when it comes to use ES6 Modules (or even CommonJS format). I will go through some of those here:</p>\n<ul>\n<li>Ease in sharing the code (both internal and between projects).</li>\n<li>Independant testability.</li>\n<li>Having ability to hide the implementation details.</li>\n<li>Single responsibility principle, code can be split in smaller chunks with a specific purpose.</li>\n<li>Simplifying dependency detection/injection.</li>\n<li>Defining clear interface for a block of code.</li>\n<li>Can be used alongside a dependency injection system to load a block of code.</li>\n<li>Can help the tree shaking to eliminate unused code.</li>\n</ul>\n<h2 id=\"whats-the-catch-\" style=\"position:relative;\"><a href=\"#whats-the-catch-\" aria-label=\"whats the catch  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What’s the catch 🤔?</h2>\n<p>There are a few points you should be aware of while using ES6 Modules:</p>\n<ul>\n<li>They are running in strict mode by default (you don’t need to have <code class=\"language-text\">use strict</code> anymore).</li>\n<li>Top level value of <code class=\"language-text\">this</code> is <code class=\"language-text\">undefined</code>.</li>\n<li>Top level variables are local to the module.</li>\n<li>ES6 Modules are loaded and executed asynchronously. This means the browser will finish parsing and loading the <code class=\"language-text\">HTML</code> first, and then executes the module code. The load can be in parallel or be done beforehand using <code class=\"language-text\">link rel=preload</code>.</li>\n</ul>\n<h2 id=\"trick-or-treat\" style=\"position:relative;\"><a href=\"#trick-or-treat\" aria-label=\"trick or treat permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Trick or treat?</h2>\n<p>This one is probably my favourite part. You can dynamically load the module and execute it. This is done by using the <code class=\"language-text\">import</code> keyword as a function rather than normal command.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"43968591991716040000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import('/modules/my-module.js')\r\n  .then((module) => {\r\n    // Do something with the module.\r\n  });`, `43968591991716040000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">import</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/modules/my-module.js'</span><span class=\"token punctuation\">)</span>\r\n  <span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">module</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token comment\">// Do something with the module.</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Or even better:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"15612156166815082000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const module = await import('/modules/my-module.js');`, `15612156166815082000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> module <span class=\"token operator\">=</span> <span class=\"token keyword\">await</span> <span class=\"token keyword\">import</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/modules/my-module.js'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<h3 id=\"wow-thats-great-but-why-\" style=\"position:relative;\"><a href=\"#wow-thats-great-but-why-\" aria-label=\"wow thats great but why  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Wow that’s great, but why 🧐?</h3>\n<p>Suppose you have an application which has different user experience or behaviour for mobile vs desktop. This problem can’t be solved just by having a responsive design, so you build a <em>page renderer</em> which loads and renders each page based on visitor’s platform.</p>\n<p>Technically this is just a strategy pattern where the page renderer decides which module to load at run time. This can be solved easily using dynamic imports. There are many other use cases out there which can be benefited from dynamic imports.</p>\n<p>However, with great power, comes great responsibility. You need to be careful when to use this fantastic feature as it has its own drawbacks. At the least, you lose automatic bundling of lazy-loaded chunks, type inference and more.</p>\n<h2 id=\"how-can-i-use-them\" style=\"position:relative;\"><a href=\"#how-can-i-use-them\" aria-label=\"how can i use them permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How can I use them?</h2>\n<p>I’ve shown you many examples in this article how to consume a module in another file or module. However, sometimes you need to consume them in browser (from <code class=\"language-text\">HTML</code>). Chrome, Safari, Firefox and Edge all support ES6 Modules, however you need to change your script tag’s type from script to module:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"80958480770650010000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// html.js\r\nexport function tag (tag, text) {\r\n  const el = document.createElement(tag)\r\n  el.textContent = text\r\n\r\n  return el\r\n}`, `80958480770650010000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token comment\">// html.js</span>\r\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">function</span> <span class=\"token function\">tag</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">tag<span class=\"token punctuation\">,</span> text</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">const</span> el <span class=\"token operator\">=</span> document<span class=\"token punctuation\">.</span><span class=\"token function\">createElement</span><span class=\"token punctuation\">(</span>tag<span class=\"token punctuation\">)</span>\r\n  el<span class=\"token punctuation\">.</span>textContent <span class=\"token operator\">=</span> text\r\n\r\n  <span class=\"token keyword\">return</span> el\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"32230064003187663000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<script type=&quot;module&quot;>\r\n  import { tag } from './html.js'\r\n\r\n  const h1 = tag('h1', ' Hello Modules!')\r\n  document.body.appendChild(h1)\r\n</script>`, `32230064003187663000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>script</span> <span class=\"token attr-name\">type</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>module<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"><span class=\"token language-javascript\">\r\n  <span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> tag <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'./html.js'</span>\r\n\r\n  <span class=\"token keyword\">const</span> h1 <span class=\"token operator\">=</span> <span class=\"token function\">tag</span><span class=\"token punctuation\">(</span><span class=\"token string\">'h1'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">' Hello Modules!'</span><span class=\"token punctuation\">)</span>\r\n  document<span class=\"token punctuation\">.</span>body<span class=\"token punctuation\">.</span><span class=\"token function\">appendChild</span><span class=\"token punctuation\">(</span>h1<span class=\"token punctuation\">)</span>\r\n</span></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>script</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Or just simply import the module file into another file and use external reference:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"80406228456911370000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// app.js\r\nimport { tag } from './html.js'\r\n\r\nconst h1 = tag('h1', ' Hello Modules!')\r\ndocument.body.appendChild(h1)`, `80406228456911370000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token comment\">// app.js</span>\r\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> tag <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'./html.js'</span>\r\n\r\n<span class=\"token keyword\">const</span> h1 <span class=\"token operator\">=</span> <span class=\"token function\">tag</span><span class=\"token punctuation\">(</span><span class=\"token string\">'h1'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">' Hello Modules!'</span><span class=\"token punctuation\">)</span>\r\ndocument<span class=\"token punctuation\">.</span>body<span class=\"token punctuation\">.</span><span class=\"token function\">appendChild</span><span class=\"token punctuation\">(</span>h1<span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"93419032564653050000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<script type=&quot;module&quot; src=&quot;app.js&quot;></script>`, `93419032564653050000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>script</span> <span class=\"token attr-name\">type</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>module<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>app.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>script</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p><strong>Tip</strong>: There are still some old browsers out there <strong>cough</strong> IE11 <strong>cough</strong> which doesn’t support it, so make sure you have a back up plan. This can be done using <code class=\"language-text\">nomodule</code> attribute.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"7448234496639384000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<script type=&quot;module&quot; src=&quot;module.js&quot;></script>\r\n<script nomodule src=&quot;fallback.js&quot;></script>`, `7448234496639384000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token operator\">&lt;</span>script type<span class=\"token operator\">=</span><span class=\"token string\">\"module\"</span> src<span class=\"token operator\">=</span><span class=\"token string\">\"module.js\"</span><span class=\"token operator\">></span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>script<span class=\"token operator\">></span>\r\n<span class=\"token operator\">&lt;</span>script nomodule src<span class=\"token operator\">=</span><span class=\"token string\">\"fallback.js\"</span><span class=\"token operator\">></span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>script<span class=\"token operator\">></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>We had a look at before and after ES6 Modules and saw some of the differences in syntax. We saw the power of the module system in JavaScript and its benefits when it comes to using it in larger code bases. And at last we reviewed dynamic import which has massive potential but should be used with cautious.</p>\n<p>Hope you’ve enjoyed the read and till next time 👋🏼.</p>","timeToRead":6,"frontmatter":{"title":"JavaScript modules, the good, the bad and the ugly 🧐","unformattedDate":"2019-08-27T00:00:00.000Z","date":"Aug 27, 2019","path":"/js-modules/","author":"Yaser Adel Mehraban","tags":["showdev","webdev","javascript","module"],"thumbnail":null}}},{"node":{"id":"53d36f7f-5415-5d38-a5bf-b9dada4d2b23","html":"<p>First of all let me apologise for not writing as frequent as I would’ve liked. It’s been a crazy busy couple of weeks and I had heaps of fun speaking at <a href=\"https://dddperth.com/agenda?sessionId=00311b92-6c21-47a8-b8d2-af325581d6f9\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">DDD Perth 2019</a>.</p>\n<p>That taken care of, this time I thought let’s go through the new features which are added into the <a href=\"https://www.ecma-international.org/ecma-262/10.0/index.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">ECMAScript 2019 (aka ES2019 or ES10)</a>, because they’re super exciting and make our lives much easier 😎.</p>\n<!--more-->\n<h2 id=\"tldr\" style=\"position:relative;\"><a href=\"#tldr\" aria-label=\"tldr permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>TLDR;</h2>\n<p>At a glance, this version adds a few built-in functions on <code class=\"language-text\">Array.prototype</code>, <code class=\"language-text\">flat</code> and <code class=\"language-text\">flatMap</code>. Then we have <code class=\"language-text\">Object.fromEntries</code> for directly turning the return value of <code class=\"language-text\">Object.entries</code> into a new <em>Object</em>.</p>\n<p>We also have <code class=\"language-text\">trimStart</code> and <code class=\"language-text\">trimEnd</code> on <code class=\"language-text\">String.prototype</code> over the widely used non-standard versions <code class=\"language-text\">String.prototype.trimLeft</code> and <code class=\"language-text\">trimRight</code>.</p>\n<p>Another exciting feature is optional <code class=\"language-text\">catch</code> binding parameters. In addition to those, there are some JSON improvements, addition of <code class=\"language-text\">Symbol.prototype.description</code> which allows you to specify a description for your symbol, <code class=\"language-text\">JSON.stringify</code> returns well-formed <strong>UTF-8</strong> regardless of input, and at last clarifying <code class=\"language-text\">Function.prototype.toString</code> by requiring that it either return the corresponding original source text or a standard placeholder.</p>\n<p>So if you’re ready let’s dig in.</p>\n<h3 id=\"code-classlanguage-textarrayprototypeflat-flatmapcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textarrayprototypeflat-flatmapcode\" aria-label=\"code classlanguage textarrayprototypeflat flatmapcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">Array.prototype.{flat, flatMap}</code></h3>\n<p><code class=\"language-text\">flat()</code> is a new method which let’s you create a one-dimensional array from a multi-dimensional one.</p>\n<p>Imagine we have an array like below:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"6893819996006267000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const myArray = [1, 2, 3, [4, 5, 6, [7, 8, 9, [10, 11, 12]]]];`, `6893819996006267000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> myArray <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">,</span> <span class=\"token number\">5</span><span class=\"token punctuation\">,</span> <span class=\"token number\">6</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">,</span> <span class=\"token number\">8</span><span class=\"token punctuation\">,</span> <span class=\"token number\">9</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">,</span> <span class=\"token number\">11</span><span class=\"token punctuation\">,</span> <span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>Prior to <code class=\"language-text\">flat</code> if you wanted to achieve this, you had to do something like this:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"2513915572999203300\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const myNewArray = [].concat.apply([], myArray)\r\n// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`, `2513915572999203300`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> myNewArray <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">concat</span><span class=\"token punctuation\">.</span><span class=\"token function\">apply</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> myArray<span class=\"token punctuation\">)</span>\r\n<span class=\"token comment\">// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>Or:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"10524976869743251000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var myNewArray = myArray.reduce(\r\n  function(prev,curr) {\r\n    return prev.concat(curr)\r\n  }\r\n)\r\n// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`, `10524976869743251000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">var</span> myNewArray <span class=\"token operator\">=</span> myArray<span class=\"token punctuation\">.</span><span class=\"token function\">reduce</span><span class=\"token punctuation\">(</span>\r\n  <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">prev<span class=\"token punctuation\">,</span>curr</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">return</span> prev<span class=\"token punctuation\">.</span><span class=\"token function\">concat</span><span class=\"token punctuation\">(</span>curr<span class=\"token punctuation\">)</span>\r\n  <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">)</span>\r\n<span class=\"token comment\">// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>With this new addition, it would be as simple as:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"40995054898060124000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var myNewArray = myArray.flat(4);\r\n// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`, `40995054898060124000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">var</span> myNewArray <span class=\"token operator\">=</span> myArray<span class=\"token punctuation\">.</span><span class=\"token function\">flat</span><span class=\"token punctuation\">(</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token comment\">// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>You can also chain multiple calls:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"75096317484716150000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var myNewArray = myArray.flat().flat().flat().flat();\r\n// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`, `75096317484716150000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">var</span> myNewArray <span class=\"token operator\">=</span> myArray<span class=\"token punctuation\">.</span><span class=\"token function\">flat</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">flat</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">flat</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">flat</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token comment\">// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>The argument into the <code class=\"language-text\">flat</code> function just specifies how deep it should look into the array. If you’re not sure how deep the array is, simply use <code class=\"language-text\">Infinity</code> as input:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"39227244846955676000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var myNewArray = myArray.flat(Infinity);\r\n// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`, `39227244846955676000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">var</span> myNewArray <span class=\"token operator\">=</span> myArray<span class=\"token punctuation\">.</span><span class=\"token function\">flat</span><span class=\"token punctuation\">(</span><span class=\"token number\">Infinity</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token comment\">// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>And if you don’t provide any input, by default it only goes one level deep:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"7697364309605081000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var myNewArray = myArray.flat(); \r\n// [1, 2, 3, 4, 5, 6, Array(4)];`, `7697364309605081000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">var</span> myNewArray <span class=\"token operator\">=</span> myArray<span class=\"token punctuation\">.</span><span class=\"token function\">flat</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> \r\n<span class=\"token comment\">// [1, 2, 3, 4, 5, 6, Array(4)];</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<hr/>\n<p><code class=\"language-text\">flatMap</code> on the other hand, allows you to map each element using a mapping function and then flattens the result into a new array. Think of it as chaining a <code class=\"language-text\">map</code> function with a single <code class=\"language-text\">flat</code>. However, it can be really helpful both in terms of usage and efficiency of execution.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"2658992310512054000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let myArray = [1, 2, 3, 4, 5];\r\n\r\nlet mappedArray = myArray.map(x => [x, x * 2]);\r\n// [Array(2), Array(2), Array(2), Array(2), Array(2)]\r\n// 0: (2)[1, 2]\r\n// 1: (2)[2, 4]\r\n// 2: (2)[3, 6]\r\n// 3: (2)[4, 8]\r\n// 4: (2)[5, 10]\r\n\r\nlet mappedFlattenedArr = mappedArray.flat();\r\n\r\n// [1, 2, 2, 4, 3, 6, 4, 8, 5, 10]\r\n\r\nlet myNewArray = myArray.flatMap(v => [v, v * 2]);\r\n// [1, 2, 2, 4, 3, 6, 4, 8, 5, 10]`, `2658992310512054000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">let</span> myArray <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token number\">4</span><span class=\"token punctuation\">,</span> <span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">let</span> mappedArray <span class=\"token operator\">=</span> myArray<span class=\"token punctuation\">.</span><span class=\"token function\">map</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">x</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span>x<span class=\"token punctuation\">,</span> x <span class=\"token operator\">*</span> <span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token comment\">// [Array(2), Array(2), Array(2), Array(2), Array(2)]</span>\r\n<span class=\"token comment\">// 0: (2)[1, 2]</span>\r\n<span class=\"token comment\">// 1: (2)[2, 4]</span>\r\n<span class=\"token comment\">// 2: (2)[3, 6]</span>\r\n<span class=\"token comment\">// 3: (2)[4, 8]</span>\r\n<span class=\"token comment\">// 4: (2)[5, 10]</span>\r\n\r\n<span class=\"token keyword\">let</span> mappedFlattenedArr <span class=\"token operator\">=</span> mappedArray<span class=\"token punctuation\">.</span><span class=\"token function\">flat</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token comment\">// [1, 2, 2, 4, 3, 6, 4, 8, 5, 10]</span>\r\n\r\n<span class=\"token keyword\">let</span> myNewArray <span class=\"token operator\">=</span> myArray<span class=\"token punctuation\">.</span><span class=\"token function\">flatMap</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">v</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span>v<span class=\"token punctuation\">,</span> v <span class=\"token operator\">*</span> <span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token comment\">// [1, 2, 2, 4, 3, 6, 4, 8, 5, 10]</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textstringprototypetrimstart-trimendcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textstringprototypetrimstart-trimendcode\" aria-label=\"code classlanguage textstringprototypetrimstart trimendcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">String.prototype.{trimStart, .trimEnd}</code></h3>\n<p>These methods are fairly obvious as to what they will do for us. Just keep in mind that we had <code class=\"language-text\">trimLeft</code> which will be replaced by <code class=\"language-text\">trimStart</code> and <code class=\"language-text\">trimRight</code> which will be replaced by <code class=\"language-text\">trimEnd</code>.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"2837229623663307000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let message = ' This is a string with white space around ';\r\n\r\nmessage = message.trimStart();\r\n// 'This is a string with white space around '\r\n\r\nmessage = message.trimEnd();\r\n// 'This is a string with white space around'`, `2837229623663307000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">let</span> message <span class=\"token operator\">=</span> <span class=\"token string\">' This is a string with white space around '</span><span class=\"token punctuation\">;</span>\r\n\r\nmessage <span class=\"token operator\">=</span> message<span class=\"token punctuation\">.</span><span class=\"token function\">trimStart</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token comment\">// 'This is a string with white space around '</span>\r\n\r\nmessage <span class=\"token operator\">=</span> message<span class=\"token punctuation\">.</span><span class=\"token function\">trimEnd</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token comment\">// 'This is a string with white space around'</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textobjectfromentriescode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textobjectfromentriescode\" aria-label=\"code classlanguage textobjectfromentriescode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">Object.fromEntries</code></h3>\n<p>This method get an <code class=\"language-text\">Iterable</code> and transforms key-value pairs to an object. But let’s see what is an <code class=\"language-text\">Iterable</code>:</p>\n<blockquote>\n<p>JavaScript supports a protocol by which objects such as arrays can be used by control structures such as <code class=\"language-text\">for</code>…<code class=\"language-text\">of</code> and the spread operator <code class=\"language-text\">...</code> to loop through data sequentially. This is referred to as the iterable and the data structures that support this functionality are called iterables. While JavaScript provides maps, arrays and sets with an iterable property from the get-go, plain objects do not have this by default.</p>\n</blockquote>\n<p>To see this in action:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"84816668586520690000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let entries = new Map([[&quot;name&quot;, &quot;john&quot;], [&quot;age&quot;, 22]]);\r\n\r\nlet newObj = Object.fromEntries(entries);\r\n// { name: 'john', age: 22 }`, `84816668586520690000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">let</span> entries <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Map</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token string\">\"name\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"john\"</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token string\">\"age\"</span><span class=\"token punctuation\">,</span> <span class=\"token number\">22</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">let</span> newObj <span class=\"token operator\">=</span> Object<span class=\"token punctuation\">.</span><span class=\"token function\">fromEntries</span><span class=\"token punctuation\">(</span>entries<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token comment\">// { name: 'john', age: 22 }</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span></span></pre></div>\n<p>A real life use case is when parsing query strings:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"71048431634011600000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let query = Object.fromEntries(new URLSearchParams('foo=bar&baz=qux'));\r\n\r\n// { foo: 'bar', baz: 'qux' }`, `71048431634011600000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">let</span> query <span class=\"token operator\">=</span> Object<span class=\"token punctuation\">.</span><span class=\"token function\">fromEntries</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">new</span> <span class=\"token class-name\">URLSearchParams</span><span class=\"token punctuation\">(</span><span class=\"token string\">'foo=bar&amp;baz=qux'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token comment\">// { foo: 'bar', baz: 'qux' }</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"optional-catch-binding\" style=\"position:relative;\"><a href=\"#optional-catch-binding\" aria-label=\"optional catch binding permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Optional Catch Binding</h3>\n<p>Optional catch binding allows us to use <code class=\"language-text\">try/catch</code> without the <code class=\"language-text\">error</code> parameter inside the catch block.</p>\n<p>Previously you had to use this syntax regardless of whether you cared about <code class=\"language-text\">err</code> or not, such as when you had to fall back to feature to support older browsers:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"60527336140552815000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`try {\r\n  // try to use a web feature which may not be implemented\r\n} catch (unused) {\r\n  // fall back to a less desirable web feature with broader support\r\n}`, `60527336140552815000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">try</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token comment\">// try to use a web feature which may not be implemented</span>\r\n<span class=\"token punctuation\">}</span> <span class=\"token keyword\">catch</span> <span class=\"token punctuation\">(</span>unused<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token comment\">// fall back to a less desirable web feature with broader support</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>With <code class=\"language-text\">ES2019</code> you can do:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"59224185000542120000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`try {\r\n  // ...\r\n} catch {\r\n  // ...\r\n}`, `59224185000542120000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">try</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token comment\">// ...</span>\r\n<span class=\"token punctuation\">}</span> <span class=\"token keyword\">catch</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token comment\">// ...</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textsymboldescriptioncode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textsymboldescriptioncode\" aria-label=\"code classlanguage textsymboldescriptioncode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">Symbol.description</code></h3>\n<p>This new read-only description property is a string returning the optional description of <code class=\"language-text\">Symbol</code> objects. This method is encouraged to be used instead of <code class=\"language-text\">Symbol.prototype.toString</code>\r\nwhere it wasn’t obvious what will be returned.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"99601005929547100000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`let description = 'This symbol represents an emoji 😁';\r\n\r\nlet mySym = Symbol(description);\r\n// Symbol('This symbol represents an emoji 😁')\r\n\r\nconsole.log(mySym.description);\r\n'This symbol represents an emoji 😁'`, `99601005929547100000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">let</span> description <span class=\"token operator\">=</span> <span class=\"token string\">'This symbol represents an emoji 😁'</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">let</span> mySym <span class=\"token operator\">=</span> <span class=\"token function\">Symbol</span><span class=\"token punctuation\">(</span>description<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token comment\">// Symbol('This symbol represents an emoji 😁')</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>mySym<span class=\"token punctuation\">.</span>description<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token string\">'This symbol represents an emoji 😁'</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textfunctiontostringcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textfunctiontostringcode\" aria-label=\"code classlanguage textfunctiontostringcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">Function.toString</code></h3>\n<p>This method is a really useful one which returns the source code of a function. Imagine doing a troubleshooting on a block of code which is using a third party function. This can potentially help you to see the implementation (given it’s got source maps).</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"7939666379531385000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function myFn(emoji) {\r\n  let msg = \\`\\${emoji} is hellav an emoji\\`;\r\n  console.log(msg);\r\n}\r\n\r\nconsole.log(myFn.toString());\r\n\r\n// &quot;function myFn(emoji) {\r\n//   let msg = \\`\\${emoji} is hellav an emoji\\`;\r\n//   console.log(msg);\r\n// }&quot;`, `7939666379531385000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">myFn</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">emoji</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">let</span> msg <span class=\"token operator\">=</span> <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>emoji<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\"> is hellav an emoji</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">;</span>\r\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>msg<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>myFn<span class=\"token punctuation\">.</span><span class=\"token function\">toString</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token comment\">// \"function myFn(emoji) {</span>\r\n<span class=\"token comment\">//   let msg = `${emoji} is hellav an emoji`;</span>\r\n<span class=\"token comment\">//   console.log(msg);</span>\r\n<span class=\"token comment\">// }\"</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Of course this doesn’t work for everything. If we try it for <code class=\"language-text\">map</code> function on array:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"71701125668128140000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`Array.prototype.map.toString();\r\n\r\n// &quot;function map() { [native code] }&quot;`, `71701125668128140000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token class-name\">Array</span><span class=\"token punctuation\">.</span>prototype<span class=\"token punctuation\">.</span>map<span class=\"token punctuation\">.</span><span class=\"token function\">toString</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token comment\">// \"function map() { [native code] }\"</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>Since the implementation is not written in JavaScript, it will just show you that this function is written in native code.</p>\n<h3 id=\"code-classlanguage-textjsonstringifycode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textjsonstringifycode\" aria-label=\"code classlanguage textjsonstringifycode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">JSON.stringify</code></h3>\n<p>The team have done an improvement with <strong>Unicode</strong> characters and now this method can’t return malformed data.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"79189388891340200000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// Non-BMP characters still serialize to surrogate pairs.\r\nJSON.stringify('𝌆')\r\n// → '&quot;𝌆&quot;'\r\nJSON.stringify('\\uD834\\uDF06')\r\n// → '&quot;𝌆&quot;'\r\n\r\n// Unpaired surrogate code units will serialize to escape sequences.\r\nJSON.stringify('\\uDF06\\uD834')\r\n// → '&quot;\\\\udf06\\\\ud834&quot;'\r\nJSON.stringify('\\uDEAD')\r\n// → '&quot;\\\\udead&quot;'`, `79189388891340200000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token comment\">// Non-BMP characters still serialize to surrogate pairs.</span>\r\n<span class=\"token constant\">JSON</span><span class=\"token punctuation\">.</span><span class=\"token function\">stringify</span><span class=\"token punctuation\">(</span><span class=\"token string\">'𝌆'</span><span class=\"token punctuation\">)</span>\r\n<span class=\"token comment\">// → '\"𝌆\"'</span>\r\n<span class=\"token constant\">JSON</span><span class=\"token punctuation\">.</span><span class=\"token function\">stringify</span><span class=\"token punctuation\">(</span><span class=\"token string\">'\\uD834\\uDF06'</span><span class=\"token punctuation\">)</span>\r\n<span class=\"token comment\">// → '\"𝌆\"'</span>\r\n\r\n<span class=\"token comment\">// Unpaired surrogate code units will serialize to escape sequences.</span>\r\n<span class=\"token constant\">JSON</span><span class=\"token punctuation\">.</span><span class=\"token function\">stringify</span><span class=\"token punctuation\">(</span><span class=\"token string\">'\\uDF06\\uD834'</span><span class=\"token punctuation\">)</span>\r\n<span class=\"token comment\">// → '\"\\\\udf06\\\\ud834\"'</span>\r\n<span class=\"token constant\">JSON</span><span class=\"token punctuation\">.</span><span class=\"token function\">stringify</span><span class=\"token punctuation\">(</span><span class=\"token string\">'\\uDEAD'</span><span class=\"token punctuation\">)</span>\r\n<span class=\"token comment\">// → '\"\\\\udead\"'</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textarraysortcode-stability\" style=\"position:relative;\"><a href=\"#code-classlanguage-textarraysortcode-stability\" aria-label=\"code classlanguage textarraysortcode stability permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">Array.sort</code> Stability</h3>\n<p>The team has decided to change the sort implementation so that it would be stable (that is, elements that compare equal must remain in their original order).</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"11540727351695067000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const grades = [\r\n  { topic: 'math', grade: 10 },\r\n  { topic: 'literacy', grade: 10 },\r\n  { topic: 'chemical', grade: 13 },\r\n  { topic: 'geography', grade: 12 },\r\n  { topic: 'modern history', grade: 12 },\r\n  { topic: 'art', grade: 13 },\r\n  { topic: 'computer basics', grade: 14 },\r\n  { topic: 'algebra', grade: 14 },\r\n];\r\n\r\ngrades.sort(a, b => a.grade - b.grade);\r\n\r\n// 0: {topic: &quot;math&quot;, grade: 10}\r\n// 1: {topic: &quot;literacy&quot;, grade: 10}\r\n// 2: {topic: &quot;geography&quot;, grade: 12}\r\n// 3: {topic: &quot;modern history&quot;, grade: 12}\r\n// 4: {topic: &quot;chemical&quot;, grade: 13}\r\n// 5: {topic: &quot;art&quot;, grade: 13}\r\n// 6: {topic: &quot;computer basics&quot;, grade: 14}\r\n// 7: {topic: &quot;algebra&quot;, grade: 14}`, `11540727351695067000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> grades <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span>\r\n  <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">topic</span><span class=\"token operator\">:</span> <span class=\"token string\">'math'</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">grade</span><span class=\"token operator\">:</span> <span class=\"token number\">10</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">topic</span><span class=\"token operator\">:</span> <span class=\"token string\">'literacy'</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">grade</span><span class=\"token operator\">:</span> <span class=\"token number\">10</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">topic</span><span class=\"token operator\">:</span> <span class=\"token string\">'chemical'</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">grade</span><span class=\"token operator\">:</span> <span class=\"token number\">13</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">topic</span><span class=\"token operator\">:</span> <span class=\"token string\">'geography'</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">grade</span><span class=\"token operator\">:</span> <span class=\"token number\">12</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">topic</span><span class=\"token operator\">:</span> <span class=\"token string\">'modern history'</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">grade</span><span class=\"token operator\">:</span> <span class=\"token number\">12</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">topic</span><span class=\"token operator\">:</span> <span class=\"token string\">'art'</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">grade</span><span class=\"token operator\">:</span> <span class=\"token number\">13</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">topic</span><span class=\"token operator\">:</span> <span class=\"token string\">'computer basics'</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">grade</span><span class=\"token operator\">:</span> <span class=\"token number\">14</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">topic</span><span class=\"token operator\">:</span> <span class=\"token string\">'algebra'</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">grade</span><span class=\"token operator\">:</span> <span class=\"token number\">14</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n\r\ngrades<span class=\"token punctuation\">.</span><span class=\"token function\">sort</span><span class=\"token punctuation\">(</span>a<span class=\"token punctuation\">,</span> <span class=\"token parameter\">b</span> <span class=\"token operator\">=></span> a<span class=\"token punctuation\">.</span>grade <span class=\"token operator\">-</span> b<span class=\"token punctuation\">.</span>grade<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token comment\">// 0: {topic: \"math\", grade: 10}</span>\r\n<span class=\"token comment\">// 1: {topic: \"literacy\", grade: 10}</span>\r\n<span class=\"token comment\">// 2: {topic: \"geography\", grade: 12}</span>\r\n<span class=\"token comment\">// 3: {topic: \"modern history\", grade: 12}</span>\r\n<span class=\"token comment\">// 4: {topic: \"chemical\", grade: 13}</span>\r\n<span class=\"token comment\">// 5: {topic: \"art\", grade: 13}</span>\r\n<span class=\"token comment\">// 6: {topic: \"computer basics\", grade: 14}</span>\r\n<span class=\"token comment\">// 7: {topic: \"algebra\", grade: 14}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h3>\n<p>In general JavaScript is moving in the right direction for helping developers write more stable, reliable and consistent code. You can find more information on their <a href=\"https://github.com/tc39/proposals\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">GitHub repo here</a>.</p>\n<p>Hope to see more awesome features in JavaScript and see you soon with another article.</p>","timeToRead":5,"frontmatter":{"title":"JavaScript: useful features of ECMAScript 2019 reviewed","unformattedDate":"2019-08-06T00:00:00.000Z","date":"Aug 06, 2019","path":"/es2019/","author":"Yaser Adel Mehraban","tags":["webdev","javascript","showdev","es2019"],"thumbnail":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB10lEQVR42k2R7U8TQRDGr+2+9CpSaG0JatTg3+4HP5oSqR8gEEBKCUqtBQlEMSZo6Nu99a7X291xZtsak83lsvv8npl5xtEPJei7MHDN0NXDoh5JPRZ4lP3Br1rcSHxCDSpJ3C8h6CwxV42IMZ6k4wtICiYQEHGYMDOW83trJ/5ZOECYVAtMGJ9DyE0oep/qqVe8/7F2960CCcN7e8iCyoyKBM+7stU4BNxEAmZ89FDafrcFkL//udrceQmQU5GEUICPLiRWY66GRWfZqiUnUscCpZ3zjePDZwCOSgvvG1uD3yug8vQUChMwatDWd/RiSEYPCdcJQ/j2en1//znCviebO68mGMGsYKY4P8JcY/92fmeZECd4yk3KQOcCT2w3Xn+/XW+fbu7tkUsWk6+ekExTcQvP29Z2WhWJNBIJhqxzw/6jOOahLyNMweTI1OQh5ToSJP4fzgIJinXbld0PmxdnT96+edE6qp0c17qd8lVv9eb68WW3fH5aaX+sZZHEFc53tkib4Bm/65UvWtWbr2snB/WD3Y3LTqV1VP/crv75tdI6rDUbT7+cVVPcVmi3hWnjujB05WEzEsPLfKlw/qQAU6YDYWIMCSdimScUhhLammMiYeD+BWwvoragF0BGAAAAAElFTkSuQmCC"},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/0010bff258a8fb3470b02787d5fbb94a/bda6f/es.jpg","srcSet":"/static/0010bff258a8fb3470b02787d5fbb94a/ebf29/es.jpg 750w,\n/static/0010bff258a8fb3470b02787d5fbb94a/1c35b/es.jpg 1080w,\n/static/0010bff258a8fb3470b02787d5fbb94a/bda6f/es.jpg 1280w","sizes":"(min-width: 1280px) 1280px, 100vw"},"sources":[{"srcSet":"/static/0010bff258a8fb3470b02787d5fbb94a/ad370/es.webp 750w,\n/static/0010bff258a8fb3470b02787d5fbb94a/10f26/es.webp 1080w,\n/static/0010bff258a8fb3470b02787d5fbb94a/5d501/es.webp 1280w","type":"image/webp","sizes":"(min-width: 1280px) 1280px, 100vw"}]},"width":1280,"height":710}}}}}},{"node":{"id":"a97edd92-c626-50e0-88f7-816e694d07af","html":"<p>You might be wondering why I am writing less these days. I assure you, it’s not because I am getting lazy (I am ATM 🤩), it’s just that I am on a long overdue holiday. But to keep my juices going, I thought now that I am having fun, let’s write a fun post 😊.</p>\n<!--more-->\n<p>In this post I will go through some of the funniest yet unbelievable JavaScript snippets ever. Are you ready?</p>\n<h2 id=\"code-classlanguage-textcode-is-equal-code-classlanguage-textcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textcode-is-equal-code-classlanguage-textcode\" aria-label=\"code classlanguage textcode is equal code classlanguage textcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">[]</code> is equal <code class=\"language-text\">![]</code></h2>\n<p>Array is equal to not array 😂:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"63015057189728820000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`;[] == ![] // -> true`, `63015057189728820000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token punctuation\">;</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token operator\">!</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span> <span class=\"token comment\">// -> true</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p><strong>💡 What’s happening?</strong></p>\n<p>The abstract equality operator converts both sides to numbers before comparing them, and both sides will be converted to <code class=\"language-text\">0</code> for different reasons. Arrays are truthy, so the right hand side becomes false which then is coerced to <code class=\"language-text\">0</code>. On the left however, the empty array is coerced to a number without becoming a boolean first, and empty arrays are coerced to <code class=\"language-text\">0</code>, despite being truthy 🤯.</p>\n<h2 id=\"code-classlanguage-texttruecode-is-code-classlanguage-textfalsecode\" style=\"position:relative;\"><a href=\"#code-classlanguage-texttruecode-is-code-classlanguage-textfalsecode\" aria-label=\"code classlanguage texttruecode is code classlanguage textfalsecode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">true</code> is <code class=\"language-text\">false</code></h2>\n<p>True is false:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"13730440912950725000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`!!'false' == !!'true' // -> true\r\n!!'false' === !!'true' // -> true`, `13730440912950725000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token operator\">!</span><span class=\"token operator\">!</span><span class=\"token string\">'false'</span> <span class=\"token operator\">==</span> <span class=\"token operator\">!</span><span class=\"token operator\">!</span><span class=\"token string\">'true'</span> <span class=\"token comment\">// -> true</span>\r\n<span class=\"token operator\">!</span><span class=\"token operator\">!</span><span class=\"token string\">'false'</span> <span class=\"token operator\">===</span> <span class=\"token operator\">!</span><span class=\"token operator\">!</span><span class=\"token string\">'true'</span> <span class=\"token comment\">// -> true</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p><strong>💡 What’s happening?</strong></p>\n<p><code class=\"language-text\">true</code> is <code class=\"language-text\">truthy</code> and is represented by value 1 (number), <code class=\"language-text\">true</code> in string form, is <code class=\"language-text\">NaN</code>. So:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"56467176367363630000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`true == 'true' // -> false\r\nfalse == 'false' // -> false`, `56467176367363630000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token boolean\">true</span> <span class=\"token operator\">==</span> <span class=\"token string\">'true'</span> <span class=\"token comment\">// -> false</span>\r\n<span class=\"token boolean\">false</span> <span class=\"token operator\">==</span> <span class=\"token string\">'false'</span> <span class=\"token comment\">// -> false</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p><code class=\"language-text\">false</code> is not the empty string, so it’s a truthy value, so:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"79606073740216200000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`!!'false' // -> true\r\n!!'true' // -> true`, `79606073740216200000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token operator\">!</span><span class=\"token operator\">!</span><span class=\"token string\">'false'</span> <span class=\"token comment\">// -> true</span>\r\n<span class=\"token operator\">!</span><span class=\"token operator\">!</span><span class=\"token string\">'true'</span> <span class=\"token comment\">// -> true</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>Cool, ha? 😎</p>\n<h2 id=\"banana-\" style=\"position:relative;\"><a href=\"#banana-\" aria-label=\"banana  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>baNaNa 🍌</h2>\n<p>Let’s create a banana:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"35440683457763987000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`'b' + 'a' + +'a' + 'a' // -> baNaNa`, `35440683457763987000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token string\">'b'</span> <span class=\"token operator\">+</span> <span class=\"token string\">'a'</span> <span class=\"token operator\">+</span> <span class=\"token operator\">+</span><span class=\"token string\">'a'</span> <span class=\"token operator\">+</span> <span class=\"token string\">'a'</span> <span class=\"token comment\">// -> baNaNa</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p><strong>💡 What’s happening?</strong></p>\n<p>This one is an old trick, remixed. The expression is converted to <code class=\"language-text\">\"ba\" + (+\"a\") + \"a\"</code>, and since <code class=\"language-text\">\"a\"</code> is converted to a number, it becomes <code class=\"language-text\">NaN</code>.</p>\n<h2 id=\"lets-fail\" style=\"position:relative;\"><a href=\"#lets-fail\" aria-label=\"lets fail permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Let’s fail</h2>\n<p>You wouldn’t believe this in your wildest dreams, but:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"41237866546935930000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`;(![] + [])[+[]] +\r\n  (![] + [])[+!+[]] +\r\n  ([![]] + [][[]])[+!+[] + [+[]]] +\r\n  (![] + [])[!+[] + !+[]]\r\n// -> 'fail'`, `41237866546935930000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token punctuation\">;</span><span class=\"token punctuation\">(</span><span class=\"token operator\">!</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">[</span><span class=\"token operator\">+</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span>\r\n  <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">[</span><span class=\"token operator\">+</span><span class=\"token operator\">!</span><span class=\"token operator\">+</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span>\r\n  <span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token operator\">!</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">[</span><span class=\"token operator\">+</span><span class=\"token operator\">!</span><span class=\"token operator\">+</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> <span class=\"token punctuation\">[</span><span class=\"token operator\">+</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span>\r\n  <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">[</span><span class=\"token operator\">!</span><span class=\"token operator\">+</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> <span class=\"token operator\">!</span><span class=\"token operator\">+</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span>\r\n<span class=\"token comment\">// -> 'fail'</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p><strong>💡 What’s happening?</strong></p>\n<p>If we break this lot into smaller pieces, we notice that the following pattern occurs often:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"56982679795327740000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`![] + [] // -> 'false'\r\n![] // -> false`, `56982679795327740000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token operator\">!</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span> <span class=\"token comment\">// -> 'false'</span>\r\n<span class=\"token operator\">!</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span> <span class=\"token comment\">// -> false</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>We try adding <code class=\"language-text\">[]</code> to <code class=\"language-text\">false</code>, but because of a number of function calls internally, we’ll end up converting the right operand into a string:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"28428300503440896000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`![] + [].toString() // -> 'false'`, `28428300503440896000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token operator\">!</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">toString</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// -> 'false'</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>Thinking of a string as an array we can access its first character via <code class=\"language-text\">[0]</code>:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"7844135779709816000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`'false'[0] // -> 'f'`, `7844135779709816000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token string\">'false'</span><span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token comment\">// -> 'f'</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>The rest is obvious, but the <code class=\"language-text\">i</code> is tricky. The <code class=\"language-text\">i</code> in fail is grabbed by generating the string <code class=\"language-text\">falseundefined</code> and taking the element on index <code class=\"language-text\">['10']</code>.</p>\n<h2 id=\"array-equality-is-evil-\" style=\"position:relative;\"><a href=\"#array-equality-is-evil-\" aria-label=\"array equality is evil  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Array equality is evil 👾</h2>\n<p>Array equality is evil in JavaScript, see below:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"19194679611470830000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`[] == ''   // -> true\r\n[] == 0    // -> true\r\n[''] == '' // -> true\r\n[0] == 0   // -> true\r\n[0] == ''  // -> false\r\n[''] == 0  // -> true\r\n\r\n[null] == ''      // true\r\n[null] == 0       // true\r\n[undefined] == '' // true\r\n[undefined] == 0  // true\r\n\r\n[[]] == 0  // true\r\n[[]] == '' // true\r\n\r\n[[[[[[]]]]]] == '' // true\r\n[[[[[[]]]]]] == 0  // true\r\n\r\n[[[[[[ null ]]]]]] == 0  // true\r\n[[[[[[ null ]]]]]] == '' // true\r\n\r\n[[[[[[ undefined ]]]]]] == 0  // true\r\n[[[[[[ undefined ]]]]]] == '' // true`, `19194679611470830000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token string\">''</span>   <span class=\"token comment\">// -> true</span>\r\n<span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token number\">0</span>    <span class=\"token comment\">// -> true</span>\r\n<span class=\"token punctuation\">[</span><span class=\"token string\">''</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token string\">''</span> <span class=\"token comment\">// -> true</span>\r\n<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token number\">0</span>   <span class=\"token comment\">// -> true</span>\r\n<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token string\">''</span>  <span class=\"token comment\">// -> false</span>\r\n<span class=\"token punctuation\">[</span><span class=\"token string\">''</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token number\">0</span>  <span class=\"token comment\">// -> true</span>\r\n\r\n<span class=\"token punctuation\">[</span><span class=\"token keyword\">null</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token string\">''</span>      <span class=\"token comment\">// true</span>\r\n<span class=\"token punctuation\">[</span><span class=\"token keyword\">null</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token number\">0</span>       <span class=\"token comment\">// true</span>\r\n<span class=\"token punctuation\">[</span><span class=\"token keyword\">undefined</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token string\">''</span> <span class=\"token comment\">// true</span>\r\n<span class=\"token punctuation\">[</span><span class=\"token keyword\">undefined</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token number\">0</span>  <span class=\"token comment\">// true</span>\r\n\r\n<span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token number\">0</span>  <span class=\"token comment\">// true</span>\r\n<span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token string\">''</span> <span class=\"token comment\">// true</span>\r\n\r\n<span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token string\">''</span> <span class=\"token comment\">// true</span>\r\n<span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token number\">0</span>  <span class=\"token comment\">// true</span>\r\n\r\n<span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span> <span class=\"token keyword\">null</span> <span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token number\">0</span>  <span class=\"token comment\">// true</span>\r\n<span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span> <span class=\"token keyword\">null</span> <span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token string\">''</span> <span class=\"token comment\">// true</span>\r\n\r\n<span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span> <span class=\"token keyword\">undefined</span> <span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token number\">0</span>  <span class=\"token comment\">// true</span>\r\n<span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span> <span class=\"token keyword\">undefined</span> <span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token string\">''</span> <span class=\"token comment\">// true</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p><strong>💡 What’s happening?</strong></p>\n<p>The explanation behind this is rather long. So I introduce you to <a href=\"https://www.ecma-international.org/ecma-262/#sec-abstract-equality-comparison\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">section 7.2.13 Abstract Equality Comparison</a> of the specification.</p>\n<h2 id=\"code-classlanguage-textparseintcode-is-just-bad\" style=\"position:relative;\"><a href=\"#code-classlanguage-textparseintcode-is-just-bad\" aria-label=\"code classlanguage textparseintcode is just bad permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">parseInt</code> is just bad</h2>\n<p><code class=\"language-text\">parseInt</code> is famous by its quirks, I just mention one of the most famous ones:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"49823908262508620000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`parseInt('f**k') // -> NaN\r\nparseInt('f**k', 16) // -> 15`, `49823908262508620000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token function\">parseInt</span><span class=\"token punctuation\">(</span><span class=\"token string\">'f**k'</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// -> NaN</span>\r\n<span class=\"token function\">parseInt</span><span class=\"token punctuation\">(</span><span class=\"token string\">'f**k'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">16</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// -> 15</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p><strong>💡 What’s happening?</strong></p>\n<p>This happens because <code class=\"language-text\">parseInt</code> will continue parsing character by character until it hits one it doesn’t know. The <code class=\"language-text\">f</code> in <code class=\"language-text\">f**k</code> is the hexadecimal digit <code class=\"language-text\">15</code>.</p>\n<h2 id=\"code-classlanguage-textnancode-is-not-a-number\" style=\"position:relative;\"><a href=\"#code-classlanguage-textnancode-is-not-a-number\" aria-label=\"code classlanguage textnancode is not a number permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">NaN</code> is <del>not</del> a number</h2>\n<p>Type of <code class=\"language-text\">NaN</code> is a <code class=\"language-text\">number</code>:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"7309965607688862000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`typeof NaN // -> 'number'`, `7309965607688862000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">typeof</span> <span class=\"token number\">NaN</span> <span class=\"token comment\">// -> 'number'</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p><strong>💡 What’s happening?</strong></p>\n<p>Explanations of how <code class=\"language-text\">typeof</code> and <code class=\"language-text\">instanceof</code> operators work:</p>\n<ul>\n<li><a href=\"https://www.ecma-international.org/ecma-262/#sec-typeof-operator\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">12.5.5 The <code class=\"language-text\">typeof</code> Operator</a></li>\n<li><a href=\"https://www.ecma-international.org/ecma-262/#sec-instanceofoperator\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">12.10.4 Runtime Semantics: <code class=\"language-text\">InstanceofOperator(O,C)</code></a></li>\n</ul>\n<h2 id=\"comparison-of-three-numbers\" style=\"position:relative;\"><a href=\"#comparison-of-three-numbers\" aria-label=\"comparison of three numbers permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Comparison of three numbers</h2>\n<p>This one is gold:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"31010152508831945000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`1 < 2 < 3 // -> true\r\n3 > 2 > 1 // -> false`, `31010152508831945000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token number\">1</span> <span class=\"token operator\">&lt;</span> <span class=\"token number\">2</span> <span class=\"token operator\">&lt;</span> <span class=\"token number\">3</span> <span class=\"token comment\">// -> true</span>\r\n<span class=\"token number\">3</span> <span class=\"token operator\">></span> <span class=\"token number\">2</span> <span class=\"token operator\">></span> <span class=\"token number\">1</span> <span class=\"token comment\">// -> false</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p><strong>💡 What’s happening?</strong></p>\n<p>Why does this work that way? Well, the problem is in the first part of an expression. Here’s how it works:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"32131005059129400000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`1 < 2 < 3 // 1 < 2 -> true\r\ntrue < 3 // true -> 1\r\n1 < 3 // -> true\r\n\r\n3 > 2 > 1 // 3 > 2 -> true\r\ntrue > 1 // true -> 1\r\n1 > 1 // -> false`, `32131005059129400000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token number\">1</span> <span class=\"token operator\">&lt;</span> <span class=\"token number\">2</span> <span class=\"token operator\">&lt;</span> <span class=\"token number\">3</span> <span class=\"token comment\">// 1 &lt; 2 -> true</span>\r\n<span class=\"token boolean\">true</span> <span class=\"token operator\">&lt;</span> <span class=\"token number\">3</span> <span class=\"token comment\">// true -> 1</span>\r\n<span class=\"token number\">1</span> <span class=\"token operator\">&lt;</span> <span class=\"token number\">3</span> <span class=\"token comment\">// -> true</span>\r\n\r\n<span class=\"token number\">3</span> <span class=\"token operator\">></span> <span class=\"token number\">2</span> <span class=\"token operator\">></span> <span class=\"token number\">1</span> <span class=\"token comment\">// 3 > 2 -> true</span>\r\n<span class=\"token boolean\">true</span> <span class=\"token operator\">></span> <span class=\"token number\">1</span> <span class=\"token comment\">// true -> 1</span>\r\n<span class=\"token number\">1</span> <span class=\"token operator\">></span> <span class=\"token number\">1</span> <span class=\"token comment\">// -> false</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>You can fix this with greater than or equal operator (<code class=\"language-text\">>=</code>):</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"79744372564706700000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`3 > 2 >= 1 // true`, `79744372564706700000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token number\">3</span> <span class=\"token operator\">></span> <span class=\"token number\">2</span> <span class=\"token operator\">>=</span> <span class=\"token number\">1</span> <span class=\"token comment\">// true</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>Read more about Relational operators in the specification:</p>\n<ul>\n<li><a href=\"https://www.ecma-international.org/ecma-262/#sec-relational-operators\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">12.10 Relational Operators</a></li>\n</ul>\n<p>And that’s just some of the fun we can have with JavaScript. No wonder some people do not like it, since they don’t understand how it works 😂.</p>","timeToRead":3,"frontmatter":{"title":"JavaScript can do that?","unformattedDate":"2019-06-26T00:00:00.000Z","date":"Jun 26, 2019","path":"/js-can-do-that/","author":"Yaser Adel Mehraban","tags":["webdev","javascript","showdev"],"thumbnail":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAUBAwT/xAAWAQEBAQAAAAAAAAAAAAAAAAABAAL/2gAMAwEAAhADEAAAAVTLBfmkWg//xAAaEAEBAAIDAAAAAAAAAAAAAAACAQADEiIx/9oACAEBAAEFAjsowuUQ6rmpdE7xvv8A/8QAFREBAQAAAAAAAAAAAAAAAAAAABH/2gAIAQMBAT8BV//EABYRAQEBAAAAAAAAAAAAAAAAAAABEv/aAAgBAgEBPwGxl//EAB4QAAEDBAMAAAAAAAAAAAAAAAABERICITGRQWFx/9oACAEBAAY/AoozeErEqmdexcaONCn/xAAaEAEAAwEBAQAAAAAAAAAAAAABABEhMVFx/9oACAEBAAE/ISup6GWw3M0lyjW84iH2dDBqqb5m29Z//9oADAMBAAIAAwAAABBc7//EABcRAAMBAAAAAAAAAAAAAAAAAAABEXH/2gAIAQMBAT8QTjpg/8QAGBEAAgMAAAAAAAAAAAAAAAAAAAERMVH/2gAIAQIBAT8QY1ZLT//EABsQAQEAAwEBAQAAAAAAAAAAAAERACExQVGx/9oACAEBAAE/EHiCqiGydc66dRCwvDBW4sAK8wtBV2Q59TDiLaAn5juSosJ7n//Z"},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/b42bb6525b368ec041da166081559181/ec5b0/hdtew.jpg","srcSet":"/static/b42bb6525b368ec041da166081559181/ec5b0/hdtew.jpg 500w","sizes":"(min-width: 500px) 500px, 100vw"},"sources":[{"srcSet":"/static/b42bb6525b368ec041da166081559181/8e0eb/hdtew.webp 500w","type":"image/webp","sizes":"(min-width: 500px) 500px, 100vw"}]},"width":500,"height":283}}}}}},{"node":{"id":"228f1e50-083c-5323-9dcc-88f0f0b9f962","html":"<p>Most of the times, we write code which is being copy pasted from all over internet. <a href=\"https://stackoverflow.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">StackOverflow</a> is the main source these days for finding solutions to all sort of problems. But is it OK to blindly copy paste code without really knowing what’s happening behind the scenes?</p>\n<!--more-->\n<h1 id=\"a-bit-of-context\" style=\"position:relative;\"><a href=\"#a-bit-of-context\" aria-label=\"a bit of context permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>A bit of context</h1>\n<p>Don’t get me wrong when I say <strong>StackOverflow</strong> should not be used blindly. It’s a great source of information for most of the day to day issues and bugs developers face all around the world. It’s just that we should be a bit more proactive and decide the best way out of all the available options out there.</p>\n<p>Let me show you some examples where a piece of code can be written in multiple ways, and the most obvious choice is not necessarily the best one.</p>\n<h2 id=\"chaining-array-loop-chaining\" style=\"position:relative;\"><a href=\"#chaining-array-loop-chaining\" aria-label=\"chaining array loop chaining permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Chaining array loop chaining</h2>\n<p>Let’s assume we have an array with <code class=\"language-text\">200,000</code> objects which include name and age properties. We want to have the name of all people under age of 22 (assume we have <code class=\"language-text\">100,000</code> of them). This the solution most people might use:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"30833100955319103000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const under22People = originalList\r\n  .filter(x => x.age < 22)\r\n  .map(x => x.name)`, `30833100955319103000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> under22People <span class=\"token operator\">=</span> originalList\r\n  <span class=\"token punctuation\">.</span><span class=\"token function\">filter</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">x</span> <span class=\"token operator\">=></span> x<span class=\"token punctuation\">.</span>age <span class=\"token operator\">&lt;</span> <span class=\"token number\">22</span><span class=\"token punctuation\">)</span>\r\n  <span class=\"token punctuation\">.</span><span class=\"token function\">map</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">x</span> <span class=\"token operator\">=></span> x<span class=\"token punctuation\">.</span>name<span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>Since <code class=\"language-text\">ES5</code> and <code class=\"language-text\">ES6</code> were introduced, a nice set of array methods emerged. <code class=\"language-text\">ES6s</code> cleaner syntax makes it painless to chain methods in order to produce our desired results. The problem lies in how we use these nice methods without realising how they’re performed.</p>\n<p>In the first glance, this looks to be a really nice code, but if we look closer, we have a loop which runs <code class=\"language-text\">200,000</code> times when doing the filtering, and another <code class=\"language-text\">100,000</code> times when selecting the name.</p>\n<p>This is where we just need to loop through these items once. So let’s have a look at how can we rewrite this code:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"46772345653027635000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const under22People = []\r\noriginalList.forEach(({ age, name }) => {\r\n  age >= 18 && under22People.push(name)\r\n})`, `46772345653027635000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> under22People <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span>\r\noriginalList<span class=\"token punctuation\">.</span><span class=\"token function\">forEach</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\"><span class=\"token punctuation\">{</span> age<span class=\"token punctuation\">,</span> name <span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n  age <span class=\"token operator\">>=</span> <span class=\"token number\">18</span> <span class=\"token operator\">&amp;&amp;</span> under22People<span class=\"token punctuation\">.</span><span class=\"token function\">push</span><span class=\"token punctuation\">(</span>name<span class=\"token punctuation\">)</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Now this code runs only <code class=\"language-text\">200,000</code> times. It doesn’t look as nice as the chained methods, but it sure has much better performance.</p>\n<p>You can even use the <code class=\"language-text\">reduce</code> method to do the same:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"41948536885506770000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const under22People = originalList.reduce(\r\n  (acc, { age, name }) => {\r\n    return age >= 18 ? [...ac, name] : acc\r\n  },\r\n  []\r\n)`, `41948536885506770000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> under22People <span class=\"token operator\">=</span> originalList<span class=\"token punctuation\">.</span><span class=\"token function\">reduce</span><span class=\"token punctuation\">(</span>\r\n  <span class=\"token punctuation\">(</span><span class=\"token parameter\">acc<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span> age<span class=\"token punctuation\">,</span> name <span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">return</span> age <span class=\"token operator\">>=</span> <span class=\"token number\">18</span> <span class=\"token operator\">?</span> <span class=\"token punctuation\">[</span><span class=\"token operator\">...</span>ac<span class=\"token punctuation\">,</span> name<span class=\"token punctuation\">]</span> <span class=\"token operator\">:</span> acc\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span>\r\n<span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>It sure looks much less readable, but it does the exact same job for us.</p>\n<h2 id=\"abusing-arrow-functions\" style=\"position:relative;\"><a href=\"#abusing-arrow-functions\" aria-label=\"abusing arrow functions permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Abusing arrow functions</h2>\n<p>One of the good questions you could ask yourself when writing <code class=\"language-text\">JavaScript</code> is whether you know the difference between a traditional function and an arrow function (aka fat function).</p>\n<p>From MDN Web Docs:</p>\n<blockquote>\n<p>An arrow function expression is a syntactically compact alternative to a <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">regular function expression</a>, although without its own bindings to the <code class=\"language-text\">this</code>, <code class=\"language-text\">arguments</code>, <code class=\"language-text\">super</code>, or <code class=\"language-text\">new.target</code> keywords. Arrow function expressions are ill suited as methods, and they cannot be used as constructors.</p>\n</blockquote>\n<p>Before you ask, no they are not JavaScript equivalent of anonymous functions in languages like <code class=\"language-text\">C#</code>. But for now the only thing you need to care about is that they don’t have scope.</p>\n<p>Once of the many of their use cases is using them in class field. Consider in <code class=\"language-text\">React</code> you don’t need to manually bind your functions anymore like:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"53621859364338700000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`this.handleClick = this.handleClick.bind(this)`, `53621859364338700000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>handleClick <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">handleClick</span><span class=\"token punctuation\">.</span><span class=\"token function\">bind</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>Instead you will write your class like:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"54545765068467070000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`class MyComponent extends Component {\r\n  handleClick = () => {\r\n    // ...\r\n  }\r\n\r\n  render() {\r\n    // ...\r\n  }\r\n}`, `54545765068467070000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">class</span> <span class=\"token class-name\">MyComponent</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">Component</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token function-variable function\">handleClick</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token comment\">// ...</span>\r\n  <span class=\"token punctuation\">}</span>\r\n\r\n  <span class=\"token function\">render</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token comment\">// ...</span>\r\n  <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>As you might know, usual functions are defined in the prototype and will be shared across all instances. If we have a list of <code class=\"language-text\">N</code> components, these components will share the same method. So, if our components get clicked we still call our method <code class=\"language-text\">N</code> times, but it will call the same prototype. As we’re calling the same method multiple times across the prototype, the <code class=\"language-text\">JavaScript</code> engine can optimize it.</p>\n<p>On the other hand, for the arrow functions in class properties, if we’re creating <code class=\"language-text\">N</code> components, these <code class=\"language-text\">N</code> components will also create <code class=\"language-text\">N</code> functions. You can see by looking at the transpiled version, that class properties are initialised in the constructor. Which means if we click on <code class=\"language-text\">N</code> components, <code class=\"language-text\">N</code> different functions will be called.</p>\n<p>So consider this approach next time you are writing your new shiny <code class=\"language-text\">React</code> component.</p>\n<h2 id=\"nested-functions-vs-iifes\" style=\"position:relative;\"><a href=\"#nested-functions-vs-iifes\" aria-label=\"nested functions vs iifes permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Nested functions VS IIFEs</h2>\n<p>Nesting a function inside another function seems like a good idea to isolate some logic from outside world. Consider below code:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"84719191213493940000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function doSomething(arg1, arg2) {\r\n  function nestedHelper(arg) {\r\n    return process(arg)\r\n  }\r\n\r\n  return nestedHelper(arg1) + nestedHelper(arg2)\r\n}`, `84719191213493940000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">doSomething</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">arg1<span class=\"token punctuation\">,</span> arg2</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">function</span> <span class=\"token function\">nestedHelper</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">arg</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">return</span> <span class=\"token function\">process</span><span class=\"token punctuation\">(</span>arg<span class=\"token punctuation\">)</span>\r\n  <span class=\"token punctuation\">}</span>\r\n\r\n  <span class=\"token keyword\">return</span> <span class=\"token function\">nestedHelper</span><span class=\"token punctuation\">(</span>arg1<span class=\"token punctuation\">)</span> <span class=\"token operator\">+</span> <span class=\"token function\">nestedHelper</span><span class=\"token punctuation\">(</span>arg2<span class=\"token punctuation\">)</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>The problem with above code is that every time you call <code class=\"language-text\">doSomething</code>, the <code class=\"language-text\">nestedHeler</code> is recreated. In order to prevent that you can use an <code class=\"language-text\">IIFE</code> (Immediately invoked function):</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"52245996472864915000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const result = (function() {\r\n  function privateHelper(arg) {\r\n    var result = process(arg)\r\n    return result\r\n  }\r\n\r\n  return function(arg1, arg2) {\r\n    return (\r\n      privateHelper(arg1) + privateHelper(arg2)\r\n    )\r\n  }\r\n})()`, `52245996472864915000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> result <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">function</span> <span class=\"token function\">privateHelper</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">arg</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">var</span> result <span class=\"token operator\">=</span> <span class=\"token function\">process</span><span class=\"token punctuation\">(</span>arg<span class=\"token punctuation\">)</span>\r\n    <span class=\"token keyword\">return</span> result\r\n  <span class=\"token punctuation\">}</span>\r\n\r\n  <span class=\"token keyword\">return</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">arg1<span class=\"token punctuation\">,</span> arg2</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\r\n      <span class=\"token function\">privateHelper</span><span class=\"token punctuation\">(</span>arg1<span class=\"token punctuation\">)</span> <span class=\"token operator\">+</span> <span class=\"token function\">privateHelper</span><span class=\"token punctuation\">(</span>arg2<span class=\"token punctuation\">)</span>\r\n    <span class=\"token punctuation\">)</span>\r\n  <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>When this code gets executed, the nested method will be created only once 🤷‍♂️.</p>\n<h1 id=\"use-sets-over-arrays-where-possible\" style=\"position:relative;\"><a href=\"#use-sets-over-arrays-where-possible\" aria-label=\"use sets over arrays where possible permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use Sets over Arrays where possible</h1>\n<p>The most obvious difference between an <code class=\"language-text\">array</code> and a <code class=\"language-text\">Set</code> is that <code class=\"language-text\">array</code> is an <em>indexed</em> collection, whereas, a <code class=\"language-text\">Set</code> is key based.</p>\n<p>So why you should be using a <code class=\"language-text\">Set</code>?</p>\n<h3 id=\"searching-for-an-item\" style=\"position:relative;\"><a href=\"#searching-for-an-item\" aria-label=\"searching for an item permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Searching for an Item</h3>\n<p>Using <code class=\"language-text\">indexOf()</code> or <code class=\"language-text\">includes()</code> to check whether an item exists in an array is slow. In a <code class=\"language-text\">Set</code> you can find an item really easy using <code class=\"language-text\">has()</code>:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"22751717612982493000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const mySet = new Set([1, 1, 2])\r\n\r\nconsole.log(mySet.has(2)) // true`, `22751717612982493000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> mySet <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Set</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>mySet<span class=\"token punctuation\">.</span><span class=\"token function\">has</span><span class=\"token punctuation\">(</span><span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// true</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"deleting-an-item\" style=\"position:relative;\"><a href=\"#deleting-an-item\" aria-label=\"deleting an item permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Deleting an Item</h3>\n<p>In a <code class=\"language-text\">Set</code>, you can delete an item by its value. In an array, the equivalent is using <code class=\"language-text\">splice()</code> based on an element’s <code class=\"language-text\">index</code>. As in the previous point, depending on indices is slow.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"76196783621243700000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const mySet = new Set([1, 2, 3, 4, 5])\r\n\r\nmySet.delete(1)`, `76196783621243700000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> mySet <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Set</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token number\">4</span><span class=\"token punctuation\">,</span> <span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\r\n\r\nmySet<span class=\"token punctuation\">.</span><span class=\"token function\">delete</span><span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"insert-an-item\" style=\"position:relative;\"><a href=\"#insert-an-item\" aria-label=\"insert an item permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Insert an Item</h3>\n<p>It is much faster to add an item to a <code class=\"language-text\">Set</code> than to an array using <code class=\"language-text\">push()</code> or <code class=\"language-text\">unshift()</code>.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"59078805804048980000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const mySet = new Set([1, 2])\r\n\r\nmySet.add(3) // Successfully added`, `59078805804048980000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> mySet <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Set</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\r\n\r\nmySet<span class=\"token punctuation\">.</span><span class=\"token function\">add</span><span class=\"token punctuation\">(</span><span class=\"token number\">3</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// Successfully added</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"storing-nan\" style=\"position:relative;\"><a href=\"#storing-nan\" aria-label=\"storing nan permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Storing NaN</h3>\n<p>You cannot use <code class=\"language-text\">indexOf()</code> or <code class=\"language-text\">includes()</code> to find the value <code class=\"language-text\">NaN</code>, while a <code class=\"language-text\">Set</code> is able to store this value.</p>\n<h3 id=\"removing-duplicates\" style=\"position:relative;\"><a href=\"#removing-duplicates\" aria-label=\"removing duplicates permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Removing Duplicates</h3>\n<p><code class=\"language-text\">Set</code> objects only store unique values. If you want to avoid storing duplicates, this is a significant advantage over arrays, where additional code would be required to deal with duplicates.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"74226843293734930000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const mySet = new Set([1, 1, 2])\r\n\r\nmySet.add(3) // Successfully added\r\n\r\nconsole.log(mySet.values()) // 1,2,3`, `74226843293734930000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> mySet <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Set</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\r\n\r\nmySet<span class=\"token punctuation\">.</span><span class=\"token function\">add</span><span class=\"token punctuation\">(</span><span class=\"token number\">3</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// Successfully added</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>mySet<span class=\"token punctuation\">.</span><span class=\"token function\">values</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// 1,2,3</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h1 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h1>\n<p>There are many of these examples where you might want to be careful when writing code in a real world business application. Speed is one of the most important part of every web application and considering some items like above points, will increase your code’s performance which can result in happier users 😊.</p>\n<p>Hope this has helped you to start thinking about what other ways there are to solve the same issue rather than the first solution you’ll find on the web.</p>","timeToRead":4,"frontmatter":{"title":"Write faster JavaScript","unformattedDate":"2019-05-20T00:00:00.000Z","date":"May 20, 2019","path":"/write-faster-js/","author":"Yaser Adel Mehraban","tags":["webdev","showdev","javascript","performance"],"thumbnail":null}}},{"node":{"id":"828f2a5f-984a-5373-ad25-d7c88a965a66","html":"<p>There are multiple ways to remove duplicates from an array with JavaScript. But, it’s very important to know the details of how efficient they, especially if you’re dealing with large arrays.</p>\n<!--more-->\n<h2 id=\"simplest-approach\" style=\"position:relative;\"><a href=\"#simplest-approach\" aria-label=\"simplest approach permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Simplest approach</h2>\n<p>The easiest way is to use ES6’s (ECMAScript 2015) <code class=\"language-text\">Set</code> object. This let’s you store unique values of any type. It will automatically remove duplicates for us, isn’t that sweet?</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"43517594840932710000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const fruit = [\r\n  'apple',\r\n  'orange',\r\n  'avo',\r\n  'pear',\r\n  'cherries',\r\n  'strawberries',\r\n  'avo',\r\n  'avo',\r\n];\r\n\r\nlet uniqueFruit = [...new Set(fruit)];\r\n\r\nconsole.log(uniqueFruit); // ['apple', 'orange', 'avo', 'pear', 'cherries', 'strawberries']`, `43517594840932710000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> fruit <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span>\r\n  <span class=\"token string\">'apple'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token string\">'orange'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token string\">'avo'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token string\">'pear'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token string\">'cherries'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token string\">'strawberries'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token string\">'avo'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token string\">'avo'</span><span class=\"token punctuation\">,</span>\r\n<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">let</span> uniqueFruit <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token operator\">...</span><span class=\"token keyword\">new</span> <span class=\"token class-name\">Set</span><span class=\"token punctuation\">(</span>fruit<span class=\"token punctuation\">)</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>uniqueFruit<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// ['apple', 'orange', 'avo', 'pear', 'cherries', 'strawberries']</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Here we had to use the <code class=\"language-text\">spread</code> operator to get an array from <code class=\"language-text\">Set</code> again.</p>\n<h2 id=\"smart-but-naïve-approach\" style=\"position:relative;\"><a href=\"#smart-but-na%C3%AFve-approach\" aria-label=\"smart but naïve approach permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Smart but naïve approach</h2>\n<p>We can use the built in filter method of ES5 to achieve the same:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"97517600949632610000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const fruits = [\r\n  'apple',\r\n  'orange',\r\n  'avo',\r\n  'pear',\r\n  'cherries',\r\n  'strawberries',\r\n  'avo',\r\n  'avo',\r\n];\r\n\r\nconst uniqueFruit = fruits.filter(\r\n  (item, i, arr) => {\r\n    return arr.indexOf(item) == i;\r\n  }\r\n);\r\n\r\nconsole.log(uniqueFruit); // ['apple', 'orange', 'avo', 'pear', 'cherries', 'strawberries']`, `97517600949632610000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> fruits <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span>\r\n  <span class=\"token string\">'apple'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token string\">'orange'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token string\">'avo'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token string\">'pear'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token string\">'cherries'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token string\">'strawberries'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token string\">'avo'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token string\">'avo'</span><span class=\"token punctuation\">,</span>\r\n<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token keyword\">const</span> uniqueFruit <span class=\"token operator\">=</span> fruits<span class=\"token punctuation\">.</span><span class=\"token function\">filter</span><span class=\"token punctuation\">(</span>\r\n  <span class=\"token punctuation\">(</span><span class=\"token parameter\">item<span class=\"token punctuation\">,</span> i<span class=\"token punctuation\">,</span> arr</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">return</span> arr<span class=\"token punctuation\">.</span><span class=\"token function\">indexOf</span><span class=\"token punctuation\">(</span>item<span class=\"token punctuation\">)</span> <span class=\"token operator\">==</span> i<span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>uniqueFruit<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// ['apple', 'orange', 'avo', 'pear', 'cherries', 'strawberries']</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>What we’re doing here is not that complex. We’re going through each element and check if the first position of this item in the array is equal to current position. Since these two positions are different for duplicate items, they will get filtered and we end up with a unique array 🤩.</p>\n<div class=\"custom-block warning\"><div class=\"custom-block-body\"> <strong>Warning:</strong> This approach is not that efficient for large arrays (quadratic time).</div></div>\n<h2 id=\"hash-table-to-rescue\" style=\"position:relative;\"><a href=\"#hash-table-to-rescue\" aria-label=\"hash table to rescue permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Hash table to rescue</h2>\n<p>This approach is to place each item in a <code class=\"language-text\">hashtable</code> and then check for it’s presence. This gives a linear time, but has at least two pitfalls:</p>\n<ul>\n<li>Since hash keys can only be string, this code doesn’t distinguish number and numeric strings. This means <code class=\"language-text\">['1', 1]</code> will return <code class=\"language-text\">[1]</code>.</li>\n<li>Because of ☝🏼, all objects will be equal 🤦‍♂️.</li>\n</ul>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"39579426663133300000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function unique(fruits) {\r\n  var seen = {};\r\n  return fruits.filter(item => {\r\n    return seen.hasOwnProperty(item)\r\n      ? false\r\n      : (seen[item] = true);\r\n  });\r\n}`, `39579426663133300000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">unique</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">fruits</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">var</span> seen <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">return</span> fruits<span class=\"token punctuation\">.</span><span class=\"token function\">filter</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">item</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">return</span> seen<span class=\"token punctuation\">.</span><span class=\"token function\">hasOwnProperty</span><span class=\"token punctuation\">(</span>item<span class=\"token punctuation\">)</span>\r\n      <span class=\"token operator\">?</span> <span class=\"token boolean\">false</span>\r\n      <span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span>seen<span class=\"token punctuation\">[</span>item<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"the-hybrid-and-best-approach\" style=\"position:relative;\"><a href=\"#the-hybrid-and-best-approach\" aria-label=\"the hybrid and best approach permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The hybrid and best approach</h2>\n<p>A better approach combines both approaches. It uses hash lookups for primitive types and linear search for objects:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"21356745546644860000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function unique(fruits) {\r\n  var prims = {\r\n      boolean: {},\r\n      number: {},\r\n      string: {},\r\n    },\r\n    objs = [];\r\n\r\n  return fruits.filter(item => {\r\n    var type = typeof item;\r\n    if (type in prims)\r\n      return prims[type].hasOwnProperty(item)\r\n        ? false\r\n        : (prims[type][item] = true);\r\n    else\r\n      return objs.indexOf(item) >= 0\r\n        ? false\r\n        : objs.push(item);\r\n  });\r\n}`, `21356745546644860000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">unique</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">fruits</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">var</span> prims <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\r\n      <span class=\"token literal-property property\">boolean</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n      <span class=\"token literal-property property\">number</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n      <span class=\"token literal-property property\">string</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n    objs <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n\r\n  <span class=\"token keyword\">return</span> fruits<span class=\"token punctuation\">.</span><span class=\"token function\">filter</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">item</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">var</span> type <span class=\"token operator\">=</span> <span class=\"token keyword\">typeof</span> item<span class=\"token punctuation\">;</span>\r\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>type <span class=\"token keyword\">in</span> prims<span class=\"token punctuation\">)</span>\r\n      <span class=\"token keyword\">return</span> prims<span class=\"token punctuation\">[</span>type<span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">hasOwnProperty</span><span class=\"token punctuation\">(</span>item<span class=\"token punctuation\">)</span>\r\n        <span class=\"token operator\">?</span> <span class=\"token boolean\">false</span>\r\n        <span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span>prims<span class=\"token punctuation\">[</span>type<span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span>item<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token keyword\">else</span>\r\n      <span class=\"token keyword\">return</span> objs<span class=\"token punctuation\">.</span><span class=\"token function\">indexOf</span><span class=\"token punctuation\">(</span>item<span class=\"token punctuation\">)</span> <span class=\"token operator\">>=</span> <span class=\"token number\">0</span>\r\n        <span class=\"token operator\">?</span> <span class=\"token boolean\">false</span>\r\n        <span class=\"token operator\">:</span> objs<span class=\"token punctuation\">.</span><span class=\"token function\">push</span><span class=\"token punctuation\">(</span>item<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"sort--unique\" style=\"position:relative;\"><a href=\"#sort--unique\" aria-label=\"sort  unique permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Sort &#x26; unique</h2>\n<p>Another option is to sort the array and then remove the items equal to the one right after:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"18463533964488280000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`function unique(fruits) {\r\n  return fruits\r\n    .sort()\r\n    .filter((item, pos, ary) => {\r\n      return !pos || item != ary[pos - 1];\r\n    });\r\n}`, `18463533964488280000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">unique</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">fruits</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">return</span> fruits\r\n    <span class=\"token punctuation\">.</span><span class=\"token function\">sort</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\r\n    <span class=\"token punctuation\">.</span><span class=\"token function\">filter</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">item<span class=\"token punctuation\">,</span> pos<span class=\"token punctuation\">,</span> ary</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n      <span class=\"token keyword\">return</span> <span class=\"token operator\">!</span>pos <span class=\"token operator\">||</span> item <span class=\"token operator\">!=</span> ary<span class=\"token punctuation\">[</span>pos <span class=\"token operator\">-</span> <span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>This item is similar to hash table approach, it does not work with objects. And more importantly we have to change the original array which is a side effect and not a good way to go about this.</p>\n<h2 id=\"using-reduce\" style=\"position:relative;\"><a href=\"#using-reduce\" aria-label=\"using reduce permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Using reduce</h2>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"18199625215376058000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const uniqueFruits = fruits.reduce((a, b) => {\r\n  if (a.indexOf(b) < 0) a.push(b);\r\n  return a;\r\n}, []);\r\n\r\nconsole.log(uniqueFruits); // ['apple', 'orange', 'avo', 'pear', 'cherries', 'strawberries']`, `18199625215376058000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> uniqueFruits <span class=\"token operator\">=</span> fruits<span class=\"token punctuation\">.</span><span class=\"token function\">reduce</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">a<span class=\"token punctuation\">,</span> b</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>a<span class=\"token punctuation\">.</span><span class=\"token function\">indexOf</span><span class=\"token punctuation\">(</span>b<span class=\"token punctuation\">)</span> <span class=\"token operator\">&lt;</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> a<span class=\"token punctuation\">.</span><span class=\"token function\">push</span><span class=\"token punctuation\">(</span>b<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">return</span> a<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>uniqueFruits<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// ['apple', 'orange', 'avo', 'pear', 'cherries', 'strawberries']</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>This approach uses an accumulator to see whether the item is previously inserted into it or not. Very similar to filter approach we introduced earlier, a bit faster though.</p>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>These are just a couple of ways of doing the same operation. But as you can see, very different in nature and support in browsers. Pick carefully and happy coding.</p>","timeToRead":2,"frontmatter":{"title":"Removing duplicates from arrays in JavaScript","unformattedDate":"2019-04-24T00:00:00.000Z","date":"Apr 24, 2019","path":"/dedupe-array-js/","author":"Yaser Adel Mehraban","tags":["javascript","arrays","deduplication"],"thumbnail":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAARlAAAEZQAGA43XUAAAB80lEQVR42lVSy27TQBT1R7DgK/gOXhJSKgQSsEF8QMUKIXY8/oBXNiCEWBSJFiJo5AIqCkqbhDZNnbRpClFCHBzbGXvs2J57k9jmjlMWSFfWHc85d845M8qIQ+lwqjNwg//Ki9ANYydAPwIexW5IS1iU7cGA4Yij0rfxsTrrGBgCOBmNh5L5xwn6xpD5oeFizzCZTz9nbiiI7IXAJmB6qFgeUBHBcGVDU4csMnhaqh9sqEvb+wdrxWcrhbPFb/dM5vBo6gbinzRQSAB1EwFfNMhvwFMV8mq0dZxo7Ya2s7Smvvu+efHVp/zrwoNfg5YvEkkO5WGWDwpJl04CsH2o/YT1uni7hUej9GtN+7CeK5YrK4XbL1evq+U3zJMwsuZMgILoWqhQR2Pkl5SEIsLpx6q12+7ffaGduXWp1qy/L1VX1Set2rmjboVH6dgTtoeLKYqeyeaZkrEfzeJ0+fn+6dz5U5evXLhzrVJv9Bs3Rp2bvWauq2sBpDyIskRlQMpgjGP/5A4caV50rfly/vPVRw+rzarppXutclO7v3O4qbO5yU9gIcJeD5XfY2SZ8kWGThbjPEniJI2m8ZBBoxfvdtO2nvw4xp6ZCZ5ITLmD8mS6SYsj89H2SQWSpREXhiuGjIp2BdmxPUEPxplIAOHdAMud6V+UjQ7BNofhbQAAAABJRU5ErkJggg=="},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/df323d66b262256ebc674b09c057cc8a/cadb4/javascript.png","srcSet":"/static/df323d66b262256ebc674b09c057cc8a/cadb4/javascript.png 750w","sizes":"(min-width: 750px) 750px, 100vw"},"sources":[{"srcSet":"/static/df323d66b262256ebc674b09c057cc8a/11ba1/javascript.webp 750w","type":"image/webp","sizes":"(min-width: 750px) 750px, 100vw"}]},"width":750,"height":375}}}}}},{"node":{"id":"0ffa1c82-fc0d-50b9-a88b-612616b90e83","html":"<p>Recently I had a chance to present a talk at <a href=\"https://ndcsydney.com/talk/need-for-speed-8-performance-tuning-of-your-web-application/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">NDC Sydney</a> about web performance and it received a great feedback.</p>\n<!--more-->\n<p>That inspired me to write up a series of posts on each topic I covered in that talk, and who knows, maybe each of these posts would be a talk some day by their own 😃.</p>\n<p>All other parts:</p>\n<p><a href=\"/blog/2018/09/29/web-perf-1\">Part 1 on HTML and CSS</a></p>\n<p><a href=\"/blog/2018/10/06/web-perf-2\">Part 2 use Preload/Prefetch to boost load time</a></p>\n<p><a href=\"/blog/2018/11/12/web-perf-4\">Part 4 Image optimisation</a></p>\n<p><a href=\"/blog/2018/11/23/web-perf-5\">Part 5 Web font optimisation</a></p>\n<p>Time to see what we can do for our old friend JavaScript. So let’s begin.</p>\n<h1 id=\"switch-to-http2\" style=\"position:relative;\"><a href=\"#switch-to-http2\" aria-label=\"switch to http2 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Switch to HTTP/2</h1>\n<p>With more and more hosting providers supporting <a href=\"https://en.wikipedia.org/wiki/HTTP/2\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">HTTP/2</a>, it’s becoming a good time to switch to this protocol instead and benefit from its multiplexed nature. What it means in terms of performance is that we don’t need to bundle all of our JavaScript into large bundles to reduce the number of calls to server.</p>\n<p>With <strong>HTTP/2</strong> designed to handle large number of requests, you can now increase the number of files required to render the page. Not too much tho:</p>\n<blockquote>\n<p>Too much of a good thing is a bad thing.</p>\n</blockquote>\n<h1 id=\"async--defer\" style=\"position:relative;\"><a href=\"#async--defer\" aria-label=\"async  defer permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Async &#x26; Defer</h1>\n<p>As I mentioned before, JavaScript, like CSS is a <strong>render blocking</strong> element. This simply means the browser needs to wait for it to load and execute before it can parse the rest of the <code class=\"language-text\">HTML</code> document.</p>\n<p>This hugely increases our <a href=\"https://developers.google.com/web/tools/lighthouse/audits/first-contentful-paint\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">First Meaningful Pain</a>. In order to fix this issue we can use two of the features which are not used by many people but are very effective.</p>\n<h3 id=\"normal-execution\" style=\"position:relative;\"><a href=\"#normal-execution\" aria-label=\"normal execution permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Normal execution</h3>\n<p>When you use a <code class=\"language-text\">&lt;script></code> to load a JavaScript file, it interrupts the parsing of the document. Browser fetches the resource, executes this and then continues on paring:</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1080px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/d562503683c7b6a3c0ee5ed90085f60c/78c62/normaljsexec.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 20.740740740740744%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAEABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAIF/8QAFgEBAQEAAAAAAAAAAAAAAAAAAwEC/9oADAMBAAIQAxAAAAHVsQ7Gb//EABkQAAIDAQAAAAAAAAAAAAAAAAERAAISMf/aAAgBAQABBQLRaIlef//EABcRAAMBAAAAAAAAAAAAAAAAAAACMQH/2gAIAQMBAT8BTaLD/8QAFhEBAQEAAAAAAAAAAAAAAAAAAQAx/9oACAECAQE/AUnb/8QAGBAAAgMAAAAAAAAAAAAAAAAAAAEQMTL/2gAIAQEABj8Cs04//8QAGBAAAwEBAAAAAAAAAAAAAAAAAAERIXH/2gAIAQEAAT8hdashevR29On/2gAMAwEAAgADAAAAEPvf/8QAFhEBAQEAAAAAAAAAAAAAAAAAAQAR/9oACAEDAQE/EETTJRf/xAAWEQADAAAAAAAAAAAAAAAAAAAAASH/2gAIAQIBAT8QRBaP/8QAGRABAQADAQAAAAAAAAAAAAAAAREAIaEx/9oACAEBAAE/EJcALNGuZYGpShnMfrq+oZ//2Q=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Normal execution\"\n        title=\"\"\n        src=\"/static/d562503683c7b6a3c0ee5ed90085f60c/47311/normaljsexec.jpg\"\n        srcset=\"/static/d562503683c7b6a3c0ee5ed90085f60c/6f81f/normaljsexec.jpg 270w,\n/static/d562503683c7b6a3c0ee5ed90085f60c/09d21/normaljsexec.jpg 540w,\n/static/d562503683c7b6a3c0ee5ed90085f60c/47311/normaljsexec.jpg 1080w,\n/static/d562503683c7b6a3c0ee5ed90085f60c/78c62/normaljsexec.jpg 1589w\"\n        sizes=\"(max-width: 1080px) 100vw, 1080px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<h3 id=\"the-code-classlanguage-textasynccode-attribute\" style=\"position:relative;\"><a href=\"#the-code-classlanguage-textasynccode-attribute\" aria-label=\"the code classlanguage textasynccode attribute permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The <code class=\"language-text\">Async</code> attribute</h3>\n<p>The <code class=\"language-text\">Async</code> attribute is used to indicate that this resource can be executed asynchronously. The parsing doesn’t need to be halted, it can be done right after the resource is fetched from network and is ready.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"20109166581918147000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<script async src=&quot;script.js&quot;>`, `20109166581918147000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>script</span> <span class=\"token attr-name\">async</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>script.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>This attribute can be used only on external JavaScript files. The file would be downloaded in parallel and once the download is finished, the parsing is paused for the script to be executed:</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1080px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/7d5975f30349a8282be6f1d8b956cde5/ac99c/asyncjs.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 22.59259259259259%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAFABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAIF/8QAFgEBAQEAAAAAAAAAAAAAAAAAAgAB/9oADAMBAAIQAxAAAAHZkRsZf//EABgQAAIDAAAAAAAAAAAAAAAAAAAiAhES/9oACAEBAAEFAnMztj//xAAWEQEBAQAAAAAAAAAAAAAAAAAAARH/2gAIAQMBAT8BjH//xAAVEQEBAAAAAAAAAAAAAAAAAAAAEf/aAAgBAgEBPwGq/8QAGRAAAgMBAAAAAAAAAAAAAAAAAAEyM5Gh/9oACAEBAAY/ApLCzhJYf//EABoQAAICAwAAAAAAAAAAAAAAAAABESFR0fD/2gAIAQEAAT8hXBsV1tGBBH//2gAMAwEAAgADAAAAEPPv/8QAFxEAAwEAAAAAAAAAAAAAAAAAAAER4f/aAAgBAwEBPxBINLp//8QAFREBAQAAAAAAAAAAAAAAAAAAARD/2gAIAQIBAT8QYf/EABoQAQEBAAMBAAAAAAAAAAAAAAERACExYbH/2gAIAQEAAT8QozNTYS9QZPgl7b93/9k='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Async execution\"\n        title=\"\"\n        src=\"/static/7d5975f30349a8282be6f1d8b956cde5/47311/asyncjs.jpg\"\n        srcset=\"/static/7d5975f30349a8282be6f1d8b956cde5/6f81f/asyncjs.jpg 270w,\n/static/7d5975f30349a8282be6f1d8b956cde5/09d21/asyncjs.jpg 540w,\n/static/7d5975f30349a8282be6f1d8b956cde5/47311/asyncjs.jpg 1080w,\n/static/7d5975f30349a8282be6f1d8b956cde5/ac99c/asyncjs.jpg 1536w\"\n        sizes=\"(max-width: 1080px) 100vw, 1080px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<h3 id=\"the-code-classlanguage-textdefercode-attribute\" style=\"position:relative;\"><a href=\"#the-code-classlanguage-textdefercode-attribute\" aria-label=\"the code classlanguage textdefercode attribute permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The <code class=\"language-text\">Defer</code> attribute</h3>\n<p>The <code class=\"language-text\">Defer</code> attribute is used to tell the browser to execute this script after parsing the whole document.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"26011793348018040000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<script defer src=&quot;script.js&quot;>`, `26011793348018040000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>script</span> <span class=\"token attr-name\">defer</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>script.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>Like <code class=\"language-text\">Async</code> this file gets downloaded in parallel but the execution only happens when the whole <code class=\"language-text\">HTML</code> document is parsed:</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1080px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/61e576b9875fc024a3679482e7e2398a/ac99c/deferjs.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 22.59259259259259%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAFABQDASIAAhEBAxEB/8QAFwABAAMAAAAAAAAAAAAAAAAAAAEDBf/EABQBAQAAAAAAAAAAAAAAAAAAAAL/2gAMAwEAAhADEAAAAdmBC8F//8QAGBAAAgMAAAAAAAAAAAAAAAAAAQIAECL/2gAIAQEAAQUC1FDiv//EABURAQEAAAAAAAAAAAAAAAAAAAEQ/9oACAEDAQE/ASf/xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAcEAAABgMAAAAAAAAAAAAAAAAAAQIQESExgZH/2gAIAQEABj8CyXBa503/xAAZEAACAwEAAAAAAAAAAAAAAAABEQAQIUH/2gAIAQEAAT8hZ5UABnXP/9oADAMBAAIAAwAAABB4D//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8QH//EABURAQEAAAAAAAAAAAAAAAAAAAEQ/9oACAECAQE/EFn/xAAbEAEAAgIDAAAAAAAAAAAAAAABABEhQWFxof/aAAgBAQABPxDKBXvX2KUBXdU6pgRSuTP/2Q=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Deferred execution\"\n        title=\"\"\n        src=\"/static/61e576b9875fc024a3679482e7e2398a/47311/deferjs.jpg\"\n        srcset=\"/static/61e576b9875fc024a3679482e7e2398a/6f81f/deferjs.jpg 270w,\n/static/61e576b9875fc024a3679482e7e2398a/09d21/deferjs.jpg 540w,\n/static/61e576b9875fc024a3679482e7e2398a/47311/deferjs.jpg 1080w,\n/static/61e576b9875fc024a3679482e7e2398a/ac99c/deferjs.jpg 1536w\"\n        sizes=\"(max-width: 1080px) 100vw, 1080px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>At the end remember to put all of your <code class=\"language-text\">script</code> tags right at the end of the <code class=\"language-text\">body</code> to prevent more delay in parsing your <code class=\"language-text\">HTML</code>.</p>\n<p>As for the browser support, fortunately these attributes are fully supported by all of the major ones.</p>\n<h1 id=\"code-splitting\" style=\"position:relative;\"><a href=\"#code-splitting\" aria-label=\"code splitting permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Code splitting</h1>\n<p>Most of the modern sites will bundle all of their JavaScript into one, resulting in an increase in load time and suffering from load performance.</p>\n<p>Code splitting allows you to split your application code into separate chunks and lazy load them when needed. This also means minimum required code to client and improve the page load time.</p>\n<p>You can split your code in three areas:</p>\n<ul>\n<li>Vendor code</li>\n<li>Entry point</li>\n<li>Dynamic splitting</li>\n</ul>\n<h2 id=\"vendor-code\" style=\"position:relative;\"><a href=\"#vendor-code\" aria-label=\"vendor code permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Vendor code</h2>\n<p>Vendor code like Angular, React, moment, etc. can be separated from your main code. <a href=\"https://webpack.js.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Webpack</a> has full support for this and other methods. This technique allows you to have better control over cache invalidation of your bundles whenever your app or vendor code changes independently of one another.</p>\n<p>This is something every app should do.</p>\n<h2 id=\"entry-point\" style=\"position:relative;\"><a href=\"#entry-point\" aria-label=\"entry point permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Entry point</h2>\n<p>This technique separates your code by entry points in your app. These points are where bundlers like webpack start from, when they build a dependency tree of your app.</p>\n<p>This is bar far the easiest way to split code, but it is manual and has some pitfalls:</p>\n<ul>\n<li>If there are any duplicated modules between entry points, they will be bundled in both.</li>\n<li>It isn’t as flexible and cannot be used to dynamically split code with your app logic.</li>\n</ul>\n<p>This technique is not suitable for when you have client side routing or when you have a mix of server side rendering and a single page app.</p>\n<h2 id=\"dynamic-splitting\" style=\"position:relative;\"><a href=\"#dynamic-splitting\" aria-label=\"dynamic splitting permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Dynamic splitting</h2>\n<p>Separate code when dynamic <code class=\"language-text\">import</code> are used. This is the best option for single page applications. Having different modules for different routes in your SPA is an example of this.</p>\n<h2 id=\"do-i-even-need-code-splitting\" style=\"position:relative;\"><a href=\"#do-i-even-need-code-splitting\" aria-label=\"do i even need code splitting permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Do I even need code splitting?</h2>\n<p>This is one of many times when you hear me say it depends (I am a consultant after all 😉). If your app has many routes with isolated functionality and heavily uses frameworks and libraries, this answer is most probably <strong>YES</strong>.</p>\n<p>However, it is up to you to decide whether you need it or not by your own understanding of your app structure and code.</p>\n<h1 id=\"import-wisely\" style=\"position:relative;\"><a href=\"#import-wisely\" aria-label=\"import wisely permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Import wisely</h1>\n<p>If you use <code class=\"language-text\">npm</code> or other package management systems for your dependencies, then you will have a lot of extra and unneeded files in your buid folder.</p>\n<p>When using a framework or a library, make sure you investigate wether they have separate modules you can import and if yes, only import what you need.</p>\n<p>For instance, let’s assume you are using <a href=\"https://underscorejs.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">underscore</a>, but only use <code class=\"language-text\">groupBy</code>, <code class=\"language-text\">shuffle</code>, and <code class=\"language-text\">partition</code>. Most people import the whole library like this:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"97611862796379520000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import * as _ from 'underscore'`, `97611862796379520000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">import</span> <span class=\"token operator\">*</span> <span class=\"token keyword\">as</span> _ <span class=\"token keyword\">from</span> <span class=\"token string\">'underscore'</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>Instead of this, you could just import what you need:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"48223981261563020000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import {\r\n  groupBy,\r\n  shuffle,\r\n  partition,\r\n} from 'underscore'`, `48223981261563020000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span>\r\n  groupBy<span class=\"token punctuation\">,</span>\r\n  shuffle<span class=\"token punctuation\">,</span>\r\n  partition<span class=\"token punctuation\">,</span>\r\n<span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'underscore'</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>This way you only bring what your need and the bundlers will take care of the rest for you. Your total package size and as a result your page load time will decrease.</p>\n<h1 id=\"throttling-and-debounce\" style=\"position:relative;\"><a href=\"#throttling-and-debounce\" aria-label=\"throttling and debounce permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Throttling and debounce</h1>\n<p>Ok, enough about the size, let’s see where else we can improve our performance.</p>\n<p>Many times you have to add an event listener to do something, like listening to page scroll. Then we forget that the listener fires every time the event is triggered.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"57296372215837630000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`window.addEventListener('scroll', function() {\r\n  console.log('page scrolled')\r\n})`, `57296372215837630000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\">window<span class=\"token punctuation\">.</span><span class=\"token function\">addEventListener</span><span class=\"token punctuation\">(</span><span class=\"token string\">'scroll'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'page scrolled'</span><span class=\"token punctuation\">)</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>In the above example, the message is printed into console whenever you scroll. Imagine you have some heavy operation in that callback function, this would turn into a big performance bottleneck.</p>\n<p>If you can’t remove that event listener and use a different approach, then you can use either <code class=\"language-text\">debounce</code> or <code class=\"language-text\">throttle</code> to alleviate the situation.</p>\n<h2 id=\"debounce\" style=\"position:relative;\"><a href=\"#debounce\" aria-label=\"debounce permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Debounce</h2>\n<p>This feature enforces a function call to not happen until some time has passed since it’s last call. For example, call the function if 100 millisecond has passed from it’s last call.</p>\n<p>Look at this implementation from underscore:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"27463992666054214000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const debounce = (func, delay) => {\r\n  let inDebounce\r\n  return function() {\r\n    const context = this\r\n    const args = arguments\r\n    clearTimeout(inDebounce)\r\n    inDebounce = setTimeout(\r\n      () => func.apply(context, args),\r\n      delay\r\n    )\r\n  }\r\n}`, `27463992666054214000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token function-variable function\">debounce</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">func<span class=\"token punctuation\">,</span> delay</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">let</span> inDebounce\r\n  <span class=\"token keyword\">return</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">const</span> context <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span>\r\n    <span class=\"token keyword\">const</span> args <span class=\"token operator\">=</span> arguments\r\n    <span class=\"token function\">clearTimeout</span><span class=\"token punctuation\">(</span>inDebounce<span class=\"token punctuation\">)</span>\r\n    inDebounce <span class=\"token operator\">=</span> <span class=\"token function\">setTimeout</span><span class=\"token punctuation\">(</span>\r\n      <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token function\">func</span><span class=\"token punctuation\">.</span><span class=\"token function\">apply</span><span class=\"token punctuation\">(</span>context<span class=\"token punctuation\">,</span> args<span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\r\n      delay\r\n    <span class=\"token punctuation\">)</span>\r\n  <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Now we can debounce our event listener for every 100 millisecond:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"53433851718920960000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var efficientScrollListener = debounce(\r\n  function() {\r\n    console.log('page scrolled')\r\n  },\r\n  100\r\n)\r\n\r\nwindow.addEventListener(\r\n  'scroll',\r\n  efficientScrollListener\r\n)`, `53433851718920960000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">var</span> efficientScrollListener <span class=\"token operator\">=</span> <span class=\"token function\">debounce</span><span class=\"token punctuation\">(</span>\r\n  <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'page scrolled'</span><span class=\"token punctuation\">)</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token number\">100</span>\r\n<span class=\"token punctuation\">)</span>\r\n\r\nwindow<span class=\"token punctuation\">.</span><span class=\"token function\">addEventListener</span><span class=\"token punctuation\">(</span>\r\n  <span class=\"token string\">'scroll'</span><span class=\"token punctuation\">,</span>\r\n  efficientScrollListener\r\n<span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"throttle\" style=\"position:relative;\"><a href=\"#throttle\" aria-label=\"throttle permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Throttle</h2>\n<p>Throttling is similar to debounce but different since it will enforce the maximum number of times a function can be called over a period of time. For example execute this function once every 100 millisecond.</p>\n<p>Here is a simple implementation:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"16968976238430345000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const throttle = (func, limit) => {\r\n  let inThrottle\r\n  return function() {\r\n    const args = arguments\r\n    const context = this\r\n    if (!inThrottle) {\r\n      func.apply(context, args)\r\n      inThrottle = true\r\n      setTimeout(\r\n        () => (inThrottle = false),\r\n        limit\r\n      )\r\n    }\r\n  }\r\n}`, `16968976238430345000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token function-variable function\">throttle</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">func<span class=\"token punctuation\">,</span> limit</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">let</span> inThrottle\r\n  <span class=\"token keyword\">return</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">const</span> args <span class=\"token operator\">=</span> arguments\r\n    <span class=\"token keyword\">const</span> context <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span>\r\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>inThrottle<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n      <span class=\"token function\">func</span><span class=\"token punctuation\">.</span><span class=\"token function\">apply</span><span class=\"token punctuation\">(</span>context<span class=\"token punctuation\">,</span> args<span class=\"token punctuation\">)</span>\r\n      inThrottle <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span>\r\n      <span class=\"token function\">setTimeout</span><span class=\"token punctuation\">(</span>\r\n        <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span>inThrottle <span class=\"token operator\">=</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\r\n        limit\r\n      <span class=\"token punctuation\">)</span>\r\n    <span class=\"token punctuation\">}</span>\r\n  <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Now we can throttle our scroll event listener:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"60621288071380386000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`var efficientScrollListener = throttle(\r\n  function() {\r\n    console.log('page scrolled')\r\n  },\r\n  100\r\n)\r\n\r\nwindow.addEventListener(\r\n  'scroll',\r\n  efficientScrollListener\r\n)`, `60621288071380386000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">var</span> efficientScrollListener <span class=\"token operator\">=</span> <span class=\"token function\">throttle</span><span class=\"token punctuation\">(</span>\r\n  <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'page scrolled'</span><span class=\"token punctuation\">)</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token number\">100</span>\r\n<span class=\"token punctuation\">)</span>\r\n\r\nwindow<span class=\"token punctuation\">.</span><span class=\"token function\">addEventListener</span><span class=\"token punctuation\">(</span>\r\n  <span class=\"token string\">'scroll'</span><span class=\"token punctuation\">,</span>\r\n  efficientScrollListener\r\n<span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h1 id=\"so-what\" style=\"position:relative;\"><a href=\"#so-what\" aria-label=\"so what permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>So what</h1>\n<p>I hope I have given you enough information on just some of the areas you can focus to improve your applications performance when using JavaScript. If you would like to have other topics covered please comment below and I will add them here or in another post.</p>\n<p>And as always don’t forget to share the ❤️.</p>","timeToRead":5,"frontmatter":{"title":"Optimising JavaScript","unformattedDate":"2018-10-12T00:00:00.000Z","date":"Oct 12, 2018","path":"web-perf-3/","author":"Yaser Adel Mehraban","tags":["web","performance","javascript"],"thumbnail":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAOABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAIDBAX/xAAVAQEBAAAAAAAAAAAAAAAAAAAAAf/aAAwDAQACEAMQAAAB7aWimgQX/8QAGRABAAIDAAAAAAAAAAAAAAAAAQIRAAMS/9oACAEBAAEFAmg1T7jiWVHXkXo//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAFxAAAwEAAAAAAAAAAAAAAAAAABEg4f/aAAgBAQAGPwLYYz//xAAcEAADAAEFAAAAAAAAAAAAAAAAAREhEDFBYXH/2gAIAQEAAT8hW52ejrGuNFSY0MN3MCYD/9oADAMBAAIAAwAAABAAD//EABYRAQEBAAAAAAAAAAAAAAAAAAEQEf/aAAgBAwEBPxBHZ//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8QP//EABwQAQADAAIDAAAAAAAAAAAAAAEAESEQMUGh8f/aAAgBAQABPxBUxHf0htYKTS5474UHY+oLDaBQzJWRC62f/9k="},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/f7498c20b7bc9bf0825914e963e06b46/414c8/WebPerformance.jpg","srcSet":"/static/f7498c20b7bc9bf0825914e963e06b46/414c8/WebPerformance.jpg 448w","sizes":"(min-width: 448px) 448px, 100vw"},"sources":[{"srcSet":"/static/f7498c20b7bc9bf0825914e963e06b46/cfb46/WebPerformance.webp 448w","type":"image/webp","sizes":"(min-width: 448px) 448px, 100vw"}]},"width":448,"height":311}}}}}},{"node":{"id":"ad345796-3099-5b01-92d1-871eed53d94f","html":"<p>Mentioning this is trivial as no one seems to notice this while using default JQuery show and hide methods to show or hide an element on a page. As you might have noticed this before when you use <code class=\"language-text\">hide()</code>, the element gets a  <code class=\"language-text\">display:none;</code> style. The problem is when you use <code class=\"language-text\">show()</code> to show the element again and here is when you see<code class=\"language-text\">display:block;</code> is added to element styles.</p>\n<!--more-->\n<p>The problem compounds when animations get added into the mix (tons of inline CSS). I’m not saying don’t use them, but there are better ways and you can clean up after yourself in the DOM. Here’s an example for a simple hide/show.</p>\n<h2 id=\"html\" style=\"position:relative;\"><a href=\"#html\" aria-label=\"html permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>HTML</h2>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"43920860194300630000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<a href=&quot;#target1&quot; class=&quot;trigger&quot;>Toggle Content area 1</a>\r\n<div id=&quot;target1&quot; class=&quot;content&quot;>the content you want to hide/show</div>`, `43920860194300630000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>a</span> <span class=\"token attr-name\">href</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>#target1<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">class</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>trigger<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>Toggle Content area 1<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>a</span><span class=\"token punctuation\">></span></span>\r\n<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span> <span class=\"token attr-name\">id</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>target1<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">class</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>content<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>the content you want to hide/show<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<h2 id=\"css\" style=\"position:relative;\"><a href=\"#css\" aria-label=\"css permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>CSS</h2>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"79412283458731400000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// with this method I get to choose how I want to hide the content\r\n.is-hidden {\r\n  position: absolute;\r\n  top: -9999px;\r\n  left: -9999px;\r\n}\r\n\r\n// and I can easily make it work w/o JS\r\n.content:target {\r\n  position: static;\r\n}`, `79412283458731400000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"css\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-css line-numbers\"><code class=\"language-css\"><span class=\"token selector\">// with this method I get to choose how I want to hide the content\r\n.is-hidden</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">position</span><span class=\"token punctuation\">:</span> absolute<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">top</span><span class=\"token punctuation\">:</span> -9999px<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">left</span><span class=\"token punctuation\">:</span> -9999px<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token selector\">// and I can easily make it work w/o JS\r\n.content:target</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">position</span><span class=\"token punctuation\">:</span> static<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"javascript\" style=\"position:relative;\"><a href=\"#javascript\" aria-label=\"javascript permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>JavaScript</h2>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"62042583801709950000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`\\$('.trigger').on('click', function(e) {\r\n  e.preventDefault();\r\n\r\n  var target = \\$(this).attr('href'); // returns #target1\r\n\r\n  // see if the target area is hidden\r\n  if( \\$(target).hasClass('is-hidden') ) {\r\n\r\n    // if it's hidden, remove the class, but hide it so we can softly fade it in\r\n    \\$(target).removeClass('is-hidden').hide().fadeIn(250, function(){\r\n\r\n      // because fadeIn() adds some inline CSS, let's remove it to clean up the DOM\r\n      \\$(target).removeAttr('style');\r\n    });\r\n  } else {\r\n\r\n    // fade out the content\r\n    \\$(target)fadeOut(250, function(){\r\n\r\n     // once it's faded out, add the class and clean up the DOM\r\n     \\$(this).addClass('is-hidden').removeAttr('style');\r\n    });\r\n  }\r\n});`, `62042583801709950000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token function\">$</span><span class=\"token punctuation\">(</span><span class=\"token string\">'.trigger'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">on</span><span class=\"token punctuation\">(</span><span class=\"token string\">'click'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">e</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  e<span class=\"token punctuation\">.</span><span class=\"token function\">preventDefault</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n  <span class=\"token keyword\">var</span> target <span class=\"token operator\">=</span> <span class=\"token function\">$</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">attr</span><span class=\"token punctuation\">(</span><span class=\"token string\">'href'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// returns #target1</span>\r\n\r\n  <span class=\"token comment\">// see if the target area is hidden</span>\r\n  <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span> <span class=\"token function\">$</span><span class=\"token punctuation\">(</span>target<span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">hasClass</span><span class=\"token punctuation\">(</span><span class=\"token string\">'is-hidden'</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n\r\n    <span class=\"token comment\">// if it's hidden, remove the class, but hide it so we can softly fade it in</span>\r\n    <span class=\"token function\">$</span><span class=\"token punctuation\">(</span>target<span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">removeClass</span><span class=\"token punctuation\">(</span><span class=\"token string\">'is-hidden'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">hide</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">fadeIn</span><span class=\"token punctuation\">(</span><span class=\"token number\">250</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\r\n\r\n      <span class=\"token comment\">// because fadeIn() adds some inline CSS, let's remove it to clean up the DOM</span>\r\n      <span class=\"token function\">$</span><span class=\"token punctuation\">(</span>target<span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">removeAttr</span><span class=\"token punctuation\">(</span><span class=\"token string\">'style'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\r\n\r\n    <span class=\"token comment\">// fade out the content</span>\r\n    <span class=\"token function\">$</span><span class=\"token punctuation\">(</span>target<span class=\"token punctuation\">)</span><span class=\"token function\">fadeOut</span><span class=\"token punctuation\">(</span><span class=\"token number\">250</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\r\n\r\n     <span class=\"token comment\">// once it's faded out, add the class and clean up the DOM</span>\r\n     <span class=\"token function\">$</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">addClass</span><span class=\"token punctuation\">(</span><span class=\"token string\">'is-hidden'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">removeAttr</span><span class=\"token punctuation\">(</span><span class=\"token string\">'style'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"why-should-you-be-concerned\" style=\"position:relative;\"><a href=\"#why-should-you-be-concerned\" aria-label=\"why should you be concerned permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why should you be concerned?</h2>\n<p>I’ve been using this method for a very long time. I prefer it because it’s a cleaner separation between CSS and JavaScript and I’m not forced to use jQuery’s hide/show method of <code class=\"language-text\">display:none</code> (which really isn’t good for accessibility).</p>\n<p>Of course, the <code class=\"language-text\">fadeIn()</code> method could also be moved into a CSS animation at this point to clean this up even further.</p>","timeToRead":1,"frontmatter":{"title":"How to show or hide content without any JS library or code","unformattedDate":"2016-06-18T00:00:00.000Z","date":"Jun 18, 2016","path":"/how-to-show-or-hide-content-without-any-js-library-or-code/","author":"Yaser Adel Mehraban","tags":["css","html","javascript","Jquery"],"thumbnail":null}}}]}},"pageContext":{"tag":"javascript"}},"staticQueryHashes":[],"slicesMap":{}}