{"id":18,"date":"2018-01-02T07:00:40","date_gmt":"2018-01-02T14:00:40","guid":{"rendered":"https:\/\/coreassistance.com\/tips\/?p=18"},"modified":"2018-01-12T13:28:19","modified_gmt":"2018-01-12T20:28:19","slug":"avoid-comparison-surprises","status":"publish","type":"post","link":"https:\/\/coreassistance.com\/tips\/2018\/01\/02\/avoid-comparison-surprises\/","title":{"rendered":"Avoid Comparison Surprises"},"content":{"rendered":"<p>Get in the habit of using the strict comparison operators (<code>===<\/code> and <code>!==<\/code>) in JavaScript.  When you use <code>==<\/code> or <code>!=<\/code> JavaScript will, depending on the values being compared, perform automatic type conversion, which can cause some odd and unexpected behavior.<\/p>\n<p>Take this example:<\/p>\n<pre><code class=\"javascript\">'0' == 0; \/\/ Evaluates to true.<\/code><\/pre>\n<p>How is a string containing the character <code>0<\/code> equal to the integer zero?  Because, when using <code>==<\/code> or <code>!=<\/code>, JavaScript converts the string into a number before performing the comparison.  Using the strict comparison operators works as expected:<\/p>\n<pre><code class=\"javascript\">'0' === 0; \/\/ Evaluates to false.<\/code><\/pre>\n<p>That&#8217;s just one example of how automatic type conversion can get you into an unexpected and hard-to-debug situation.  Using <code>===<\/code> and <code>!==<\/code> will avoid such nonsense and always do what you expect.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript&#8217;s automatic type conversion can produce some strange results.  Thankfully it&#8217;s easy to avoid.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":6,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":142,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/posts\/18\/revisions\/142"}],"wp:attachment":[{"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}