{"id":132,"date":"2013-09-03T12:46:16","date_gmt":"2013-09-03T12:46:16","guid":{"rendered":"http:\/\/blog.chen-hongyi.com\/?p=132"},"modified":"2013-09-03T12:46:16","modified_gmt":"2013-09-03T12:46:16","slug":"how-to-use-default-parameter-in-js-function","status":"publish","type":"post","link":"https:\/\/chen-hongyi.com\/?p=132","title":{"rendered":"how to use default parameter in JS function"},"content":{"rendered":"<p><a href=\"http:\/\/www.markhansen.co.nz\/javascript-optional-parameters\/\">http:\/\/www.markhansen.co.nz\/javascript-optional-parameters\/<\/a><\/p>\n<p>Import is:<\/p>\n<pre><code>function connect(hostname, port, method) {\r\n    hostname = hostname || \"localhost\";\r\n    port = port || 80;\r\n    method = method || \"GET\";\r\n}\r\n<\/code><\/pre>\n<p>The short-circuit OR operator\u00a0<code>||<\/code>\u00a0returns the left side if the left argument is truthy (evaluates to\u00a0<code>true<\/code>in conditionals), otherwise it checks if the right argument is truthy, returning it. We can use this shortcut because\u00a0<code>undefined<\/code>\u00a0is falsy: in conditionals,\u00a0<code>undefined<\/code>\u00a0evaluates to\u00a0<code>false<\/code>.<\/p>\n<p>This shortcut approach is a very common idiom, but it does have a disadvantage: You can\u2019t use for any argument that could accept a falsy value:\u00a0<code>false<\/code>,\u00a0<code>0<\/code>,\u00a0<code>null<\/code>,\u00a0<code>undefined<\/code>, the empty string\u00a0<code>\"\"<\/code>, and\u00a0<code>NaN<\/code>.<\/p>\n<p>Using the\u00a0<code>||<\/code>\u00a0shortcut will override any falsy input value. If you expect a falsy value, you must explicitly check for\u00a0<code>argument === undefined<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/www.markhansen.co.nz\/javascript-o &hellip; <a href=\"https:\/\/chen-hongyi.com\/?p=132\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/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":[1],"tags":[],"class_list":["post-132","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/posts\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=132"}],"version-history":[{"count":1,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/posts\/132\/revisions"}],"predecessor-version":[{"id":133,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/posts\/132\/revisions\/133"}],"wp:attachment":[{"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}