{"id":1057,"date":"2012-05-14T16:25:45","date_gmt":"2012-05-14T22:25:45","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=1057"},"modified":"2012-05-14T21:00:25","modified_gmt":"2012-05-15T03:00:25","slug":"code-of-the-day-javascript-fixed-for-isnan","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2012\/05\/code-of-the-day-javascript-fixed-for-isnan\/","title":{"rendered":"Code of the Day: Javascript, Fix for isNaN"},"content":{"rendered":"<p>Javascript is a dynamically typed langauges. This feature causes for Arrays, Booleans, and other types to be converted to a numeric values depending on usage.<br \/>\nHowever this feature can cause headarches when trying to detect numbers.<\/p>\n<p>For example, isNaN() detects if a value is a non-number. That&#8217;s what NaN stands for, &#8220;Not a number&#8221;.<br \/>\nSo you might think that the opposite result from isNaN() would indicate if a value is a number.<\/p>\n<p><code><\/p>\n<pre lang='javascript'>\r\n\/\/ Works for most cases but not for strict comparisons.\r\nvar isNumber = function(val){\r\n\treturn !isNaN( val );\r\n};\r\n<\/pre>\n<p><\/code><\/p>\n<p><b>Example:<\/b><br \/>\nNote: Click the \u201cresult\u201d tab to run the test cases in jsfiddle.net.<\/p>\n<p><iframe style=\"width: 100%; height: 300px\" src=\"http:\/\/jsfiddle.net\/tWyHg\/11\/embedded\/\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>\n<p><b>Solution:<\/b><br \/>\nSo here&#8217;s a simple fix for isNaN and isNumber.<br \/>\n<code><\/p>\n<pre lang='javascript'>\r\n\/\/ isNaN2 returns a boolean for if a value is not a number or +-Infinity\r\nvar isNaN2 = (function (global) {\r\n    var _isNaN = global.isNaN;\r\n    return function (val) {\r\n        return _isNaN(\"\" + val) || (typeof val === \"object\" && !(val || \"\").hasOwnProperty('push'));\r\n    };\r\n}(this));\r\n\/\/ isNumeric returns a boolean for if a value is a number or +-Infinity\r\nvar isNumeric = (function (global) {\r\n    var _isNaN = global.isNaN;\r\n    return function (val) {\r\n        return !_isNaN(\"\" + val)&&(typeof val !== \"object\" || (val || \"\").hasOwnProperty('push'));\r\n    };\r\n}(this));\r\n<\/pre>\n<p><\/code><br \/>\n<b>isNaN vs isNaN2<\/b><br \/>\n<iframe style=\"width: 100%; height: 300px\" src=\"http:\/\/jsfiddle.net\/tWyHg\/7\/embedded\/\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>\n<p><b>Testcases for isNaN2 and isNumeric<\/b><br \/>\n<iframe style=\"width: 100%; height: 300px\" src=\"http:\/\/jsfiddle.net\/tWyHg\/12\/embedded\/\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>\n<p><b>*Update*<\/b><br \/>\nWell it turns out that jQuery has a better implementation. The only difference is that infinity is not a number, which is correct.<br \/>\n<code><\/p>\n<pre lang='javascript'>\r\n$.isNumeric = function( obj ){\r\n    return !isNaN( parseFloat( obj ) ) && isFinite( obj );\r\n};\r\n<\/pre>\n<p><\/code><br \/>\n<a href=\"http:\/\/api.jquery.com\/jQuery.isNumeric\">api.jquery.com\/jQuery.isNumeric<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Javascript is a dynamically typed langauges. This feature causes for Arrays, Booleans, and other types to be converted to a numeric values depending on usage. However this feature can cause headarches when trying to detect numbers. For example, isNaN() detects if a value is a non-number. That&#8217;s what NaN stands for, &#8220;Not a number&#8221;. So &hellip; <a href=\"https:\/\/bateru.com\/news\/2012\/05\/code-of-the-day-javascript-fixed-for-isnan\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the Day: Javascript, Fix for isNaN<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[30,139,164,166,140],"class_list":["post-1057","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","tag-code-of-the-day","tag-isnan","tag-javascript","tag-math","tag-numbers"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=1057"}],"version-history":[{"count":9,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1057\/revisions"}],"predecessor-version":[{"id":1066,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1057\/revisions\/1066"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=1057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=1057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=1057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}