{"id":3318,"date":"2018-03-19T10:00:45","date_gmt":"2018-03-19T04:30:45","guid":{"rendered":"https:\/\/code4developers.com\/?p=3318"},"modified":"2023-10-16T00:06:34","modified_gmt":"2023-10-15T18:36:34","slug":"arguments-object-javascript-function","status":"publish","type":"post","link":"https:\/\/code4developers.com\/arguments-object-javascript-function\/","title":{"rendered":"arguments object in JavaScript function"},"content":{"rendered":"<p>The <em><strong>arguments<\/strong><\/em> object is an Array-like object matching to the arguments passed to a function. The arguments object is a local variable available within all (non-arrow) functions. You can refer to a function&#8217;s arguments within the function by using the arguments object.<!--more-->Let&#8217;s consider below code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">function addition(num1, num2) { \r\n  return num1 + num2; \r\n} \r\naddition(10,20);<\/pre>\n<p>In above code, we are calling function &#8220;addition()&#8221; passing 10 and 20 for &#8220;num1&#8221; and &#8220;num2&#8221;. So, the function will return an answer 30. Now what if we pass one more arguments while calling function &#8220;addition()&#8221;? Let&#8217;s do it:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">function addition(num1, num2) { \r\n  return num1 + num2; \r\n} \r\naddition(10,20,25);<\/pre>\n<p>Will this function &#8220;addition()&#8221; throw an error? Answer is no. <strong>It will return an answer 30.<\/strong> So, here comes one more question. What happen to the value 25? Answer is, it will be there in <em><strong>arguments <\/strong><\/em>object.<\/p>\n<p>Let&#8217;s consider below code to access all the passed arguments using arguments object<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">function addition(num1, num2) { \r\n  return arguments[0] + arguments[1] + arguments[2]; \r\n} \r\naddition(10,20,25);<\/pre>\n<p>In above code, we are able to access all the passed arguments and the function &#8220;addition()&#8221; will return an answer 55 here. In JavaScript we can access all the passed arguments using arguments objects and this object contains an entry for each argument passed to the function, the first entry&#8217;s index starting at 0.<\/p>\n<h4 id=\"arguments-object-is-an-array-like-object\">arguments object is an array-like object<\/h4>\n<p>In above example we can see the code where we are accessing arguments object same as we access an array. But we can&#8217;t say arguments object a pure array, because it does not provide all the properties which array provides. It provides only <strong>length<\/strong> property which make it an array-like object. If you will try anything else than length in arguments object it will throw an exception calling &#8220;&#8230;is not a function&#8221;.<\/p>\n<h4 id=\"modify-an-arguments-object\">Modify an arguments object<\/h4>\n<p>We can modify or set an value to the arguments object. Refer below code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">function addition(num1, num2) { \r\n  arguments[0] = 5;\r\n  return arguments[0] + arguments[1] + arguments[2]; \r\n} \r\naddition(10,20,25);<\/pre>\n<p>We have used same code as our previous example, here we are setting first argument to the number 5 and it will get updated. So, this function &#8220;addition()&#8221; will return an answer 50.<\/p>\n<h4 id=\"convert-arguments-object-to-a-pure-array\">Convert arguments object to a pure array<\/h4>\n<p>As we discussed, arguments object is not an array, but it is array like object. So, how to convert it to the pure array. Let&#8217;s consider below code<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var args = Array.prototype.slice.call(arguments);\r\n\r\nOR\r\n\r\nvar args = [].slice.call(arguments);<\/pre>\n<p>In ECMAScript 6 we can achieve same as below<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">var args = Array.from(arguments);<\/pre>\n<p>We can also do it using <a href=\"https:\/\/code4developers.com\/spread-syntax-in-javascript\/\">spread operator<\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">var args = [...arguments];<\/pre>\n<p><a href=\"https:\/\/code4developers.com\/category\/javascript\/\">Interested in JavaScript? Click here to read more JavaScript articles in Code4Developers<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The arguments object is an Array-like object matching to the arguments passed to a function. The arguments object is a local variable available within all (non-arrow) functions. You can refer&hellip;<\/p>\n","protected":false},"author":4,"featured_media":3127,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[782,783,22,784],"powerkit_post_featured":[],"class_list":{"0":"post-3318","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript","8":"tag-arguments","9":"tag-function","10":"tag-javascript","11":"tag-object"},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8NAi4-Rw","jetpack-related-posts":[{"id":3311,"url":"https:\/\/code4developers.com\/default-parameter-in-a-javascript-function\/","url_meta":{"origin":3318,"position":0},"title":"Default Parameter in JavaScript function","author":"Yatendrasinh Joddha","date":"March 17, 2018","format":false,"excerpt":"In any programming language we often require having default parameter or default value for the parameter in a function. JavaScript allow us to initialize parameter a default value. If you are not passing any value to the parameter, then the default value of the parameter will be undefined. Let's consider\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/code4developers.com\/category\/javascript\/"},"img":{"alt_text":"javascript","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3321,"url":"https:\/\/code4developers.com\/spread-syntax-in-javascript\/","url_meta":{"origin":3318,"position":1},"title":"Spread syntax (three dots) in JavaScript","author":"Yatendrasinh Joddha","date":"March 21, 2018","format":false,"excerpt":"Spread syntax which is used by typing three dots (...) in JavaScript. It allows an array expression or string or anything which can be iterating to be expanded in places where zero or more arguments for function calls\u00a0or elements for array are expected. It can also be used for an\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/code4developers.com\/category\/javascript\/"},"img":{"alt_text":"javascript","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3106,"url":"https:\/\/code4developers.com\/javascript-template-literals\/","url_meta":{"origin":3318,"position":2},"title":"Javascript: What Are Template Literals?","author":"Arif Khoja","date":"September 30, 2017","format":false,"excerpt":"ES6 introduced two types of literals: Template Literals and Tagged Template Literals to tag Template Literals. In this post, we cover both of them in depth! In ES6, two types of literals were introduced: Template Literals for string concatenation and expression embedding in a string Tagged Template Literals to tag\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/code4developers.com\/category\/javascript\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/06\/thumb-1-e1498942297714.png?fit=240%2C269&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2662,"url":"https:\/\/code4developers.com\/ecmascript-5-strict-mode-json\/","url_meta":{"origin":3318,"position":3},"title":"ECMAScript5 Strict Mode, JSON, and More&#8230;.","author":"Arif Khoja","date":"June 25, 2017","format":false,"excerpt":"Introduction There are a number of other new features and APIs that need attention, as well. The largest of which are\u00a0Strict Mode\u00a0and native\u00a0JSON\u00a0support. Strict Mode Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a \u201cstrict\u201d operating context. This\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/code4developers.com\/category\/javascript\/"},"img":{"alt_text":"javascript","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":7069,"url":"https:\/\/code4developers.com\/ways-to-use-generator-functions-in-javascript\/","url_meta":{"origin":3318,"position":4},"title":"Ways to use Generator Functions in JavaScript","author":"Arif Khoja","date":"May 15, 2020","format":false,"excerpt":"In this Article we will see below points Generator Functions Recap: Functions How does it work? 4 ways to use Generator Functions with Examples Advantages of using Generator function What are Generator Functions? Ever imagined what would happen if a function had an infinite input or output to deal with?\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/code4developers.com\/category\/javascript\/"},"img":{"alt_text":"Functional-programming-js","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2020\/04\/avatar-js.png?fit=512%2C512&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":6797,"url":"https:\/\/code4developers.com\/functional-programming-currying\/","url_meta":{"origin":3318,"position":5},"title":"Functional Programming in Javascript &#8211; Currying","author":"Arif Khoja","date":"April 7, 2020","format":false,"excerpt":"Currying is an advanced technique of working with functions. It\u2019s used not only in JavaScript, but in other languages as well. Currying is a transformation of functions that translates a function from callable as\u00a0f(a, b, c) \u00a0into callable as\u00a0f(a)(b)(c). Currying doesn\u2019t call a function. It just transforms it.Let\u2019s see an\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/code4developers.com\/category\/javascript\/"},"img":{"alt_text":"Functional-programming-js","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2020\/04\/avatar-js.png?fit=512%2C512&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts\/3318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/comments?post=3318"}],"version-history":[{"count":4,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts\/3318\/revisions"}],"predecessor-version":[{"id":3396,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts\/3318\/revisions\/3396"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/media\/3127"}],"wp:attachment":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/media?parent=3318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/categories?post=3318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/tags?post=3318"},{"taxonomy":"powerkit_post_featured","embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/powerkit_post_featured?post=3318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}