{"componentChunkName":"component---src-templates-blog-list-js","path":"/blog/4/","result":{"data":{"allMarkdownRemark":{"edges":[{"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=\"90502209327866990000\"\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`, `90502209327866990000`)\"\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=\"82438229560098520000\"\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?>`, `82438229560098520000`)\"\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=\"11067070483432495000\"\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`, `11067070483432495000`)\"\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=\"85957468455885100000\"\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`, `85957468455885100000`)\"\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=\"38859613385754100000\"\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`, `38859613385754100000`)\"\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=\"72030878395964330000\"\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`, `72030878395964330000`)\"\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=\"77799309614012780000\"\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`, `77799309614012780000`)\"\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=\"48361647339120610000\"\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`, `48361647339120610000`)\"\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=\"20388538056669802000\"\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`, `20388538056669802000`)\"\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=\"33747698068939403000\"\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`, `33747698068939403000`)\"\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=\"33442738269519933000\"\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`, `33442738269519933000`)\"\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=\"67509778550237990000\"\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`, `67509778550237990000`)\"\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=\"51877361419628620000\"\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`, `51877361419628620000`)\"\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=\"93462476477526470000\"\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`, `93462476477526470000`)\"\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=\"51606165597582750000\"\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`, `51606165597582750000`)\"\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=\"6395464902821257000\"\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`, `6395464902821257000`)\"\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=\"98648302666269560000\"\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`, `98648302666269560000`)\"\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=\"51757936567640160000\"\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!`, `51757936567640160000`)\"\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=\"23338607126287503000\"\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`, `23338607126287503000`)\"\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=\"52429213795308050000\"\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`, `52429213795308050000`)\"\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=\"94502536040501560000\"\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!`, `94502536040501560000`)\"\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=\"9599345143591530000\"\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!`, `9599345143591530000`)\"\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=\"65594199978940800000\"\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;]`, `65594199978940800000`)\"\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=\"37081693186577570000\"\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!`, `37081693186577570000`)\"\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=\"79670526969601700000\"\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 🐶!`, `79670526969601700000`)\"\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=\"82589999142396350000\"\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 🐩!`, `82589999142396350000`)\"\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=\"70176192151195770000\"\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!`, `70176192151195770000`)\"\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=\"48307677210177390000\"\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;`, `48307677210177390000`)\"\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=\"64127256127812805000\"\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!`, `64127256127812805000`)\"\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=\"93793167900866080000\"\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;`, `93793167900866080000`)\"\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=\"60228488505759700000\"\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`, `60228488505759700000`)\"\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=\"41611504587795145000\"\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;`, `41611504587795145000`)\"\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=\"57578496812322340000\"\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;`, `57578496812322340000`)\"\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=\"3177546791667351600\"\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;`, `3177546791667351600`)\"\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=\"87491741997303070000\"\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,`, `87491741997303070000`)\"\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=\"18863390738782093000\"\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;`, `18863390738782093000`)\"\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=\"62657252691792585000\"\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`, `62657252691792585000`)\"\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=\"70998086311060950000\"\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`, `70998086311060950000`)\"\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=\"20467461727779156000\"\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`, `20467461727779156000`)\"\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=\"95084310907642530000\"\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]`, `95084310907642530000`)\"\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=\"97231270179075310000\"\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]`, `97231270179075310000`)\"\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=\"39430385057145510000\"\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}`, `39430385057145510000`)\"\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=\"65228643775495620000\"\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}`, `65228643775495620000`)\"\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=\"69703107350124590000\"\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]`, `69703107350124590000`)\"\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=\"43539887479499860000\"\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}`, `43539887479499860000`)\"\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=\"64822421432262090000\"\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// 🍉, 🍎, 🍌`, `64822421432262090000`)\"\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=\"3280300034117967000\"\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`, `3280300034117967000`)\"\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=\"13242155191797944000\"\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// ...`, `13242155191797944000`)\"\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=\"65150458332699770000\"\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.`, `65150458332699770000`)\"\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=\"42403589730883715000\"\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`, `42403589730883715000`)\"\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":"5a075de4-36f3-5743-b35b-23b50c63ac5f","html":"<p>You’ve spent many hours trying to improve your web performance and have got it to a good speed. What happens next? How do you ensure it remains in good shape especially if you’re working in a team with diverse backgrounds and level of coding.</p>\n<!--more-->\n<h2 id=\"web-performance-must-be-monitored\" style=\"position:relative;\"><a href=\"#web-performance-must-be-monitored\" aria-label=\"web performance must be monitored 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>Web performance must be monitored</h2>\n<p>I truly believe web performance shouldn’t be an item in your backlog, rather part of acceptance criteria on each story. That said, having discipline is hard when it comes to web performance. Each member of the team should be aware of dos and don’ts so that they can right code which doesn’t add a few seconds to current page load time, or user’s interactivity with the page.</p>\n<p>In order to do so, you need to constantly monitor the performance of your application and when is a better time to do it than your CI/CD pipeline or when creating a pull request to get the code merged in (it’s worth mentioning having a CI/CD pipeline is not necessary to have a high quality, well written and performing site).</p>\n<p>In addition to all of that, your site performs differently from your localhost, so you need to ensure you’re testing close to your actual environment.</p>\n<h2 id=\"lighthouse-ci\" style=\"position:relative;\"><a href=\"#lighthouse-ci\" aria-label=\"lighthouse ci 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>Lighthouse CI</h2>\n<p><a href=\"https://github.com/GoogleChrome/lighthouse-ci\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Lighthouse CI</a> is a set of commands that make continuously running, asserting, saving, and retrieving Lighthouse results as easy as possible.</p>\n<p>This is a great tool which allows you to run audits on your code base using command line, and raise an alert when someone unintentionally downgrades the performance.</p>\n<h2 id=\"what-to-measure\" style=\"position:relative;\"><a href=\"#what-to-measure\" aria-label=\"what to measure 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 to measure?</h2>\n<p>There are countless metrics on which we could focus on. Some are user centric like time to interact, some are resource centric such as the size of your <em>JavaScript</em> or <em>CSS</em> files. Lighthouse CI allows us to easily focus on the size of the files. You can create two types of budgets:</p>\n<p>1- <strong>Number of given resource</strong>: This will help you to for example enforce having only <code class=\"language-text\">3</code> <em>JavaScript</em> and <code class=\"language-text\">1</code> <em>CSS</em> file.\r\n2- <strong>Size of a given resource</strong>: This budget will enforce that a given resource is of certain size, e.g. only <code class=\"language-text\">500kb</code>.</p>\n<h2 id=\"how-does-it-know\" style=\"position:relative;\"><a href=\"#how-does-it-know\" aria-label=\"how does it know 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 know?</h2>\n<p>You’re probably wondering how does <strong>Lighthouse CI</strong> know what to measure and when to raise an alert. This **||is done via a <code class=\"language-text\">JSON</code> file which is usually named <code class=\"language-text\">budget.json</code>, but you can choose any name you prefer.</p>\n<h2 id=\"performance-budget\" style=\"position:relative;\"><a href=\"#performance-budget\" aria-label=\"performance budget 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>Performance budget</h2>\n<p>The content of this file is an array of objects representing the budgets for a path. The content will look like below if you only have one path to assert:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"25649810326442734000\"\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    &quot;resourceSizes&quot;: [\r\n      ...\r\n    ],\r\n    &quot;resourceCounts&quot;: [\r\n      ...\r\n    ]\r\n  }\r\n]`, `25649810326442734000`)\"\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=\"json\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-json line-numbers\"><code class=\"language-json\"><span class=\"token punctuation\">[</span>\r\n  <span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">\"resourceSizes\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\r\n      ...\r\n    <span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\r\n    <span class=\"token property\">\"resourceCounts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\r\n      ...\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></pre></div>\n<p>Within the <code class=\"language-text\">resourceSizes</code> and <code class=\"language-text\">resourceCounts</code> arrays, you can add individual objects that specify the <code class=\"language-text\">resourceType</code> and its budget. The budget can be any positive integer and the <code class=\"language-text\">resourceType</code> can be any of the following:</p>\n<table>\n<thead>\n<tr>\n<th><strong>Resource type</strong></th>\n<th><strong>Description</strong></th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><strong>script</strong></td>\n<td>JavaScript files</td>\n</tr>\n<tr>\n<td><strong>document</strong></td>\n<td>HTML documents</td>\n</tr>\n<tr>\n<td><strong>image</strong></td>\n<td>Images</td>\n</tr>\n<tr>\n<td><strong>stylesheet</strong></td>\n<td>CSS files</td>\n</tr>\n<tr>\n<td><strong>font</strong></td>\n<td>Webfonts</td>\n</tr>\n<tr>\n<td><strong>media</strong></td>\n<td>Other media</td>\n</tr>\n<tr>\n<td><strong>other</strong></td>\n<td>Any other resource which doesn’t match the above, e.g. data transfers over Websocket connections</td>\n</tr>\n<tr>\n<td><strong>third-party</strong></td>\n<td>All resources from a third-party domain</td>\n</tr>\n<tr>\n<td><strong>total</strong></td>\n<td>All resources combined</td>\n</tr>\n</tbody>\n</table>\n<p>For example, the following file will set a 200Kb budget for all scripts, a 500Kb for all images, and a 500Kb for all other resources. Then it sets the total network requests to be 25.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"11276160670835433000\"\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    &quot;resourceSizes&quot;: [\r\n      {\r\n        &quot;resourceType&quot;: &quot;script&quot;,\r\n        &quot;budget&quot;: 200\r\n      },\r\n      {\r\n        &quot;resourceType&quot;: &quot;image&quot;,\r\n        &quot;budget&quot;: 500\r\n      },\r\n      {\r\n        &quot;resourceType&quot;: &quot;total&quot;,\r\n        &quot;budget&quot;: 500\r\n      }\r\n    ],\r\n    &quot;resourceCounts&quot;: [\r\n      {\r\n        &quot;resourceType&quot;: &quot;total&quot;,\r\n        &quot;budget&quot;: 25\r\n      }\r\n    ]\r\n  }\r\n]`, `11276160670835433000`)\"\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=\"json\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-json line-numbers\"><code class=\"language-json\"><span class=\"token punctuation\">[</span>\r\n  <span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">\"resourceSizes\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\r\n      <span class=\"token punctuation\">{</span>\r\n        <span class=\"token property\">\"resourceType\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"script\"</span><span class=\"token punctuation\">,</span>\r\n        <span class=\"token property\">\"budget\"</span><span class=\"token operator\">:</span> <span class=\"token number\">200</span>\r\n      <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n      <span class=\"token punctuation\">{</span>\r\n        <span class=\"token property\">\"resourceType\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"image\"</span><span class=\"token punctuation\">,</span>\r\n        <span class=\"token property\">\"budget\"</span><span class=\"token operator\">:</span> <span class=\"token number\">500</span>\r\n      <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n      <span class=\"token punctuation\">{</span>\r\n        <span class=\"token property\">\"resourceType\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"total\"</span><span class=\"token punctuation\">,</span>\r\n        <span class=\"token property\">\"budget\"</span><span class=\"token operator\">:</span> <span class=\"token number\">500</span>\r\n      <span class=\"token punctuation\">}</span>\r\n    <span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\r\n    <span class=\"token property\">\"resourceCounts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\r\n      <span class=\"token punctuation\">{</span>\r\n        <span class=\"token property\">\"resourceType\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"total\"</span><span class=\"token punctuation\">,</span>\r\n        <span class=\"token property\">\"budget\"</span><span class=\"token operator\">:</span> <span class=\"token number\">25</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></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<p>As of <strong>Lighthouse</strong> 5.3 you can add a <code class=\"language-text\">path</code> property to select which pages the budget should apply to. This is very handy because it allows you to have a centralised budget file for all your pages.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"37148249262368060000\"\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    &quot;path&quot;:&quot;/*&quot;,\r\n    &quot;resourceSizes&quot;: [\r\n      ...\r\n    ],\r\n    &quot;resourceCounts&quot;: [\r\n      ...\r\n    ]\r\n  },\r\n  {\r\n    &quot;path&quot;:&quot;/blog&quot;,\r\n    &quot;resourceSizes&quot;: [\r\n      ...\r\n    ],\r\n    &quot;resourceCounts&quot;: [\r\n      ...\r\n    ]\r\n  },\r\n  {\r\n    &quot;path&quot;:&quot;/contact&quot;,\r\n    &quot;resourceSizes&quot;: [\r\n      ...\r\n    ],\r\n    &quot;resourceCounts&quot;: [\r\n      ...\r\n    ]\r\n  }\r\n]`, `37148249262368060000`)\"\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=\"json\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-json line-numbers\"><code class=\"language-json\"><span class=\"token punctuation\">[</span>\r\n  <span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">\"path\"</span><span class=\"token operator\">:</span><span class=\"token string\">\"/*\"</span><span class=\"token punctuation\">,</span>\r\n    <span class=\"token property\">\"resourceSizes\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\r\n      ...\r\n    <span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\r\n    <span class=\"token property\">\"resourceCounts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\r\n      ...\r\n    <span class=\"token punctuation\">]</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">\"path\"</span><span class=\"token operator\">:</span><span class=\"token string\">\"/blog\"</span><span class=\"token punctuation\">,</span>\r\n    <span class=\"token property\">\"resourceSizes\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\r\n      ...\r\n    <span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\r\n    <span class=\"token property\">\"resourceCounts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\r\n      ...\r\n    <span class=\"token punctuation\">]</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">\"path\"</span><span class=\"token operator\">:</span><span class=\"token string\">\"/contact\"</span><span class=\"token punctuation\">,</span>\r\n    <span class=\"token property\">\"resourceSizes\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\r\n      ...\r\n    <span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\r\n    <span class=\"token property\">\"resourceCounts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\r\n      ...\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></span><span></span><span></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=\"how-should-you-choose-your-numbers\" style=\"position:relative;\"><a href=\"#how-should-you-choose-your-numbers\" aria-label=\"how should you choose your 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>How should you choose your numbers?</h2>\n<p>Finding the performance budget is not an easy task since it will depend on your application’s structure and bundling style. However, you can use this useful tool called <a href=\"https://perf-budget-calculator.firebaseapp.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><strong>Performance Budget Calculator</strong></a> to have a starting point. You might have to run the tool multiple time to each to a point where you’d be happy with the result. You don’t want to be too aggressive and also too complacent either.</p>\n<h2 id=\"using-the-ci-locally\" style=\"position:relative;\"><a href=\"#using-the-ci-locally\" aria-label=\"using the ci locally 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 the CI locally</h2>\n<p>You can install the CI locally with:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"28686428566898446000\"\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(`npm install -g @lhci/cli`, `28686428566898446000`)\"\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 function\">npm</span> <span class=\"token function\">install</span> <span class=\"token parameter variable\">-g</span> @lhci/cli</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>It also has a dashboard which shows you the historical runs, compares reports and store their results.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"24203219055803093000\"\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(`npm install @lhci/server`, `24203219055803093000`)\"\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 function\">npm</span> <span class=\"token function\">install</span> @lhci/server</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>Before running the CI, you need to have a configuration file to tell the tool what to test and where to find the budget file (you could also pass those individually without a config file, but it’d be a pain):</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"97885388790054930000\"\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(`// lighthouserc.json\r\n{\r\n  &quot;ci&quot;: {\r\n    &quot;collect&quot;: {\r\n      &quot;numberOfRuns&quot;: 2,\r\n      &quot;startServerCommand&quot;: &quot;yarn serve&quot;,\r\n      &quot;url&quot;: &quot;http://localhost:8080&quot;\r\n    },\r\n    &quot;preset&quot;: &quot;lighthouse:recommended&quot;\r\n  }\r\n}`, `97885388790054930000`)\"\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=\"json\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-json line-numbers\"><code class=\"language-json\"><span class=\"token comment\">// lighthouserc.json</span>\r\n<span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">\"ci\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">\"collect\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\r\n      <span class=\"token property\">\"numberOfRuns\"</span><span class=\"token operator\">:</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span>\r\n      <span class=\"token property\">\"startServerCommand\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"yarn serve\"</span><span class=\"token punctuation\">,</span>\r\n      <span class=\"token property\">\"url\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"http://localhost:8080\"</span>\r\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n    <span class=\"token property\">\"preset\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"lighthouse:recommended\"</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></pre></div>\n<p>Then you can run the tool on your local web server by first running:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"63879864146989030000\"\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(`lhci autorun --config=./lighthouserc.json`, `63879864146989030000`)\"\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\">lhci autorun <span class=\"token parameter variable\">--config</span><span class=\"token operator\">=</span>./lighthouserc.json</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 then:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"77111024990371600000\"\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(`lhci assert --no-lighthouserc --budgetsFile=budget.json`, `77111024990371600000`)\"\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\">lhci assert --no-lighthouserc <span class=\"token parameter variable\">--budgetsFile</span><span class=\"token operator\">=</span>budget.json</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 get a result similar to below based on your app’s performance:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"32366766976500716000\"\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(`2 result(s) for http://localhost:8080/\r\n\r\n  ×  resource-summary.script.size failure for maxNumericValue assertion\r\n     Keep request counts low and transfer sizes small\r\n     Documentation: https://developers.google.com/web/tools/lighthouse/audits/budgets\r\n\r\n        expected: <=102400\r\n           found: 2416174\r\n      all values: 2416174, 2416174\r\n\r\n\r\n  ×  resource-summary.total.size failure for maxNumericValue assertion\r\n     Keep request counts low and transfer sizes small\r\n     Documentation: https://developers.google.com/web/tools/lighthouse/audits/budgets\r\n\r\n        expected: <=512000\r\n           found: 2437694\r\n      all values: 2437694, 2437694\r\n\r\nAssertion failed. Exiting with status code 1.`, `32366766976500716000`)\"\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 number\">2</span> result<span class=\"token punctuation\">(</span>s<span class=\"token punctuation\">)</span> <span class=\"token keyword\">for</span> http://localhost:8080/\r\n\r\n  ×  resource-summary.script.size failure <span class=\"token keyword\">for</span> maxNumericValue assertion\r\n     Keep request counts low and transfer sizes small\r\n     Documentation: https://developers.google.com/web/tools/lighthouse/audits/budgets\r\n\r\n        expected: <span class=\"token operator\">&lt;=</span><span class=\"token number\">102400</span>\r\n           found: <span class=\"token number\">2416174</span>\r\n      all values: <span class=\"token number\">2416174</span>, <span class=\"token number\">2416174</span>\r\n\r\n\r\n  ×  resource-summary.total.size failure <span class=\"token keyword\">for</span> maxNumericValue assertion\r\n     Keep request counts low and transfer sizes small\r\n     Documentation: https://developers.google.com/web/tools/lighthouse/audits/budgets\r\n\r\n        expected: <span class=\"token operator\">&lt;=</span><span class=\"token number\">512000</span>\r\n           found: <span class=\"token number\">2437694</span>\r\n      all values: <span class=\"token number\">2437694</span>, <span class=\"token number\">2437694</span>\r\n\r\nAssertion failed. Exiting with status code <span class=\"token number\">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></span><span></span><span></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=\"using-the-tool-on-your-prs\" style=\"position:relative;\"><a href=\"#using-the-tool-on-your-prs\" aria-label=\"using the tool on your prs 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 the tool on your PRs</h2>\n<p>I am focusing on <a href=\"https://github.com/features/actions\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">GitHub</a> using actions here. But you can apply this technique to <a href=\"https://azure.microsoft.com/en-au/services/devops/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Azure DevOps</a>, <a href=\"https://circleci.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">CircleCI</a>, <a href=\"https://www.jetbrains.com/teamcity/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">TeamCity</a>, or any other tool you’re using. For this to work you will need to have a <a href=\"https://guides.github.com/introduction/flow/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">GitHub flow</a> in your <code class=\"language-text\">git</code> repository defined.</p>\n<p>This article’s focus is not to teach you how to create a workflow, but here is the bare minimum you will need to have. Create a <code class=\"language-text\">.yaml</code>/<code class=\"language-text\">.yml</code> file in the <code class=\"language-text\">.github/workflows</code> folder, you can name it whatever you want. I usually name them after what’s their purpose, e.g. <code class=\"language-text\">lighthouse.yml</code> or <code class=\"language-text\">lhci-performance-bot.yml</code>.</p>\n<p>But before we get to the content of our <code class=\"language-text\">yaml</code> file, let’s see what we need to do on each PR:</p>\n<ol>\n<li>Pull down the code</li>\n<li>Build the app</li>\n<li>Deploy to a temporary site (I am using Netlify Preview here, but you can use others like staging slots on Azure App Service, etc)</li>\n<li>Run Lighthouse CI on the site</li>\n<li>Fail if the conditions are not met</li>\n</ol>\n<p>For this to work, we need an application. So I just spun up a new <a href=\"https://vuejs.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Vue</a> application using <a href=\"https://cli.vuejs.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Vue CLI</a>, then added my workflow to it and pushed it to <a href=\"https://github.com/yashints/lighthouse-ci-pr-action\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">a new GitHub repo</a>.</p>\n<p>Here is how my workflow looks like:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"99288361220041770000\"\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(`name: Lighthouse CI\r\n\r\non:\r\n  pull_request:\r\n    branches: [master]\r\n\r\njobs:\r\n  lighthouse:\r\n    runs-on: ubuntu-latest\r\n\r\n    steps:\r\n      - uses: actions/checkout@v2\r\n      - name: Setup Node 12\r\n        uses: actions/setup-node@v1\r\n        with:\r\n          node-version: 12.x\r\n      - name: Yarn Install and Build\r\n        run: |\r\n          yarn\r\n          yarn build\r\n      \r\n      - name: Waiting for 200 from the Netlify Preview\r\n        uses: jakepartusch/wait-for-netlify-action@v1\r\n        id: waitFor200\r\n        with:\r\n          site_name: &quot;lighthouse-ci-pr&quot;\r\n\r\n      - name: Audit URLs using Lighthouse\r\n        uses: treosh/lighthouse-ci-action@v2\r\n        with:\r\n          urls: |\r\n            \\$DEPLOY_URL/\r\n            \\$DEPLOY_URL/about\r\n          runs: 3\r\n          budgetPath: ./budget.json\r\n        env:\r\n          DEPLOY_URL: \\${{ steps.waitFor200.outputs.url }}`, `99288361220041770000`)\"\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=\"yml\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-yml line-numbers\"><code class=\"language-yml\"><span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Lighthouse CI\r\n\r\n<span class=\"token key atrule\">on</span><span class=\"token punctuation\">:</span>\r\n  <span class=\"token key atrule\">pull_request</span><span class=\"token punctuation\">:</span>\r\n    <span class=\"token key atrule\">branches</span><span class=\"token punctuation\">:</span> <span class=\"token punctuation\">[</span>master<span class=\"token punctuation\">]</span>\r\n\r\n<span class=\"token key atrule\">jobs</span><span class=\"token punctuation\">:</span>\r\n  <span class=\"token key atrule\">lighthouse</span><span class=\"token punctuation\">:</span>\r\n    <span class=\"token key atrule\">runs-on</span><span class=\"token punctuation\">:</span> ubuntu<span class=\"token punctuation\">-</span>latest\r\n\r\n    <span class=\"token key atrule\">steps</span><span class=\"token punctuation\">:</span>\r\n      <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">uses</span><span class=\"token punctuation\">:</span> actions/checkout@v2\r\n      <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Setup Node 12\r\n        <span class=\"token key atrule\">uses</span><span class=\"token punctuation\">:</span> actions/setup<span class=\"token punctuation\">-</span>node@v1\r\n        <span class=\"token key atrule\">with</span><span class=\"token punctuation\">:</span>\r\n          <span class=\"token key atrule\">node-version</span><span class=\"token punctuation\">:</span> 12.x\r\n      <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Yarn Install and Build\r\n        <span class=\"token key atrule\">run</span><span class=\"token punctuation\">:</span> <span class=\"token punctuation\">|</span><span class=\"token scalar string\">\r\n          yarn\r\n          yarn build</span>\r\n      \r\n      <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Waiting for 200 from the Netlify Preview\r\n        <span class=\"token key atrule\">uses</span><span class=\"token punctuation\">:</span> jakepartusch/wait<span class=\"token punctuation\">-</span>for<span class=\"token punctuation\">-</span>netlify<span class=\"token punctuation\">-</span>action@v1\r\n        <span class=\"token key atrule\">id</span><span class=\"token punctuation\">:</span> waitFor200\r\n        <span class=\"token key atrule\">with</span><span class=\"token punctuation\">:</span>\r\n          <span class=\"token key atrule\">site_name</span><span class=\"token punctuation\">:</span> <span class=\"token string\">\"lighthouse-ci-pr\"</span>\r\n\r\n      <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Audit URLs using Lighthouse\r\n        <span class=\"token key atrule\">uses</span><span class=\"token punctuation\">:</span> treosh/lighthouse<span class=\"token punctuation\">-</span>ci<span class=\"token punctuation\">-</span>action@v2\r\n        <span class=\"token key atrule\">with</span><span class=\"token punctuation\">:</span>\r\n          <span class=\"token key atrule\">urls</span><span class=\"token punctuation\">:</span> <span class=\"token punctuation\">|</span><span class=\"token scalar string\">\r\n            $DEPLOY_URL/\r\n            $DEPLOY_URL/about</span>\r\n          <span class=\"token key atrule\">runs</span><span class=\"token punctuation\">:</span> <span class=\"token number\">3</span>\r\n          <span class=\"token key atrule\">budgetPath</span><span class=\"token punctuation\">:</span> ./budget.json\r\n        <span class=\"token key atrule\">env</span><span class=\"token punctuation\">:</span>\r\n          <span class=\"token key atrule\">DEPLOY_URL</span><span class=\"token punctuation\">:</span> $<span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span> steps.waitFor200.outputs.url <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></span><span></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 is happening is that I am just creating an action on each PR. In this action, we need to pull down the code, install node, run <code class=\"language-text\">yarn</code> and <code class=\"language-text\">yarn build</code> to build the application, wait for Netlify to deploy our PR into a preview site, then get the URL and inject it into the next step.</p>\n<p>I’m just using <a href=\"https://github.com/marketplace/actions/wait-for-netlify\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">wait-for-netlify-action</code></a>, but it’s not that hard to implement this part. Then we will be using the <a href=\"https://github.com/treosh/lighthouse-ci-action/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">lighthouse-ci-action</code></a> to run our audit on the paths we have specified.</p>\n<p>Notice I am not using a <strong>Lighthouse</strong> configuration file in this workflow because it’s not needed. You can pass the parameters directly to the action like I’ve done above. I am just auditing the home and about pages since I’ve only got these two pages.</p>\n<p>You can pass as many URL’s to this as you like, but if there are many of the, it makes more sense to add the config file back.</p>\n<blockquote>\n<p>💡 Remember to setup Netlify for your site and set it to create a deployment for every PR. Then get the site id and pass it to the <code class=\"language-text\">wait-for-netlify-action</code> action.</p>\n</blockquote>\n<p><strong>Pro tip</strong>: When you setup your Vue CLI generated application which is using Vue Router, the non default routes won’t work if the user types them in the browser URL bar and hits enter. That’s because that route is handled on the client side and server doesn’t know about it. To fix that you need to add a URL rewrite rule to return your <code class=\"language-text\">index.html</code> for all legitimate routes.</p>\n<p>In case of Netlify, just create a file named <code class=\"language-text\">_redirects</code> inside your <em>public</em> and add the following line to it.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"56249322138618200000\"\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(`/*    /index.html   200`, `56249322138618200000`)\"\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\">/*    /index.html   <span class=\"token number\">200</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=\"result\" style=\"position:relative;\"><a href=\"#result\" aria-label=\"result 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>Result</h2>\n<p>I created a branch and pushed it up. Then created a PR and voila, the action kicked in and failed, since I have a large image on the about page.</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/6d8fde67ffb5d7e17f0cfe9e2c0d8100/4e663/ci-results.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: 44.074074074074076%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAJABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAMFBP/EABUBAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAHTHrQkeID/xAAcEAAABgMAAAAAAAAAAAAAAAAAAQIDBBMFEDL/2gAIAQEAAQUCmmpBWui5wZDnX//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABoQAAIDAQEAAAAAAAAAAAAAAAACATOREBL/2gAIAQEABj8CXy0wWNpY2id//8QAGBABAQADAAAAAAAAAAAAAAAAARAhMaH/2gAIAQEAAT8hymy6bJXQ3//aAAwDAQACAAMAAAAQbD//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/ED//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAdEAACAQQDAAAAAAAAAAAAAAABEQAQIVHRMZHw/9oACAEBAAE/EGaztl2GFCDZ3CTnsz1cCv8A/9k='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"PR validation failed on GitHub action using lighthouse CI&#39;s audit\"\n        title=\"\"\n        src=\"/static/6d8fde67ffb5d7e17f0cfe9e2c0d8100/47311/ci-results.jpg\"\n        srcset=\"/static/6d8fde67ffb5d7e17f0cfe9e2c0d8100/6f81f/ci-results.jpg 270w,\n/static/6d8fde67ffb5d7e17f0cfe9e2c0d8100/09d21/ci-results.jpg 540w,\n/static/6d8fde67ffb5d7e17f0cfe9e2c0d8100/47311/ci-results.jpg 1080w,\n/static/6d8fde67ffb5d7e17f0cfe9e2c0d8100/0047d/ci-results.jpg 1620w,\n/static/6d8fde67ffb5d7e17f0cfe9e2c0d8100/4e663/ci-results.jpg 1893w\"\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>I then reduced the size of the image and pushed the changes up. This time it passed.</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/63e3628409b060661f7e8007c50a4a98/98d66/auditsuccess.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: 41.11111111111111%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAIABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAQF/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAH/2gAMAwEAAhADEAAAAbMsQD//xAAZEAACAwEAAAAAAAAAAAAAAAAAAwECFAT/2gAIAQEAAQUC62XVGtxrcf/EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABoQAAICAwAAAAAAAAAAAAAAAAACMqESMWH/2gAIAQEABj8CXFt8JUSo/8QAGBAAAwEBAAAAAAAAAAAAAAAAAAFhkRH/2gAIAQEAAT8hbyF0xPgmwf/aAAwDAQACAAMAAAAQf/8A/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPxA//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPxA//8QAGxAAAQQDAAAAAAAAAAAAAAAAAQARUfBBYcH/2gAIAQEAAT8QKagCWYAl1cuLV2hf/9k='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"PR validation passing on GitHub action using lighthouse CI\"\n        title=\"\"\n        src=\"/static/63e3628409b060661f7e8007c50a4a98/47311/auditsuccess.jpg\"\n        srcset=\"/static/63e3628409b060661f7e8007c50a4a98/6f81f/auditsuccess.jpg 270w,\n/static/63e3628409b060661f7e8007c50a4a98/09d21/auditsuccess.jpg 540w,\n/static/63e3628409b060661f7e8007c50a4a98/47311/auditsuccess.jpg 1080w,\n/static/63e3628409b060661f7e8007c50a4a98/0047d/auditsuccess.jpg 1620w,\n/static/63e3628409b060661f7e8007c50a4a98/98d66/auditsuccess.jpg 1905w\"\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=\"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 learned together how to use Lighthouse CI, a tool which allows you to run audits on your sites from command line in our CI/CD pipeline to make sure no one is downgrading the performance when they work on the application.</p>\n<p>This way, we’re always on top of our game rather than creating backlog items to find the issues and fix them later.</p>\n<p>In addition to that, your users are going to be happy and remain happy with their experience which is worth all the effort.</p>\n<p>Hope you’ve enjoyed reading this and happy performance enforcement 👋🏽.</p>","timeToRead":6,"frontmatter":{"title":"Stay on top of performance with Lighthouse CI 🚦","unformattedDate":"2020-04-13T00:00:00.000Z","date":"Apr 13, 2020","path":"/lighthouse-ci/","author":"Yaser Adel Mehraban","tags":["webdev","showdev","performance","lighthouse"],"thumbnail":null}}},{"node":{"id":"7140239a-3118-5a48-a0a7-4323a38b2d40","html":"<p>I was working on a project a week ago and had a local branch (with a few commits) I was going to push to upstream to create a PR (pull request). However, after pulling down master and rebasing, I realised all of my code except my first commit is gone after rebase 😱.</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>I had a local branch which I was working on. Since I’d made a few changes before end of the day, I committed those and went about to do some parenting.</p>\n<p>The day after that, I restarted my work and had many other changes in a few other commits by the time I was ready to push my code to upstream. But before we do it, it’s our practice to pull down master and rebase in case there is any conflict, or changes made by someone else has had any side effects on the bit we’ve done.</p>\n<h2 id=\"rebase\" style=\"position:relative;\"><a href=\"#rebase\" aria-label=\"rebase 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>Rebase</h2>\n<p>So I pulled down the master branch and rebased it directly from origin using (don’t do this at home please, I’ll explain later):</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"78845794609779600000\"\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(`git pull origin master --rebase`, `78845794609779600000`)\"\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=\"powershell\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-powershell line-numbers\"><code class=\"language-powershell\">git pull origin master <span class=\"token operator\">--</span>rebase</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 done, I realised I had some conflicts since someone else had worked on one of the files I’d modified. So I started the merge conflict process and made sure all was good. However, I didn’t realised that when I tested the web application, the latest code wasn’t refreshed, and I was seeing my own code (don’t ask me why, it’s an old code base and you need to rebuild and hard refresh every time you make a change).</p>\n<p>Having seen all is good I created a PR and went for lunch. One of colleagues then reviewed the PR and since all my changes had gone apart from the small parts from yesterday’s commit, he approved and completed the PR.</p>\n<h2 id=\"someone-raised-a-bug\" style=\"position:relative;\"><a href=\"#someone-raised-a-bug\" aria-label=\"someone raised a 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>Someone raised a bug</h2>\n<p>I was working on something else when I realised someone has raised a bug on the feature I’d implemented and when I checked the code I was in total shock since most of my code wasn’t there anymore. So I checked my local branch and saw that’s the case, in fact several of my commits were missing from that branch altogether.</p>\n<p>Later I realised that I’d forgotten to add everything before committing 🤦🏽‍♂️. Just to clarify this was during a really crazy time where we were working day and night for over a week to deliver this project for all of our hospitals across Australia and New Zealand to monitor their resources in intensive care like number of available beds, ventilator, etc. I will tell you all about it once I find some time.</p>\n<h2 id=\"initial-try\" style=\"position:relative;\"><a href=\"#initial-try\" aria-label=\"initial try 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>Initial try</h2>\n<p>I started by running:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"1711596810571270000\"\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(`git reflog`, `1711596810571270000`)\"\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=\"powershell\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-powershell line-numbers\"><code class=\"language-powershell\">git reflog</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>I saw anything but my commits which expected because that commit command never had happened due to the files that weren’t added.</p>\n<h2 id=\"checking-stash\" style=\"position:relative;\"><a href=\"#checking-stash\" aria-label=\"checking stash 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 stash</h2>\n<p>Next thing I checked was my stashed files:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"79348230669002240000\"\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(`git stash list`, `79348230669002240000`)\"\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=\"powershell\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-powershell line-numbers\"><code class=\"language-powershell\">git stash list</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 showed me there were two sets of stashed files.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"17759798570379858000\"\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(`stash@{0}: WIP on elmah: 82eb0a4 Merged PR 426: Fixed pass change page\r\nstash@{1}: WIP on availability: 87b2264 Dashboard is now grouping based on hospital category, has got a filter and a search box to search for hospitals`, `17759798570379858000`)\"\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=\"powershell\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-powershell line-numbers\"><code class=\"language-powershell\">stash@<span class=\"token punctuation\">{</span>0<span class=\"token punctuation\">}</span>: WIP on elmah: 82eb0a4 Merged PR 426: Fixed pass change page\r\nstash@<span class=\"token punctuation\">{</span>1<span class=\"token punctuation\">}</span>: WIP on availability: 87b2264 Dashboard is now grouping based on hospital category<span class=\"token punctuation\">,</span> has got a <span class=\"token keyword\">filter</span> and a search box to search <span class=\"token keyword\">for</span> hospitals</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>Then I looked into each of these with <code class=\"language-text\">git stash show stash@{0}</code> and <code class=\"language-text\">git stash show stash@{1}</code> but I didn’t see any of those.</p>\n<h2 id=\"checking-git-database\" style=\"position:relative;\"><a href=\"#checking-git-database\" aria-label=\"checking git database 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 git database</h2>\n<p>At this point I realised that I needed some help and I contacted Scott one of our <code class=\"language-text\">git</code> gurus to help me fine out what happened. He suggested we start by checking the database for any lost commit using:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"64168742395750940000\"\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(`git fsck --lost-found`, `64168742395750940000`)\"\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=\"powershell\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-powershell line-numbers\"><code class=\"language-powershell\">git fsck <span class=\"token operator\">--</span>lost-found</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>From the documentation:</p>\n<blockquote>\n<p><strong>—lost-found:</strong></p>\n</blockquote>\n<p>Write dangling objects into .git/lost-found/commit/ or .git/lost-found/other/, depending on type. If the object is a blob, the contents are written into the file, rather than its object name.</p>\n<p>This showed me a bunch of stuff including some dangling commits.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"25840577999049953000\"\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(`Checking object directories: 100% (256/256), done.\r\nChecking objects: 100% (4282/4282), done.\r\ndangling tree 484106954fff79e5a4d7b8fd53fca05311e56e79\r\ndangling blob d10124a92b82ccdcb1521ef814f815b75223e0f3\r\ndangling commit 4c8288a8c30d4171c4935982e18ebd842a2c2811\r\ndangling tree ae02e6d9b291775c9bc1eb1489ba341f8b033f48\r\ndangling commit 2ec37c71f59766d55c09830a5b6e2c5e1c741a95\r\ndangling blob 40c3c7ccbd197c78ceddf71c2be01b0db2114ba9\r\ndangling tag a70363ef35f023e57e00fc625bee362c2e9e4bc9\r\ndangling commit 1184e4a583e3d0f9cf9f3cb994a3375b16d122cd`, `25840577999049953000`)\"\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=\"powershell\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-powershell line-numbers\"><code class=\"language-powershell\">Checking object directories: 100% <span class=\"token punctuation\">(</span>256/256<span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span> done<span class=\"token punctuation\">.</span>\r\nChecking objects: 100% <span class=\"token punctuation\">(</span>4282/4282<span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span> done<span class=\"token punctuation\">.</span>\r\ndangling tree 484106954fff79e5a4d7b8fd53fca05311e56e79\r\ndangling blob d10124a92b82ccdcb1521ef814f815b75223e0f3\r\ndangling commit 4c8288a8c30d4171c4935982e18ebd842a2c2811\r\ndangling tree ae02e6d9b291775c9bc1eb1489ba341f8b033f48\r\ndangling commit 2ec37c71f59766d55c09830a5b6e2c5e1c741a95\r\ndangling blob 40c3c7ccbd197c78ceddf71c2be01b0db2114ba9\r\ndangling tag a70363ef35f023e57e00fc625bee362c2e9e4bc9\r\ndangling commit 1184e4a583e3d0f9cf9f3cb994a3375b16d122cd</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>So we started to go through some of these to find out which one contained the files I was after.</p>\n<p>We checked out a few of those commits and couldn’t find what we were after. Then he suggested we check those dangling blobs which technically are a tree object containing a snapshot of everything in your current branch.</p>\n<h2 id=\"checking-git-dangling-blobs\" style=\"position:relative;\"><a href=\"#checking-git-dangling-blobs\" aria-label=\"checking git dangling blobs 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 git dangling blobs</h2>\n<p>You cannot checkout those objects, instead you need to use below command where the last part is the hash code of the blob you’re looking into:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"50342731481156714000\"\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(` git ls-tree -r d134e9e`, `50342731481156714000`)\"\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=\"powershell\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-powershell line-numbers\"><code class=\"language-powershell\"> git <span class=\"token function\">ls</span><span class=\"token operator\">-</span>tree <span class=\"token operator\">-</span>r d134e9e</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>You can copy this from the output of the previous command and you don’t need the whole hash value, just a few from the beginning.</p>\n<p>The output of this command looks like this:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"30164224654152606000\"\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(`100644 blob ea25f3a9fe6094d700588f33c340d32281a7614b    src/ARV.Database/ARV.Database.sqlproj.orig\r\n100644 blob 2e47fe1d11bd2624065866591af00f80f16e5e1e    src/ARV.Web.Reach/Partials/hird/incident.form.campuses.list.html\r\n100644 blob 2a6b2547ace20f6bbda788dc4e97e4a749281185    src/ARV.Web.Reach/Scripts/app/core/filters.js\r\n100644 blob a366e78d88695f3c1a2b5617ec03fd24387f2323    src/ARV.Web.Reach/Scripts/app/hird/incident.form.controller.js\r\n100644 blob 461c0e11c1ad64a0bee7bb7f57ba9a99314b1510    src/Arv.Cases.External/App_Data/Arv.Cases.External.XML\r\n100644 blob 276ace5a9dae6be5613beebedcfbf5a64f8e4c4b    src/Arv.Services/Dtos/ReferenceDataDto.cs.orig\r\n100644 blob 5336a10e14295222f28ef087643ac6696dc3cb73    src/Arv.Services/Reach/IIncidents.cs\r\n100644 blob 6e186554f4de5347a754d39ebca1dd8867b7eb0a    src/Arv.Services/Reach/Incidents.cs\r\n100644 blob cf70ac7b4af8f9d8e89c2d84e45a23bdaf7c99de    src/Arv.Services/ReferenceData.cs.orig\r\n100644 blob 502e1286886624c6e0d46b9f65886fbc38b86bca    src/Arv.Web.Core/Controllers/Reach/IncidentController.cs\r\n100644 blob 5ef3891af8afa82dd7cdfc45058a6186e4656c34    src/Arv.Web.Core/Controllers/ReferenceController.cs\r\n100644 blob 49e5d63ba2e59c6389c21c24b81809326711fd27    src/Arv.Web.Core/Controllers/ReferenceController.cs.orig`, `30164224654152606000`)\"\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=\"powershell\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-powershell line-numbers\"><code class=\"language-powershell\">100644 blob ea25f3a9fe6094d700588f33c340d32281a7614b    src/ARV<span class=\"token punctuation\">.</span>Database/ARV<span class=\"token punctuation\">.</span>Database<span class=\"token punctuation\">.</span>sqlproj<span class=\"token punctuation\">.</span>orig\r\n100644 blob 2e47fe1d11bd2624065866591af00f80f16e5e1e    src/ARV<span class=\"token punctuation\">.</span>Web<span class=\"token punctuation\">.</span>Reach/Partials/hird/incident<span class=\"token punctuation\">.</span>form<span class=\"token punctuation\">.</span>campuses<span class=\"token punctuation\">.</span>list<span class=\"token punctuation\">.</span>html\r\n100644 blob 2a6b2547ace20f6bbda788dc4e97e4a749281185    src/ARV<span class=\"token punctuation\">.</span>Web<span class=\"token punctuation\">.</span>Reach/Scripts/app/core/filters<span class=\"token punctuation\">.</span>js\r\n100644 blob a366e78d88695f3c1a2b5617ec03fd24387f2323    src/ARV<span class=\"token punctuation\">.</span>Web<span class=\"token punctuation\">.</span>Reach/Scripts/app/hird/incident<span class=\"token punctuation\">.</span>form<span class=\"token punctuation\">.</span>controller<span class=\"token punctuation\">.</span>js\r\n100644 blob 461c0e11c1ad64a0bee7bb7f57ba9a99314b1510    src/Arv<span class=\"token punctuation\">.</span>Cases<span class=\"token punctuation\">.</span>External/App_Data/Arv<span class=\"token punctuation\">.</span>Cases<span class=\"token punctuation\">.</span>External<span class=\"token punctuation\">.</span>XML\r\n100644 blob 276ace5a9dae6be5613beebedcfbf5a64f8e4c4b    src/Arv<span class=\"token punctuation\">.</span>Services/Dtos/ReferenceDataDto<span class=\"token punctuation\">.</span>cs<span class=\"token punctuation\">.</span>orig\r\n100644 blob 5336a10e14295222f28ef087643ac6696dc3cb73    src/Arv<span class=\"token punctuation\">.</span>Services/Reach/IIncidents<span class=\"token punctuation\">.</span>cs\r\n100644 blob 6e186554f4de5347a754d39ebca1dd8867b7eb0a    src/Arv<span class=\"token punctuation\">.</span>Services/Reach/Incidents<span class=\"token punctuation\">.</span>cs\r\n100644 blob cf70ac7b4af8f9d8e89c2d84e45a23bdaf7c99de    src/Arv<span class=\"token punctuation\">.</span>Services/ReferenceData<span class=\"token punctuation\">.</span>cs<span class=\"token punctuation\">.</span>orig\r\n100644 blob 502e1286886624c6e0d46b9f65886fbc38b86bca    src/Arv<span class=\"token punctuation\">.</span>Web<span class=\"token punctuation\">.</span>Core/Controllers/Reach/IncidentController<span class=\"token punctuation\">.</span>cs\r\n100644 blob 5ef3891af8afa82dd7cdfc45058a6186e4656c34    src/Arv<span class=\"token punctuation\">.</span>Web<span class=\"token punctuation\">.</span>Core/Controllers/ReferenceController<span class=\"token punctuation\">.</span>cs\r\n100644 blob 49e5d63ba2e59c6389c21c24b81809326711fd27    src/Arv<span class=\"token punctuation\">.</span>Web<span class=\"token punctuation\">.</span>Core/Controllers/ReferenceController<span class=\"token punctuation\">.</span>cs<span class=\"token punctuation\">.</span>orig</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>After checking a few of these blobs, I finally found the one which contained a version of the file close to the finishing state (yes I had to redo some of my work, but now I had to spend half an hour instead of a day).</p>\n<h2 id=\"checking-the-file\" style=\"position:relative;\"><a href=\"#checking-the-file\" aria-label=\"checking the file 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 the file</h2>\n<p>Once we found the blob, we had to check what’s the content of those files. You can do that using:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"78857886987834380000\"\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(`git cat-file -p cf70ac7b`, `78857886987834380000`)\"\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=\"powershell\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-powershell line-numbers\"><code class=\"language-powershell\">git <span class=\"token function\">cat</span><span class=\"token operator\">-</span>file <span class=\"token operator\">-</span>p cf70ac7b</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>Now you can see the content printed out to the console. But if the file is bit enough like my situation, you could just pipe this command to write it to a file like so:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"91095464579884970000\"\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(`git cat-file -p cf70ac7b | out-file temp.js`, `91095464579884970000`)\"\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=\"powershell\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-powershell line-numbers\"><code class=\"language-powershell\">git <span class=\"token function\">cat</span><span class=\"token operator\">-</span>file <span class=\"token operator\">-</span>p cf70ac7b <span class=\"token punctuation\">|</span> <span class=\"token function\">out-file</span> temp<span class=\"token punctuation\">.</span>js</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>If you’re using <em>linux</em> or <em>Mac</em>, just use your version of the equivalent.</p>\n<p>And that’s it, repeat this until you have all of the files you need.</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 few lessons I learnt from this accident:</p>\n<ul>\n<li>\n<p>Always check you have added all your files before committing (I know it sounds obvious, but it could happen to you the way it happened to me).</p>\n</li>\n<li>\n<p>Always make sure you commit your changes often after each set of critical change.</p>\n</li>\n<li>\n<p>This one has become my preference now. Rebase from your local branch and not upstream:</p>\n</li>\n</ul>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"41799620345000260000\"\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(`git add .\r\n\r\ngit commit -m &quot;Your awesome and informative commit message&quot;\r\n\r\ngit checkout master\r\n\r\ngit pull\r\n\r\ngit checkout feature-branch\r\n\r\ngit rebase master`, `41799620345000260000`)\"\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=\"powershell\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-powershell line-numbers\"><code class=\"language-powershell\">git add <span class=\"token punctuation\">.</span>\r\n\r\ngit commit <span class=\"token operator\">-</span>m <span class=\"token string\">\"Your awesome and informative commit message\"</span>\r\n\r\ngit checkout master\r\n\r\ngit pull\r\n\r\ngit checkout feature-branch\r\n\r\ngit rebase master</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<ul>\n<li>And if you lost your code, don’t give up, <code class=\"language-text\">git</code> is a really powerful tool.</li>\n</ul>\n<p>Hope this helps someone to save some time in case of a similar accident.</p>","timeToRead":6,"frontmatter":{"title":"How I recovered a day's worth of work with git 🏥","unformattedDate":"2020-04-05T00:00:00.000Z","date":"Apr 05, 2020","path":"/git-logs/","author":"Yaser Adel Mehraban","tags":["showdev","git","recovery"],"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=\"44808055899506320000\"\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}`, `44808055899506320000`)\"\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=\"67479658660356325000\"\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.`, `67479658660356325000`)\"\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":"38f38c9e-3667-53ff-ab7d-4c474b19875c","html":"<p>I help some of my friends perform code reviews on their code bases from time to time as a side activity. I’ve used <a href=\"https://www.npmjs.com/package/codelyzer\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">codelyzer</code></a> before and it’s very similar to <code class=\"language-text\">tslint</code> in a sense. However, I wanted to test something new and thought let’s give <a href=\"https://www.sonarqube.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">SonarQube</a> a shot this time.</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://www.sonarqube.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">SonarQube</a>® is an automatic code review tool to detect bugs, vulnerabilities and code smells in your code. It can integrate with your existing workflow to enable continuous code inspection across your project branches and pull requests.</p>\n<p>It has a server component where you can create projects and apply configurations like adding plugins and updating existing ones. And then there is a scanner component where it scans your code base and sends the result to the server to show in the <em>SonarQube</em> dashboard.</p>\n<p>There are multiple versions of <em>SonarQube</em> but we’re going to use the <a href=\"https://www.sonarqube.org/community/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">community edition</a> which is free and open source.</p>\n<p>So without further ado, let’s get to it.</p>\n<h2 id=\"setup\" style=\"position:relative;\"><a href=\"#setup\" aria-label=\"setup 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>Setup</h2>\n<p><a href=\"https://docs.sonarqube.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">The documentation</a> is really great, and there is a great <a href=\"https://docs.sonarqube.org/latest/setup/overview/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">two minute get started</a> which is what I used to get my hand wet real quick. However, soon I realised that I have to install a lot of dependencies to get it working.</p>\n<p>I was thinking how to avoid the extra installations, when suddenly a smile came to my face when I saw there is a docker image as well.</p>\n<p>On the <a href=\"https://hub.docker.com/_/sonarqube/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Docker Hub</a>, you can find the information you need to get started, but for the purpose of this article, I just use a single command to get it going.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"11652155895881378000\"\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(`docker run -d --name sonarqube -p 9000:9000 sonarqube`, `11652155895881378000`)\"\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 function\">docker</span> run <span class=\"token parameter variable\">-d</span> <span class=\"token parameter variable\">--name</span> sonarqube <span class=\"token parameter variable\">-p</span> <span class=\"token number\">9000</span>:9000 sonarqube</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 command will pull the image down and create a container from it. Run the container in the background mode and then exposes the port 9000 through to host.</p>\n<p>Once done, you need to wait for a few mistunes for the server to get started. You can open a browser of your choice and go to <code class=\"language-text\">localhost:9000</code> to see the server dashboard.</p>\n<p>Use the default username as <code class=\"language-text\">admin</code> and the password <code class=\"language-text\">admin</code> to login.</p>\n<p><img src=\"/9572ec987eacfab62a0913acfc3f9a05/sonarhome.jpg\" alt=\"SonarQube home page\"></p>\n<h2 id=\"create-a-project\" style=\"position:relative;\"><a href=\"#create-a-project\" aria-label=\"create a project 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>Create a project</h2>\n<p>I don’t want to go deep into <em>SonarQube</em> here, but a project represents a code base where you can see the result of a scan done by the scanner. So once logged in, click on create a new project button in the centre of the page, then enter a unique project key and a display name.</p>\n<p>On the next page enter a name for your project token. This is the key which scanner will use to authenticate to the server and send the result of a scan up.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 502px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/06fd8f70fa3859909660ea319638ea41/6f76c/newproj.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: 69.25925925925927%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAOABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAEF/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAAB3aoUf//EABYQAQEBAAAAAAAAAAAAAAAAABABEf/aAAgBAQABBQLGP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEABj8CX//EABkQAAIDAQAAAAAAAAAAAAAAAAERACAxUf/aAAgBAQABPyFOQIYKv//aAAwDAQACAAMAAAAQUA//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/ED//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAbEAEAAgIDAAAAAAAAAAAAAAABABEQcUFhkf/aAAgBAQABPxA5Q6GAaQNS9+SjaFV1n//Z'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"New Project screen\"\n        title=\"\"\n        src=\"/static/06fd8f70fa3859909660ea319638ea41/6f76c/newproj.jpg\"\n        srcset=\"/static/06fd8f70fa3859909660ea319638ea41/6f81f/newproj.jpg 270w,\n/static/06fd8f70fa3859909660ea319638ea41/6f76c/newproj.jpg 502w\"\n        sizes=\"(max-width: 502px) 100vw, 502px\"\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>Take note of this key as you can never see it once you’re navigated away from this page. Click continue and you’ll see a screen where you can specify your project configuration.</p>\n<p>I wanted to run this on an Angular project with TypeScript, so I chose other, and then windows from the OS prompt.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 605px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/86a93b55b1d4586818b60c320e2b8049/3cb18/projectconfig.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: 63.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAECBf/EABUBAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAHtN1KF/8QAGBAAAgMAAAAAAAAAAAAAAAAAAAERICL/2gAIAQEAAQUCbg3X/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAFxAAAwEAAAAAAAAAAAAAAAAAABEgQv/aAAgBAQAGPwJGZ//EABsQAAEFAQEAAAAAAAAAAAAAAAEAEBEhMWGR/9oACAEBAAE/IQFaCcpZ3wWFkqOt/9oADAMBAAIAAwAAABCfz//EABURAQEAAAAAAAAAAAAAAAAAABEQ/9oACAEDAQE/EBn/xAAVEQEBAAAAAAAAAAAAAAAAAAAQEf/aAAgBAgEBPxCn/8QAGRABAAMBAQAAAAAAAAAAAAAAAQARMWEh/9oACAEBAAE/EBpOIquQMuMDPZYVpUEOoT//2Q=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"project config screen\"\n        title=\"\"\n        src=\"/static/86a93b55b1d4586818b60c320e2b8049/3cb18/projectconfig.jpg\"\n        srcset=\"/static/86a93b55b1d4586818b60c320e2b8049/6f81f/projectconfig.jpg 270w,\n/static/86a93b55b1d4586818b60c320e2b8049/09d21/projectconfig.jpg 540w,\n/static/86a93b55b1d4586818b60c320e2b8049/3cb18/projectconfig.jpg 605w\"\n        sizes=\"(max-width: 605px) 100vw, 605px\"\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=\"downloading-the-scanner\" style=\"position:relative;\"><a href=\"#downloading-the-scanner\" aria-label=\"downloading the scanner 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>Downloading the scanner</h2>\n<p>At this point you need to download the scanner and unzip it in a folder named <code class=\"language-text\">sonarqube</code> on your drive. I usually use <code class=\"language-text\">c:\\tools</code> for these sort of usage (replace this with what you used if you chose to unzip it elsewhere).</p>\n<p>Once done, open your scanner config file named <code class=\"language-text\">sonar-scanner.properties</code> from <code class=\"language-text\">c:\\tools\\sonarqube\\config</code> folder and uncomment the line which specifies the server address.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"1397624685029708000\"\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(`#Configure here general information about the environment, such as SonarQube server connection details for example\r\n#No information about specific project should appear here\r\n\r\n#----- Default SonarQube server\r\nsonar.host.url=http://localhost:9000\r\n\r\n#----- Default source code encoding\r\nsonar.sourceEncoding=UTF-8`, `1397624685029708000`)\"\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=\"yaml\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-yaml line-numbers\"><code class=\"language-yaml\"><span class=\"token comment\">#Configure here general information about the environment, such as SonarQube server connection details for example</span>\r\n<span class=\"token comment\">#No information about specific project should appear here</span>\r\n\r\n<span class=\"token comment\">#----- Default SonarQube server</span>\r\nsonar.host.url=http<span class=\"token punctuation\">:</span>//localhost<span class=\"token punctuation\">:</span><span class=\"token number\">9000</span>\r\n\r\n<span class=\"token comment\">#----- Default source code encoding</span>\r\nsonar.sourceEncoding=UTF<span class=\"token punctuation\">-</span><span class=\"token number\">8</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=\"running-a-scan\" style=\"position:relative;\"><a href=\"#running-a-scan\" aria-label=\"running a scan 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>Running a scan</h2>\n<p>Once done, you’re ready to go. Run the command below from your project root and wait for the scan to finish.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"76854763690823290000\"\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(`C:\\tools\\sonar\\bin\\sonar-scanner.bat -D&quot;sonar.projectKey=fp&quot; -D&quot;sonar.sources=.&quot; -D&quot;sonar.host.url=http://localhost:9000&quot; -D&quot;sonar.login=**********************************&quot; -D&quot;sonar.exclusions=node_modules/**/*&quot;`, `76854763690823290000`)\"\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=\"powershell\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-powershell line-numbers\"><code class=\"language-powershell\">C:\\tools\\sonar\\bin\\sonar-scanner<span class=\"token punctuation\">.</span>bat <span class=\"token operator\">-</span>D<span class=\"token string\">\"sonar.projectKey=fp\"</span> <span class=\"token operator\">-</span>D<span class=\"token string\">\"sonar.sources=.\"</span> <span class=\"token operator\">-</span>D<span class=\"token string\">\"sonar.host.url=http://localhost:9000\"</span> <span class=\"token operator\">-</span>D<span class=\"token string\">\"sonar.login=**********************************\"</span> <span class=\"token operator\">-</span>D<span class=\"token string\">\"sonar.exclusions=node_modules/**/*\"</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>Don’t forget to replace the key you got from the previous step in the <code class=\"language-text\">sonar.login</code> argument.</p>\n<blockquote>\n<p>💡 In the last step when you downloaded the scanner zip file, the command you need to execute is written down for you ready to go, just add the exclude folder to avoid code analysis on external libraries.</p>\n</blockquote>\n<p>It takes a while for the scan to finish based on the size of your code base. But once done, you can see the result in the <em>SonarQube</em> dashboard.</p>\n<blockquote>\n<p>ℹ️ Remember to run <code class=\"language-text\">npm install</code> or <code class=\"language-text\">yarn</code> if you’ve just forked the code base from your <code class=\"language-text\">git</code> repository. I faced a few errors before I realised I didn’t have those installed and they are used by the scanner.</p>\n</blockquote>\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/29df73690949903c226b2735ef7d93db/9288f/scan.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: 40.74074074074074%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAIABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAIF/8QAFQEBAQAAAAAAAAAAAAAAAAAAAQD/2gAMAwEAAhADEAAAAciBJBf/xAAWEAADAAAAAAAAAAAAAAAAAAAAARD/2gAIAQEAAQUCHP/EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEABj8Cf//EABYQAAMAAAAAAAAAAAAAAAAAAAAQEf/aAAgBAQABPyFIf//aAAwDAQACAAMAAAAQ9D//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/ED//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAZEAACAwEAAAAAAAAAAAAAAAAAAREhQVH/2gAIAQEAAT8QbqC2EuH/2Q=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"scan in progress\"\n        title=\"\"\n        src=\"/static/29df73690949903c226b2735ef7d93db/47311/scan.jpg\"\n        srcset=\"/static/29df73690949903c226b2735ef7d93db/6f81f/scan.jpg 270w,\n/static/29df73690949903c226b2735ef7d93db/09d21/scan.jpg 540w,\n/static/29df73690949903c226b2735ef7d93db/47311/scan.jpg 1080w,\n/static/29df73690949903c226b2735ef7d93db/0047d/scan.jpg 1620w,\n/static/29df73690949903c226b2735ef7d93db/274e1/scan.jpg 2160w,\n/static/29df73690949903c226b2735ef7d93db/9288f/scan.jpg 2626w\"\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>And once done, here is yow your dashboard will look like. Obviously the code base I was reviewing was a bit chaotic.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 892px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/303844ddea00bcb177f38ea50a1773ee/65069/scanresult.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.7037037037037%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAARABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAEF/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAAB3aAAFB//xAAUEAEAAAAAAAAAAAAAAAAAAAAw/9oACAEBAAEFAh//xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAEDAQE/AR//xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAECAQE/AR//xAAUEAEAAAAAAAAAAAAAAAAAAAAw/9oACAEBAAY/Ah//xAAaEAABBQEAAAAAAAAAAAAAAAABABARIDFR/9oACAEBAAE/IYFuI43/2gAMAwEAAgADAAAAEADHAP/EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQMBAT8QH//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8QH//EACAQAAEEAAcBAAAAAAAAAAAAAAEAESExEEFxgZGx0fD/2gAIAQEAAT8QAi7FzFpmvtNrygBHiymNl9SN4j//2Q=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"scan results\"\n        title=\"\"\n        src=\"/static/303844ddea00bcb177f38ea50a1773ee/65069/scanresult.jpg\"\n        srcset=\"/static/303844ddea00bcb177f38ea50a1773ee/6f81f/scanresult.jpg 270w,\n/static/303844ddea00bcb177f38ea50a1773ee/09d21/scanresult.jpg 540w,\n/static/303844ddea00bcb177f38ea50a1773ee/65069/scanresult.jpg 892w\"\n        sizes=\"(max-width: 892px) 100vw, 892px\"\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>And you when you click on issues you can see them by category and also the rules used to perform the scan.</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/50e9086321a0ebdc46828894b5557fd9/3bd77/errordetails.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: 68.14814814814815%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAOABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAEF/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAH/2gAMAwEAAhADEAAAAduxKB//xAAUEAEAAAAAAAAAAAAAAAAAAAAg/9oACAEBAAEFAl//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/AT//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAUEAEAAAAAAAAAAAAAAAAAAAAg/9oACAEBAAY/Al//xAAaEAACAgMAAAAAAAAAAAAAAAAAARARITFx/9oACAEBAAE/IeCsyPSFH//aAAwDAQACAAMAAAAQGz//xAAWEQEBAQAAAAAAAAAAAAAAAAAAARH/2gAIAQMBAT8QkY//xAAVEQEBAAAAAAAAAAAAAAAAAAABEP/aAAgBAgEBPxBn/8QAHBABAAIBBQAAAAAAAAAAAAAAAQARIRAxQZHh/9oACAEBAAE/EGwHBuJmb6jl5pNk5Z//2Q=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"scan error details\"\n        title=\"\"\n        src=\"/static/50e9086321a0ebdc46828894b5557fd9/47311/errordetails.jpg\"\n        srcset=\"/static/50e9086321a0ebdc46828894b5557fd9/6f81f/errordetails.jpg 270w,\n/static/50e9086321a0ebdc46828894b5557fd9/09d21/errordetails.jpg 540w,\n/static/50e9086321a0ebdc46828894b5557fd9/47311/errordetails.jpg 1080w,\n/static/50e9086321a0ebdc46828894b5557fd9/3bd77/errordetails.jpg 1171w\"\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=\"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>In terms of the speed, I was very happy with the result. It took nearly 2 minutes to scan my entire code base with more than 520 files and thousands line of code.</p>\n<p>In term of the scan result, I would say based on the default rule set and considering that this was the community edition, the results are slightly better than <code class=\"language-text\">tslint</code> or <code class=\"language-text\">codelyzer</code>. I could easily navigate through issues, find out what line of code needed attention. It showed me the results with syntax highlighting and a lo of categories and tags where I could drill down and find what I wanted quickly.</p>\n<p>So go ahead and give it a shot, I am sure you’ll find enough to keep you busy for a while to fix those. And bare in mind that it not only gives you linting errors, it will also give you some security hints based on <a href=\"https://en.wikipedia.org/wiki/OWASP\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">OWASP top 10</a> as well.</p>","timeToRead":4,"frontmatter":{"title":"Run code analysis with sonarqube using docker","unformattedDate":"2020-02-23T00:00:00.000Z","date":"Feb 23, 2020","path":"/sonarqube-docker/","author":"Yaser Adel Mehraban","tags":["webdev","showdev","codeanalysis","docker"],"thumbnail":null}}},{"node":{"id":"0940bc08-c5d1-5d44-ab8a-03a8e8bded05","html":"<p>One of the first things I learnt to do with HTML was using marquee to move text across the screen horizontally. It was so cool to be able to create advertisements on a page and make users notice things with just a tag. Of course people overused it and then it became a out of fashion item in the new designs just like baggy jeans from 90s.</p>\n<!--more-->\n<h2 id=\"history\" style=\"position:relative;\"><a href=\"#history\" aria-label=\"history 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>History</h2>\n<p>The HTML <code class=\"language-text\">&lt;marquee></code> element was used to insert a scrolling area of text. You could control what happened when the text reached the edge of it’s area using its attribute. Of course when I use past tense it doesn’t mean it’s not supported anymore. It became obsolete a while back, but most browsers still support it.</p>\n<p>You can see it in action:</p>\n<iframe height=\"265\" style=\"width: 100%;\" scrolling=\"no\" title=\"Marquee\" src=\"https://codepen.io/yashints/embed/preview/QWbwVKr?height=265&theme-id=default&default-tab=html,result\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\">\r\n  See the Pen <a href='https://codepen.io/yashints/pen/QWbwVKr'>Marquee</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=\"css-marquees\" style=\"position:relative;\"><a href=\"#css-marquees\" aria-label=\"css marquees 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 marquees</h2>\n<p>So I want to simulate some of these with just <code class=\"language-text\">CSS</code>. We’ll be using <code class=\"language-text\">CSS</code> animations and play with <code class=\"language-text\">overflow</code> to simulate text reaching to the edge.</p>\n<h3 id=\"scrolling-text\" style=\"position:relative;\"><a href=\"#scrolling-text\" aria-label=\"scrolling text 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>Scrolling text</h3>\n<p>So let’s play with <code class=\"language-text\">translateX()</code> to specify the content placement at the start and end of the animation. We’ll use this to keep the text moving between start and end.</p>\n<p>For the animation we will use an infinite linear with 10 seconds duration.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"6801035804933542000\"\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(`.marquee {\r\n  height: 50px;\r\n  overflow: hidden;\r\n  position: relative;\r\n}\r\n.marquee span {\r\n  font-size: 2em;\r\n  color: turquoise;\r\n  position: absolute;\r\n  width: 100%;\r\n  height: 100%;\r\n  margin: 0;\r\n  line-height: 50px;\r\n  text-align: center;\r\n  transform:translateX(100%);\r\n  animation: cssmarquee 10s linear infinite;\r\n}\r\n@keyframes cssmarquee {\r\n  0% {\r\n  transform: translateX(100%);\r\n  }\r\n  100% {\r\n  transform: translateX(-100%);\r\n  }\r\n}`, `6801035804933542000`)\"\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\">.marquee</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">height</span><span class=\"token punctuation\">:</span> 50px<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">overflow</span><span class=\"token punctuation\">:</span> hidden<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">position</span><span class=\"token punctuation\">:</span> relative<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n<span class=\"token selector\">.marquee span</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">font-size</span><span class=\"token punctuation\">:</span> 2em<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">color</span><span class=\"token punctuation\">:</span> turquoise<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\">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 property\">margin</span><span class=\"token punctuation\">:</span> 0<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">line-height</span><span class=\"token punctuation\">:</span> 50px<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">text-align</span><span class=\"token punctuation\">:</span> center<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">transform</span><span class=\"token punctuation\">:</span><span class=\"token function\">translateX</span><span class=\"token punctuation\">(</span>100%<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">animation</span><span class=\"token punctuation\">:</span> cssmarquee 10s linear infinite<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n<span class=\"token atrule\"><span class=\"token rule\">@keyframes</span> cssmarquee</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token selector\">0%</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">transform</span><span class=\"token punctuation\">:</span> <span class=\"token function\">translateX</span><span class=\"token punctuation\">(</span>100%<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n  <span class=\"token selector\">100%</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">transform</span><span class=\"token punctuation\">:</span> <span class=\"token function\">translateX</span><span class=\"token punctuation\">(</span>-100%<span class=\"token punctuation\">)</span><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></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 the HTML is simple:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"39737539321404380000\"\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;marquee&quot;>\r\n  <span>Scrolling Text </span>\r\n</div>`, `39737539321404380000`)\"\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>marquee<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>span</span><span class=\"token punctuation\">></span></span>Scrolling Text <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>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 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<iframe height=\"265\" style=\"width: 100%;\" scrolling=\"no\" title=\"CSS-Marquee-ScrollText\" src=\"https://codepen.io/yashints/embed/preview/ZEGYMLw?height=265&theme-id=default&default-tab=css,result\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\">\r\n  See the Pen <a href='https://codepen.io/yashints/pen/ZEGYMLw'>CSS-Marquee-ScrollText</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<h3 id=\"slide-in\" style=\"position:relative;\"><a href=\"#slide-in\" aria-label=\"slide in 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>Slide in</h3>\n<p>Now let’s slide in the text and fix it at the beginning. We’ll use <code class=\"language-text\">ease-out</code> with a <code class=\"language-text\">200%</code> value for <code class=\"language-text\">translateX</code> start and <code class=\"language-text\">0%</code> for the end.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"76121577157112790000\"\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(`.marquee {\r\n  height: 50px;\r\n  overflow: hidden;\r\n  position: relative;\r\n}\r\n.marquee span {\r\n  font-size: 2em;\r\n  color: green;\r\n  position: absolute;\r\n  width: 100%;\r\n  height: 100%;\r\n  margin: 0;\r\n  line-height: 50px;\r\n  text-align: left;\r\n  animation: cssmarquee 10s ease-out;\r\n}\r\n@keyframes cssmarquee {\r\n  0% {\r\n    transform: translateX(200%);\r\n  }\r\n  100% {\r\n    transform: translateX(0%);\r\n  }\r\n}`, `76121577157112790000`)\"\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\">.marquee</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">height</span><span class=\"token punctuation\">:</span> 50px<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">overflow</span><span class=\"token punctuation\">:</span> hidden<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">position</span><span class=\"token punctuation\">:</span> relative<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n<span class=\"token selector\">.marquee span</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">font-size</span><span class=\"token punctuation\">:</span> 2em<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">color</span><span class=\"token punctuation\">:</span> green<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\">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 property\">margin</span><span class=\"token punctuation\">:</span> 0<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">line-height</span><span class=\"token punctuation\">:</span> 50px<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">text-align</span><span class=\"token punctuation\">:</span> left<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">animation</span><span class=\"token punctuation\">:</span> cssmarquee 10s ease-out<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n<span class=\"token atrule\"><span class=\"token rule\">@keyframes</span> cssmarquee</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token selector\">0%</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">transform</span><span class=\"token punctuation\">:</span> <span class=\"token function\">translateX</span><span class=\"token punctuation\">(</span>200%<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n  <span class=\"token selector\">100%</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">transform</span><span class=\"token punctuation\">:</span> <span class=\"token function\">translateX</span><span class=\"token punctuation\">(</span>0%<span class=\"token punctuation\">)</span><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></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>I won’t repeat the <code class=\"language-text\">HTML</code> code again to save some typing 😁. It’s exactly as above.</p>\n<iframe height=\"265\" style=\"width: 100%;\" scrolling=\"no\" title=\"CSS-Marquee-SlineIn\" src=\"https://codepen.io/yashints/embed/preview/wvaBEJp?height=265&theme-id=default&default-tab=css,result\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\">\r\n  See the Pen <a href='https://codepen.io/yashints/pen/wvaBEJp'>CSS-Marquee-SlineIn</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<h3 id=\"left-to-right\" style=\"position:relative;\"><a href=\"#left-to-right\" aria-label=\"left to right 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>Left to right</h3>\n<p>Now let’s move the text from left to right this time. Again, we’ll use a linear infinite animation with the reverse values for <code class=\"language-text\">translateX</code> as our first example.</p>\n<p>The only other difference will be the initial value for transform property.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"27102016006516204000\"\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(`.marquee {\r\n  height: 50px;\r\n  overflow: hidden;\r\n  position: relative;\r\n}\r\n.marquee span {\r\n  font-size: 2em;\r\n  position: absolute;\r\n  width: 100%;\r\n  height: 100%;\r\n  color: green;\r\n  margin: 0;\r\n  line-height: 50px;\r\n  text-align: center;\r\n  /* Starting position */\r\n  transform: translateX(-100%);\r\n  animation: cssmarquee 20s linear infinite;\r\n}\r\n@keyframes cssmarquee {\r\n  0% {\r\n    transform: translateX(-100%);\r\n  }\r\n  100% {\r\n    transform: translateX(100%);\r\n  }\r\n}`, `27102016006516204000`)\"\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\">.marquee</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">height</span><span class=\"token punctuation\">:</span> 50px<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">overflow</span><span class=\"token punctuation\">:</span> hidden<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">position</span><span class=\"token punctuation\">:</span> relative<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n<span class=\"token selector\">.marquee span</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">font-size</span><span class=\"token punctuation\">:</span> 2em<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\">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 property\">color</span><span class=\"token punctuation\">:</span> green<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">margin</span><span class=\"token punctuation\">:</span> 0<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">line-height</span><span class=\"token punctuation\">:</span> 50px<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">text-align</span><span class=\"token punctuation\">:</span> center<span class=\"token punctuation\">;</span>\r\n  <span class=\"token comment\">/* Starting position */</span>\r\n  <span class=\"token property\">transform</span><span class=\"token punctuation\">:</span> <span class=\"token function\">translateX</span><span class=\"token punctuation\">(</span>-100%<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">animation</span><span class=\"token punctuation\">:</span> cssmarquee 20s linear infinite<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n<span class=\"token atrule\"><span class=\"token rule\">@keyframes</span> cssmarquee</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token selector\">0%</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">transform</span><span class=\"token punctuation\">:</span> <span class=\"token function\">translateX</span><span class=\"token punctuation\">(</span>-100%<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n  <span class=\"token selector\">100%</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">transform</span><span class=\"token punctuation\">:</span> <span class=\"token function\">translateX</span><span class=\"token punctuation\">(</span>100%<span class=\"token punctuation\">)</span><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></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<iframe height=\"265\" style=\"width: 100%;\" scrolling=\"no\" title=\"CSS-Marquee-L2R\" src=\"https://codepen.io/yashints/embed/preview/xxGbarb?height=265&theme-id=default&default-tab=css,result\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\">\r\n  See the Pen <a href='https://codepen.io/yashints/pen/xxGbarb'>CSS-Marquee-L2R</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<h3 id=\"scroll-vertically\" style=\"position:relative;\"><a href=\"#scroll-vertically\" aria-label=\"scroll vertically 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 vertically</h3>\n<p>By now you should be comfortable guessing what will be using. Instead of <code class=\"language-text\">translateX</code>, we will be using <code class=\"language-text\">translateY</code>.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"92563872564723000000\"\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(`.marquee {\r\n  height: 100px;\r\n  overflow: hidden;\r\n  position: relative;\r\n}\r\n.marquee span {\r\n  font-size: 2em;\r\n  position: absolute;\r\n  color: green;\r\n  width: 100%;\r\n  height: 100%;\r\n  margin: 0;\r\n  line-height: 50px;\r\n  text-align: center;\r\n  transform: translateY(-100%);\r\n  animation: cssmarquee 5s linear infinite;\r\n}\r\n@keyframes cssmarquee {\r\n  0% {\r\n    transform: translateY(-100%);\r\n  }\r\n  100% {\r\n    transform: translateY(100%);\r\n  }\r\n}`, `92563872564723000000`)\"\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\">.marquee</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">height</span><span class=\"token punctuation\">:</span> 100px<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">overflow</span><span class=\"token punctuation\">:</span> hidden<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">position</span><span class=\"token punctuation\">:</span> relative<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n<span class=\"token selector\">.marquee span</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">font-size</span><span class=\"token punctuation\">:</span> 2em<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\">color</span><span class=\"token punctuation\">:</span> green<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 property\">margin</span><span class=\"token punctuation\">:</span> 0<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">line-height</span><span class=\"token punctuation\">:</span> 50px<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">text-align</span><span class=\"token punctuation\">:</span> center<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">transform</span><span class=\"token punctuation\">:</span> <span class=\"token function\">translateY</span><span class=\"token punctuation\">(</span>-100%<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">animation</span><span class=\"token punctuation\">:</span> cssmarquee 5s linear infinite<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n<span class=\"token atrule\"><span class=\"token rule\">@keyframes</span> cssmarquee</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token selector\">0%</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">transform</span><span class=\"token punctuation\">:</span> <span class=\"token function\">translateY</span><span class=\"token punctuation\">(</span>-100%<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n  <span class=\"token selector\">100%</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">transform</span><span class=\"token punctuation\">:</span> <span class=\"token function\">translateY</span><span class=\"token punctuation\">(</span>100%<span class=\"token punctuation\">)</span><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></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<iframe height=\"265\" style=\"width: 100%;\" scrolling=\"no\" title=\"CSS-Marquee-Down\" src=\"https://codepen.io/yashints/embed/preview/LYVEJjG?height=265&theme-id=default&default-tab=css,result\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\">\r\n  See the Pen <a href='https://codepen.io/yashints/pen/LYVEJjG'>CSS-Marquee-Down</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<h3 id=\"bouncing-text\" style=\"position:relative;\"><a href=\"#bouncing-text\" aria-label=\"bouncing text 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>Bouncing text</h3>\n<p>Now let’s assume back then people wanted to get fancy. We want to bounce the text this time 😃. We will need to add <code class=\"language-text\">alternate</code> to our animation and reduce the delay. We will also add <code class=\"language-text\">trnslateX</code> so that the content doesn’t bounce off the whole page.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"74131573192734230000\"\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(`.marquee {\r\n  height: 200px;\r\n  overflow: hidden;\r\n  position: relative;\r\n}\r\n.marquee span {\r\n  font-size: 2em;\r\n  position: absolute;\r\n  color: turquoise;\r\n  width: 100%;\r\n  height: 100%;\r\n  margin: 0;\r\n  line-height: 50px;\r\n  text-align: center;\r\n  transform: translateY(70%);\r\n  animation: cssmarquee 1s linear infinite alternate;\r\n}\r\n@keyframes cssmarquee {\r\n  0% {\r\n    transform: translateY(70%);\r\n  }\r\n  100% {\r\n    transform: translateY(0%);\r\n  }\r\n}`, `74131573192734230000`)\"\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\">.marquee</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\">overflow</span><span class=\"token punctuation\">:</span> hidden<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">position</span><span class=\"token punctuation\">:</span> relative<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n<span class=\"token selector\">.marquee span</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">font-size</span><span class=\"token punctuation\">:</span> 2em<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\">color</span><span class=\"token punctuation\">:</span> turquoise<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 property\">margin</span><span class=\"token punctuation\">:</span> 0<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">line-height</span><span class=\"token punctuation\">:</span> 50px<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">text-align</span><span class=\"token punctuation\">:</span> center<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">transform</span><span class=\"token punctuation\">:</span> <span class=\"token function\">translateY</span><span class=\"token punctuation\">(</span>70%<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">animation</span><span class=\"token punctuation\">:</span> cssmarquee 1s linear infinite alternate<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n<span class=\"token atrule\"><span class=\"token rule\">@keyframes</span> cssmarquee</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token selector\">0%</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">transform</span><span class=\"token punctuation\">:</span> <span class=\"token function\">translateY</span><span class=\"token punctuation\">(</span>70%<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n  <span class=\"token selector\">100%</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">transform</span><span class=\"token punctuation\">:</span> <span class=\"token function\">translateY</span><span class=\"token punctuation\">(</span>0%<span class=\"token punctuation\">)</span><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></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<iframe height=\"265\" style=\"width: 100%;\" scrolling=\"no\" title=\"CSS-Marquee-Bounce\" src=\"https://codepen.io/yashints/embed/preview/ZEGYMJP?height=265&theme-id=default&default-tab=css,result\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\">\r\n  See the Pen <a href='https://codepen.io/yashints/pen/ZEGYMJP'>CSS-Marquee-Bounce</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<h3 id=\"typewriting-effect\" style=\"position:relative;\"><a href=\"#typewriting-effect\" aria-label=\"typewriting effect 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>Typewriting effect</h3>\n<p>Now this time we want to mix our animation with a bit of stepping to create a typewriter effect. We will be using <code class=\"language-text\">from</code> and <code class=\"language-text\">to</code> for our keyframes. Then use <code class=\"language-text\">steps</code> and <code class=\"language-text\">step-end</code> to simulate the text and blink cursor effect.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"52996967295673780000\"\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  font-family: 'Special Elite', cursive;\r\n  background: #efefef;\r\n  padding-top: 5em;\r\n  display: flex;\r\n  justify-content: center;\r\n}\r\n\r\n.typewriter h1 {\r\n  overflow: hidden; \r\n  border-right: .15em solid green; \r\n  white-space: nowrap;\r\n  margin: 0 auto; \r\n  letter-spacing: .15em; \r\n  animation: \r\n    type 4s steps(50, end),\r\n    blink-cursor .7s step-end infinite;\r\n}\r\n\r\n@keyframes type {\r\n  from { width: 0 }\r\n  to { width: 100% }\r\n}\r\n\r\n@keyframes blink-cursor {\r\n  from, to { border-color: transparent }\r\n  50% { border-color: green; }\r\n}`, `52996967295673780000`)\"\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\">body</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">font-family</span><span class=\"token punctuation\">:</span> <span class=\"token string\">'Special Elite'</span><span class=\"token punctuation\">,</span> cursive<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">background</span><span class=\"token punctuation\">:</span> #efefef<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">padding-top</span><span class=\"token punctuation\">:</span> 5em<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\">justify-content</span><span class=\"token punctuation\">:</span> center<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token selector\">.typewriter h1</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">overflow</span><span class=\"token punctuation\">:</span> hidden<span class=\"token punctuation\">;</span> \r\n  <span class=\"token property\">border-right</span><span class=\"token punctuation\">:</span> .15em solid green<span class=\"token punctuation\">;</span> \r\n  <span class=\"token property\">white-space</span><span class=\"token punctuation\">:</span> nowrap<span class=\"token punctuation\">;</span>\r\n  <span class=\"token property\">margin</span><span class=\"token punctuation\">:</span> 0 auto<span class=\"token punctuation\">;</span> \r\n  <span class=\"token property\">letter-spacing</span><span class=\"token punctuation\">:</span> .15em<span class=\"token punctuation\">;</span> \r\n  <span class=\"token property\">animation</span><span class=\"token punctuation\">:</span> \r\n    type 4s <span class=\"token function\">steps</span><span class=\"token punctuation\">(</span>50<span class=\"token punctuation\">,</span> end<span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\r\n    blink-cursor .7s step-end infinite<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token atrule\"><span class=\"token rule\">@keyframes</span> type</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token selector\">from</span> <span class=\"token punctuation\">{</span> <span class=\"token property\">width</span><span class=\"token punctuation\">:</span> 0 <span class=\"token punctuation\">}</span>\r\n  <span class=\"token selector\">to</span> <span class=\"token punctuation\">{</span> <span class=\"token property\">width</span><span class=\"token punctuation\">:</span> 100% <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token atrule\"><span class=\"token rule\">@keyframes</span> blink-cursor</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token selector\">from, to</span> <span class=\"token punctuation\">{</span> <span class=\"token property\">border-color</span><span class=\"token punctuation\">:</span> transparent <span class=\"token punctuation\">}</span>\r\n  <span class=\"token selector\">50%</span> <span class=\"token punctuation\">{</span> <span class=\"token property\">border-color</span><span class=\"token punctuation\">:</span> green<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>Don’t forget to add the <code class=\"language-text\">Special Elite</code> font from Google fonts.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"32297016668199730000\"\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(`<head>\r\n  <link href=&quot;https://fonts.googleapis.com/css?family=Special+Elite&display=swap&quot; rel=&quot;stylesheet&quot;>\r\n</head>`, `32297016668199730000`)\"\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>head</span><span class=\"token punctuation\">></span></span>\r\n  <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>link</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://fonts.googleapis.com/css?family=Special+Elite&amp;display=swap<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">rel</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>stylesheet<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>head</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<iframe height=\"265\" style=\"width: 100%;\" scrolling=\"no\" title=\"CSS-Marquee-TypeWriter\" src=\"https://codepen.io/yashints/embed/preview/qBdEMxb?height=265&theme-id=default&default-tab=css,result\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\">\r\n  See the Pen <a href='https://codepen.io/yashints/pen/qBdEMxb'>CSS-Marquee-TypeWriter</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=\"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>That was it. You can go back to whatever important job you were doing. A bit of distraction is always good for me and hope this bit was good for you too 😊. Until next time 👋🏼.</p>","timeToRead":4,"frontmatter":{"title":"Retro HTML, let's create marquees 😃","unformattedDate":"2020-02-09T00:00:00.000Z","date":"Feb 09, 2020","path":"/marquee/","author":"Yaser Adel Mehraban","tags":["webdev","showdev","html","marquee"],"thumbnail":null}}}]}},"pageContext":{"limit":10,"skip":30,"numPages":13,"currentPage":4}},"staticQueryHashes":[],"slicesMap":{}}