{"componentChunkName":"component---src-pages-author-author-yaml-id-js","path":"/author/greg-sakai/","result":{"data":{"allMarkdownRemark":{"edges":[{"node":{"id":"0f63d421-ce01-5020-8b12-991083fd5274","html":"<p>You'll learn about JavasScript NaN in this tutorial, how to check whether a value is NaN, and how to effectively handle NaN.</p>\n<h2 id=\"what-is-nan-in-javascript\" style=\"position:relative;\"><a href=\"#what-is-nan-in-javascript\" aria-label=\"what is nan 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>What is NaN in Javascript?</h2>\n<p>NaN, in JavaScript, can be many things. In fact, it can be almost anything, so long as it is <em>Not a Number</em>. Its type is technically “number” (when evaluated with “typeof”), although it stands for <strong>Not a Number</strong>.  </p>\n<h2 id=\"values-in-nan-javascript\" style=\"position:relative;\"><a href=\"#values-in-nan-javascript\" aria-label=\"values in nan 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>Values in NaN Javascript</h2>\n<p>Values can become NaN through a variety of means, which usually involve erroneous math calculations (such as 0/0), or as a result of type coercion, either implicit or explicit.</p>\n<p>A common example is when you run parseInt on a string that starts with an alphabetical character. This isn’t exclusive to parseInt, as it also applies when using explicit coercion with Number(), or with the unary “+” operator.  </p>\n<h2 id=\"how-to-check-nan-in-javascript\" style=\"position:relative;\"><a href=\"#how-to-check-nan-in-javascript\" aria-label=\"how to check nan 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>How to check NaN in Javascript?</h2>\n<p>Before selecting a method for checking for NaN, <em>how should you</em> <strong><em>not</em></strong> <em>check for NaN?</em></p>\n<p>NaN is a bizarre value in JavaScript, as it does not equal itself when compared, either with the loose equality (==) or strict equality (===) operator. NaN is the only value in the entire language which behaves in this manner with regards to comparisons.  </p>\n<p>For example, if parseInt(“a”) returns NaN, then parseInt(“a”) === NaN will return false. This may seem strange, but it makes perfect sense after thinking about what NaN really is.  </p>\n<p>NaN doesn’t tell you what something is, it tells you what it <strong>isn’t</strong>.  </p>\n<p>These two different strings being passed to parseInt() will both return NaN.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">parseInt</span><span class=\"mtk1\">(“</span><span class=\"mtk12\">abc</span><span class=\"mtk1\">”) </span><span class=\"mtk3\">// NaN</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">parseInt</span><span class=\"mtk1\">(“</span><span class=\"mtk12\">def</span><span class=\"mtk1\">”) </span><span class=\"mtk3\">// NaN</span></span></code></pre>\n<p>Both statements return NaN, but are they <em>really</em> the same? Maybe, but it certainly makes sense why JavaScript would disagree, given that they are derived from different string arguments.  </p>\n<p>Here are a few examples of strict inequality comparisons, which demonstrate the inconsistency of NaN.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk7\">2</span><span class=\"mtk1\"> !== </span><span class=\"mtk7\">2</span><span class=\"mtk1\"> </span><span class=\"mtk3\">// false</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">true</span><span class=\"mtk1\"> !== </span><span class=\"mtk4\">true</span><span class=\"mtk1\"> </span><span class=\"mtk3\">// false</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">“</span><span class=\"mtk12\">abc</span><span class=\"mtk1\">” !== “</span><span class=\"mtk12\">abc</span><span class=\"mtk1\">” </span><span class=\"mtk3\">// false</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">...</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">NaN</span><span class=\"mtk1\"> !== </span><span class=\"mtk4\">NaN</span><span class=\"mtk1\"> </span><span class=\"mtk3\">// true</span></span></code></pre>\n<h3 id=\"method-1-isnan-or-numberisnan\" style=\"position:relative;\"><a href=\"#method-1-isnan-or-numberisnan\" aria-label=\"method 1 isnan or numberisnan permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Method 1: isNaN or Number.isNaN</h3>\n<p>JavaScript has a built-in method, appropriately named “isNaN,” which checks for NaN. There is a newer function called Number.isNaN, which is included in the ES2015 spec.  </p>\n<p>The difference between isNaN and Number.isNaN is that isNaN coerces the argument into a number type. To avoid complicated and unexpected outcomes, it is often advised to use the newer, more robust Number.isNaN to avoid these side effects.</p>\n<p>Number.isNaN does not perform any forcible type conversion, so it simply returns the boolean based on the parameter.  </p>\n<p>Here is an example of the difference between the two methods:  </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">isNaN</span><span class=\"mtk1\">(</span><span class=\"mtk4\">undefined</span><span class=\"mtk1\">) </span><span class=\"mtk3\">// true</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk10\">Number</span><span class=\"mtk1\">.</span><span class=\"mtk11\">isNaN</span><span class=\"mtk1\">(</span><span class=\"mtk4\">undefined</span><span class=\"mtk1\">) </span><span class=\"mtk3\">// false</span></span></code></pre>\n<p>isNaN, when passed undefined, returns true because undefined becomes NaN after number coercion. You can test this yourself by running Number(undefined). You will find that it returns NaN.  </p>\n<p>Number.isNaN, on the other hand, returns false. This is because no coercion takes place, and undefined is <em>not</em> NaN, it is simply undefined.  </p>\n<p>It is also important to note that Number.isNaN is a newer (ES2015) <a href=\"https://www.loginradius.com/blog/engineering/16-javascript-hacks-for-optimization/\">method in JavaScript</a>, so browser support for Number.isNaN is not as stable as isNaN, which has been around since ES1 (1997).  </p>\n<h3 id=\"method-2-objectis\" style=\"position:relative;\"><a href=\"#method-2-objectis\" aria-label=\"method 2 objectis permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Method 2: Object.is</h3>\n<p><code>Object.is</code> is a JavaScript method which checks for sameness. It generally performs the same evaluations as a strict equality operator (===), although it treats NaN differently from strict equality.  </p>\n<p><code>Object.is(0, -0)</code> will return false, while 0 === -0 will return true. Comparisons of 0 and -0 differ, as do comparisons of NaN. This concept is called “Same-value-zero equality.”  </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">NaN</span><span class=\"mtk1\"> === </span><span class=\"mtk4\">NaN</span><span class=\"mtk1\"> </span><span class=\"mtk3\">// false</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk10\">Object</span><span class=\"mtk1\">.</span><span class=\"mtk11\">is</span><span class=\"mtk1\">(</span><span class=\"mtk4\">NaN</span><span class=\"mtk1\">, </span><span class=\"mtk4\">NaN</span><span class=\"mtk1\">) </span><span class=\"mtk3\">// true</span></span></code></pre>\n<p><code>Object.is(NaN, NaN)</code> will in fact return true, while we already know that NaN === NaN returns false. That makes this yet another valid way to check if something is not a number.</p>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>Between the given methods of checking for NaN, the most common is to use the global isNaN function, or the ES2015 Number.isNaN method. While method #2 is valid, most people will typically use isNaN or Number.isNaN, which were created specifically for checking for NaN.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n</style>","frontmatter":{"title":"NaN in JavaScript: An Essential Guide","author":{"id":"Greg Sakai","github":null,"avatar":null},"date":"November 22, 2019","updated_date":null,"tags":["JavaScript"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.3333333333333333,"src":"/static/64b659badd7a09c3d955b9c49a7f271f/1f5c5/numbers-1.webp","srcSet":"/static/64b659badd7a09c3d955b9c49a7f271f/61e93/numbers-1.webp 200w,\n/static/64b659badd7a09c3d955b9c49a7f271f/1f5c5/numbers-1.webp 400w","sizes":"(max-width: 400px) 100vw, 400px"}}}},"fields":{"authorId":"Greg Sakai","slug":"/engineering/checking-for-nan-in-javascript/"}}},{"node":{"id":"6df2af8c-e948-5ef1-be16-64e709922921","html":"<p><strong>What is Web Accessibility?</strong></p>\n<p>Web Accessibility (often stylized “a11y”), is the practice of ensuring that your websites and web applications are accessible by people with disabilities of many kinds.</p>\n<p>Some examples of common disabilities which we need to consider are: Blindness and visual impairment, deafness and hearing difficulties, motor skill impairment, and many more.</p>\n<p>The common categories of disabilities include:</p>\n<ul>\n<li>Auditory</li>\n<li>Cognitive, learning, and neurological</li>\n<li>Physical</li>\n<li>Speech</li>\n<li>Visual</li>\n</ul>\n<p><em>This list was taken from the W3C Web Accessibility Initiative (WAI). A link to their resources can be found at the bottom.</em></p>\n<p><strong>What is Semantic HTML? What is its correlation with Web Accessibility?</strong></p>\n<p>HTML semantics refers to conveying meaning through the use of HTML elements. An example of <em>semantic</em> HTML would be using <strong><code>&#x3C;header></code></strong>, <strong><code>&#x3C;main></code></strong>, <strong><code>&#x3C;footer></code></strong>, and other content-specific tags when applicable. <em>Unsemantic</em> HTML would be using <strong><code>&#x3C;div></code></strong> or <strong><code>&#x3C;span></code></strong> tags for every use-case.</p>\n<p><strong>Images and “alt” attributes</strong></p>\n<p>Users who are visually impaired may encounter trouble when the content of a website relies on an image. Luckily, there are precautions that we - as developers - can take to ensure that their user experience does not suffer because they are unable to see images.</p>\n<p>The easiest way to do this is to add an <strong>alt</strong> attribute to <strong>all</strong> of your <strong><code>&#x3C;img></code></strong> tags. This way, screen readers will be able to describe the image to the user. Here are a few suggestions and best practices for writing <strong>alt</strong> text:</p>\n<ul>\n<li>\n<p><strong>Do not include words like “image”, or “picture”.</strong> It is redundant, because the screen reader will notify the user that it’s an image. An exception to this would be if the image is of a painting, or an illustration. In this case, emphasizing that is fine, because you want the user to know that it’s specifically a painting, as opposed to any image.</p>\n<ul>\n<li>Example: <strong>alt=”Painted portrait of Albert Einstein”</strong></li>\n</ul>\n</li>\n<li>\n<p><strong>Always include the alt attribute</strong>, even if you don’t want the screen reader to read it. If you have a decorative image (one that is simply cosmetic, and not necessary for the site), then you can add an <strong>alt</strong> tag and assign it to an empty string. In this case, the image will be ignored by the screen reader. If this is neglected, the screen reader may read the file name instead, which may not be particularly helpful.</p>\n<ul>\n<li>Tip: you can also assign an attribute of <strong>role=”presentation”</strong> for decorative images. This can be done in addition to the alt attribute.</li>\n</ul>\n</li>\n<li><strong>Keep it concise.</strong> While there is no limit for length of <strong>alt</strong> text, some screen readers will cut off the alt text at around 125 characters. Either way, you don’t want to waste the user’s time by having their screen reader read a needlessly long description.</li>\n</ul>\n<p><strong>Form Labels and ARIA Labelling</strong></p>\n<p>Users with screen readers will need some way to differentiate between multiple inputs on a single form. Without labels, the user will have a difficult time figuring out what each input is for.</p>\n<p>The easiest way to implement form labelling is with the HTML <strong><code>&#x3C;label></code></strong> tag. The <strong><code>&#x3C;label></code></strong> element uses an attribute called <code>for</code> in order to form a relationship with the ID of the <strong><code>&#x3C;input></code></strong> tag.</p>\n<p>Another way to give context to a screen reader is with the ARIA <strong><code>aria-labelledby</code></strong> attribute. This attribute can be placed on (but not limited to) <strong><code>&#x3C;input></code></strong> tags, and then reference the <strong>id</strong> of the label. You can use <strong><code>&#x3C;label for=””></code></strong> in conjunction with <strong><code>&#x3C;input aria-labelledby=””></code></strong> in order to provide extra support and maximize compatibility.</p>\n<p><strong>Buttons and Tabindices</strong></p>\n<p>Buttons should use <strong><code>&#x3C;button></code></strong> tags (or <strong><code>&#x3C;input type=”submit”></code></strong> if applicable), rather than <strong><code>&#x3C;a></code></strong>, <strong><code>&#x3C;span></code></strong>, <strong><code>&#x3C;div></code></strong>, or anything else that doesn’t have semantic meaning for clickable buttons. Often people choose to use these tags, because they want to clear all styling and not have default <strong><code>&#x3C;button></code></strong> styling. This is ill-advised because <strong><code>&#x3C;button></code></strong> tags come with a ton of functionality built-in, an important part being its <strong><code>tabindex</code></strong>. A user who isn’t able to use a mouse, and must navigate the site with their keyboard, will press the <strong><code>tab</code></strong> key to jump through interactive elements. Standard <strong><code>&#x3C;div></code></strong> tags are not tabbable by default, and assigning a <strong><code>tabindex</code></strong> to it manually can break the flow of the natural tabindices of the page.</p>\n<p><strong>Other Semantic HTML tags</strong></p>\n<p>Other than images and forms, there are still many ways to provide the best accessibility possible. Semantic HTML tags are useful because they describe the content and the relationship with your overall page. Using appropriate, accurate tags such as <strong><code>&#x3C;article></code></strong>, <strong><code>&#x3C;aside></code></strong>, <strong><code>&#x3C;section></code></strong> will give screen readers an idea of what the role is for those sections.</p>\n<p>Other elements such as <strong><code>&#x3C;h1></code></strong> and other heading tags should be carefully planned before sprinkled throughout your webpage. The <strong><code>&#x3C;h1></code></strong> through <strong><code>&#x3C;h6></code></strong> tags should form direct relationships with each other in order to differentiate their meaning. Headings with lower importance (relative to the previous heading) can be used to start new subsections within its current section. Your different page sections can even use <strong><code>aria-labelledby</code></strong> to point to its corresponding heading in order to label your sections accurately.</p>\n<p>The full W3C specification on HTML semantics can be found at: <a href=\"https://www.w3.org/TR/2016/REC-html51-20161101/dom.html\">W3C Recommendation</a>.</p>\n<p>More information about the varying disabilities and barriers can be found at: <a href=\"https://www.w3.org/WAI/people-use-web/abilities-barriers/\">Diverse Abilities and Barriers</a></p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n</style>","frontmatter":{"title":"Introduction to Web Accessibility with Semantic HTML5","author":{"id":"Greg Sakai","github":null,"avatar":null},"date":"July 24, 2019","updated_date":null,"tags":["HTML5","HTML","Web Accessibility","Semantic HTML5"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1,"src":"/static/70f4300c1f86c6d8e337f40979e66dea/58556/handicapped.webp","srcSet":"/static/70f4300c1f86c6d8e337f40979e66dea/61e93/handicapped.webp 200w,\n/static/70f4300c1f86c6d8e337f40979e66dea/1f5c5/handicapped.webp 400w,\n/static/70f4300c1f86c6d8e337f40979e66dea/58556/handicapped.webp 800w,\n/static/70f4300c1f86c6d8e337f40979e66dea/cc834/handicapped.webp 1024w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"fields":{"authorId":"Greg Sakai","slug":"/engineering/introduction-to-web-accessibility-with-semantic-html5/"}}},{"node":{"id":"41249db1-bbc4-561f-9e26-b45636298819","html":"<p><strong>A basic example of a JavaScript event</strong></p>\n<p>Events, in JavaScript, are occurrences that can trigger certain functionality, and can result in certain behaviour. A common example of an event, is a “click”, or a “hover”. You can set listeners to watch for these events that will trigger your desired functionality.</p>\n<p><strong>What is “propagation”?</strong></p>\n<p>Propagation refers to how events travel through the Document Object Model (DOM) tree. The DOM tree is the structure which contains parent/child/sibling elements in relation to each other. You can think of propagation as electricity running through a wire, until it reaches its destination. The event needs to pass through every node on the DOM until it reaches the end, or if it is forcibly stopped.</p>\n<p><strong>Event Bubbling and Capturing</strong></p>\n<p>Bubbling and Capturing are the two phases of propagation. In their simplest definitions, <strong>bubbling</strong> travels from the <em>target</em> to the <em>root</em>, and <strong>capturing</strong> travels from the <em>root</em> to the <em>target</em>. However, that doesn’t make much sense without first defining what a target and a root is.</p>\n<p>The <em>target</em> is the DOM node on which you click, or trigger with any other event.</p>\n<p>For example, a button with a click event would be the event target.</p>\n<p>The <em>root</em> is the highest-level parent of the target. This is usually the <strong>document</strong>, which is a parent of the <strong><body></strong>, which is a (possibly distant) parent of your target element.</p>\n<p>Capturing is not used nearly as commonly as bubbling, so our examples will revolve around the bubbling phase. As an option though, <strong>EventTarget.addEventListener()</strong> has an optional third parameter - which takes its argument as a boolean - which controls the phase of the propagation. The parameter is called <strong>useCapture</strong>, and passing <strong>true</strong> will cause the listener to be on the capturing phase. The default is <strong>false</strong>, which will apply it to the bubbling phase.</p>\n<p>Once you trigger the event, it will <em>propagate</em> up to the root, and it will trigger every single event handler which is associated with the same type. For example, if your button has a click event, during the bubbling phase, it will bubble up to the root, and trigger every click event along the way.</p>\n<p>This kind of behaviour might not sound desirable - and it often isn’t - but there’s an easy workaround...</p>\n<p><strong>Event.stopPropagation()</strong></p>\n<p>These two methods are used for solving the previously mentioned problem regarding event propagation. Calling <strong>Event.stopPropagation()</strong> will prevent further propagation through the DOM tree, and only run the event handler from which it was called.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">&lt;!--</span><span class=\"mtk12\">Try</span><span class=\"mtk1\"> </span><span class=\"mtk12\">it</span><span class=\"mtk1\">--&gt;</span></span></code></pre>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">function</span><span class=\"mtk1\"> </span><span class=\"mtk11\">first</span><span class=\"mtk1\">() {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk10\">console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">log</span><span class=\"mtk1\">(</span><span class=\"mtk7\">1</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">function</span><span class=\"mtk1\"> </span><span class=\"mtk11\">second</span><span class=\"mtk1\">() {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk10\">console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">log</span><span class=\"mtk1\">(</span><span class=\"mtk7\">2</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">button</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">document</span><span class=\"mtk1\">.</span><span class=\"mtk11\">getElementById</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;button&quot;</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">container</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">document</span><span class=\"mtk1\">.</span><span class=\"mtk11\">getElementById</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;container&quot;</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">button</span><span class=\"mtk1\">.</span><span class=\"mtk11\">addEventListener</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;click&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">first</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">container</span><span class=\"mtk1\">.</span><span class=\"mtk11\">addEventListener</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;click&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">second</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>In this example, clicking the button will cause the console to print <strong>1, 2</strong>. If we wanted to modify this so that <em>only</em> the button’s click event is triggered, we could use <strong>Event.stopPropagation()</strong> to immediately stop the event from bubbling to its parent.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">function</span><span class=\"mtk1\"> </span><span class=\"mtk11\">first</span><span class=\"mtk1\">(</span><span class=\"mtk12\">event</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">event</span><span class=\"mtk1\">.</span><span class=\"mtk11\">stopPropagation</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk10\">console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">log</span><span class=\"mtk1\">(</span><span class=\"mtk7\">1</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p>This modification will allow the console to print <strong>1</strong>, but it will end the event chain right away, preventing it from reaching <strong>2</strong>.</p>\n<p><strong>How does this differ from <code>Event.stopImmediatePropagation()</code>? When would you use either?</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">&lt;!--</span><span class=\"mtk12\">Try</span><span class=\"mtk1\"> </span><span class=\"mtk12\">it</span><span class=\"mtk1\">--&gt;</span></span></code></pre>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">function</span><span class=\"mtk1\"> </span><span class=\"mtk11\">first</span><span class=\"mtk1\">(</span><span class=\"mtk12\">event</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk10\">console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">log</span><span class=\"mtk1\">(</span><span class=\"mtk7\">1</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">function</span><span class=\"mtk1\"> </span><span class=\"mtk11\">second</span><span class=\"mtk1\">() {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk10\">console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">log</span><span class=\"mtk1\">(</span><span class=\"mtk7\">2</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">function</span><span class=\"mtk1\"> </span><span class=\"mtk11\">third</span><span class=\"mtk1\">() {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk10\">console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">log</span><span class=\"mtk1\">(</span><span class=\"mtk7\">3</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">button</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">document</span><span class=\"mtk1\">.</span><span class=\"mtk11\">getElementById</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;button&quot;</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">container</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">document</span><span class=\"mtk1\">.</span><span class=\"mtk11\">getElementById</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;container&quot;</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">button</span><span class=\"mtk1\">.</span><span class=\"mtk11\">addEventListener</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;click&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">first</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">button</span><span class=\"mtk1\">.</span><span class=\"mtk11\">addEventListener</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;click&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">second</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">container</span><span class=\"mtk1\">.</span><span class=\"mtk11\">addEventListener</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;click&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">third</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>Let’s suppose we wanted to add a third function, which prints <strong>3</strong> to the console. In this scenario, we will also move the <strong>second</strong> function to also be on the button. We will apply <strong>third</strong> to the container now.</p>\n<p><strong>Long story short</strong>: we have two event handlers on the button, and clicking <strong>&#x3C;div#container></strong> will now print <strong>3</strong>.</p>\n<p>What will happen now? This will behave the same as before, and it will propagate through the DOM tree, and print <strong>1, 2, 3</strong>, in that order.</p>\n<p><strong>How does this tie in to <code>Event.stopPropagation()</code> and <code>Event.stopImmediatePropagation()</code>?</strong></p>\n<p>Adding <strong>Event.stopPropagation()</strong> to the first function, like so, will print <strong>1, 2</strong> to the console.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">function</span><span class=\"mtk1\"> </span><span class=\"mtk11\">first</span><span class=\"mtk1\">(</span><span class=\"mtk12\">event</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">event</span><span class=\"mtk1\">.</span><span class=\"mtk11\">stopPropagation</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk10\">console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">log</span><span class=\"mtk1\">(</span><span class=\"mtk7\">1</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p>This is because <strong>Event.stopPropagation()</strong> will immediately prevent all click events on the <em>parent</em> from being triggered, but it does <strong>not</strong> stop any <strong>other</strong> event handlers from being called.</p>\n<p>As you can see in our example, our button has two click handlers, and it <em>did</em> stop the propagation to its parents. If you wanted to stop <em>all</em> event handlers after the first, <em>that’s where</em> <strong>Event.stopImmediatePropagation()</strong> <em>comes in.</em></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">function</span><span class=\"mtk1\"> </span><span class=\"mtk11\">first</span><span class=\"mtk1\">(</span><span class=\"mtk12\">event</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">event</span><span class=\"mtk1\">.</span><span class=\"mtk11\">stopImmediatePropagation</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk10\">console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">log</span><span class=\"mtk1\">(</span><span class=\"mtk7\">1</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p>Now, the first function <em>really will</em> stop the parent click handlers, and all other click handlers. The console will now print just <strong>1</strong>, whereas if we simply used <strong>Event.stopPropagation()</strong>, it would print <strong>1, 2</strong>, and not including either would print <strong>1, 2, 3</strong> .</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n</style>","frontmatter":{"title":"JavaScript Events: Bubbling, Capturing, and Propagation","author":{"id":"Greg Sakai","github":null,"avatar":null},"date":"July 22, 2019","updated_date":null,"tags":["JavaScript","JavaScript Events"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.3333333333333333,"src":"/static/53a3895e9c38870937a9136813ac9e10/0ce14/bubbles.webp","srcSet":"/static/53a3895e9c38870937a9136813ac9e10/61e93/bubbles.webp 200w,\n/static/53a3895e9c38870937a9136813ac9e10/1f5c5/bubbles.webp 400w,\n/static/53a3895e9c38870937a9136813ac9e10/0ce14/bubbles.webp 772w","sizes":"(max-width: 772px) 100vw, 772px"}}}},"fields":{"authorId":"Greg Sakai","slug":"/engineering/javascript-events-bubbling-capturing-and-propagation/"}}}]},"authorYaml":{"id":"Greg Sakai","bio":"Greg Sakai is a Software Developer at LoginRadius. He is currently studying Computer Science part time, and he loves all things JavaScript. In his spare time, he likes doing homework.","github":null,"stackoverflow":null,"linkedin":null,"medium":null,"twitter":null,"avatar":null}},"pageContext":{"id":"Greg Sakai","__params":{"id":"greg-sakai"}}},"staticQueryHashes":["1171199041","1384082988","2100481360","23180105","528864852"]}