{"id":4136,"date":"2019-03-29T10:46:33","date_gmt":"2019-03-29T10:46:33","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=4136"},"modified":"2019-03-29T10:46:33","modified_gmt":"2019-03-29T10:46:33","slug":"dont-name-global-javascript-function-clear","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/javascript\/dont-name-global-javascript-function-clear\/","title":{"rendered":"Don&#8217;t name your global JavaScript function &#8216;clear&#8217;"},"content":{"rendered":"<h2>TL;DR<\/h2>\n<p>Same as the title, don&#8217;t name your global JavaScript function <code>clear<\/code>.<\/p>\n<h2>TL;DR<\/h2>\n<p>I learned the hard way not to use <code>clear<\/code> as the name for my global JavaScript function.<\/p>\n<p>Given the example below:<\/p>\n<pre><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n    &lt;meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"&gt;\n    &lt;title&gt;Test 1&lt;\/title&gt;\n\n    &lt;script&gt;\n        function clear() {\n            document.getElementById('result').innerHTML = '';\n        }\n    &lt;\/script&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n    &lt;button id=\"clear\" onclick=\"clear()\"&gt;Clear&lt;\/button&gt;\n    &lt;div id=\"result\"&gt;Testing&lt;\/div&gt;\n&lt;\/body&gt;\n\n&lt;\/html&gt;\n<\/code><\/pre>\n<p>and clicking the <code>Clear<\/code> button, the <code>Testing<\/code> text will just not be removed.<\/p>\n<p>I found this very cool <a href=\"https:\/\/stackoverflow.com\/questions\/7165570\/is-clear-a-reserved-word-in-javascript\">StackOverflow explanation<\/a> of why is that. The gist of it is that this way the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/clear\">document.clear<\/a> function is being called, and simply renaming it to something like <code>clearResult<\/code> works fine.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TL;DR Same as the title, don&#8217;t name your global JavaScript function clear. TL;DR I learned the hard way not to use clear as the name for my global&hellip;<\/p>\n","protected":false},"author":1,"featured_media":4137,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-4136","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/4136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=4136"}],"version-history":[{"count":1,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/4136\/revisions"}],"predecessor-version":[{"id":4138,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/4136\/revisions\/4138"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/4137"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=4136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=4136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=4136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}