{"id":425,"date":"2022-08-30T15:01:08","date_gmt":"2022-08-30T15:01:08","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-do-you-handle-multi-argument-javascript-functions-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:01:08","modified_gmt":"2022-08-30T15:01:08","slug":"how-do-you-handle-multi-argument-javascript-functions-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-do-you-handle-multi-argument-javascript-functions-collection-of-common-programming-errors\/","title":{"rendered":"How do you handle multi-argument JavaScript functions?-Collection of common programming errors"},"content":{"rendered":"<p>I have defined my JavaScript function as follows:<\/p>\n<pre><code>function printCompanyName(company1, company2, company3, company4, company5)\n{\ndocument.write(\"<br \/>\" + company1 + \"<br \/><br \/>\");\ndocument.write(\"<br \/>\" + company2 + \"<br \/><br \/>\");\ndocument.write(\"<br \/>\" + company3 + \"<br \/><br \/>\");\ndocument.write(\"<br \/>\" + company4 + \"<br \/><br \/>\");\ndocument.write(\"<br \/>\" + company5 + \"<br \/><br \/>\");\n}\n<\/code><\/pre>\n<p>And called it as follows:<\/p>\n<pre><code>printCompanyName(\"Dell, Microsoft, Apple, Gizmodo, Amazon\");\n<\/code><\/pre>\n<p>But I get the following output:<\/p>\n<pre><code>Dell, Microsoft, Apple, Gizmodo, Amazon\n\nundefined\n\nundefined\n\nundefined\n\nundefined\n<\/code><\/pre>\n<p>What gives!? I have been trying to figure this out for hrs. I want:<\/p>\n<pre><code>Dell\nMicrosoft\nApple\nGizmodo\nAmazon\n<\/code><\/pre>\n<ol>\n<li>\n<p>You want to call:<\/p>\n<pre><code>printCompanyName(\"Dell\", \"Microsoft\", \"Apple\", \"Gizmodo\", \"Amazon\");\n<\/code><\/pre>\n<p>The way you&#8217;re currently doing it you&#8217;re passing in one company &#8220;Dell, Microsoft, Apple, Gizmodo, Amazon&#8221;.<\/p>\n<\/li>\n<li>\n<p>You&#8217;re passing a single string that happens to contain 4 commas. Therefore, the first parameter contains that single string, and the other 4 are undefined. (Sisnce you only gave one value)<\/p>\n<p>Since Javascript parameters are optional, you don&#8217;t get an error by not passing values for the other parameters.<\/p>\n<p>You need to pass 5 different strings with commas between them, like this:<\/p>\n<pre><code>printCompanyName(\"Dell\", \"Microsoft\", \"Apple\", \"Gizmodo\", \"Amazon\");\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Try this:<\/p>\n<pre><code>printCompanyName(\"Dell\", \"Microsoft\", \"Apple\", \"Gizmodo\", \"Amazon\");\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:26:27. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have defined my JavaScript function as follows: function printCompanyName(company1, company2, company3, company4, company5) { document.write(&#8220;&#8221; + company1 + &#8220;&#8221;); document.write(&#8220;&#8221; + company2 + &#8220;&#8221;); document.write(&#8220;&#8221; + company3 + &#8220;&#8221;); document.write(&#8220;&#8221; + company4 + &#8220;&#8221;); document.write(&#8220;&#8221; + company5 + &#8220;&#8221;); } And called it as follows: printCompanyName(&#8220;Dell, Microsoft, Apple, Gizmodo, Amazon&#8221;); But I get the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-425","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=425"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/425\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}