Changing JavaScript function's parameter value using arguments array not working












35















I am learning JavaScript and am pretty confused about the arguments property array.



I have a function that takes a single argument and returns it. When I pass the parameter and reassign it using arguments[0] = value, it's updating the value.






function a(b) {
arguments[0] = 2;
return b;
}
console.log(a(1)); //returns 2





But when I call the same function with no parameters it returns undefined.






function a(b) {
arguments[0] = 2;
return b;
}
console.log(a()); //returns undefined





But even if I pass undefined, the value will update as well.






function a(b) {
arguments[0] = 2;
return b;
}
console.log(a(undefined)); //returns 2





I thought that if you do not pass a parameter to a JavaScript function, it automatically creates it and assigns the value to undefined and after updating it should reflect the updated value, right?



Also a() and a(undefined) are the same thing, right?










share|improve this question





























    35















    I am learning JavaScript and am pretty confused about the arguments property array.



    I have a function that takes a single argument and returns it. When I pass the parameter and reassign it using arguments[0] = value, it's updating the value.






    function a(b) {
    arguments[0] = 2;
    return b;
    }
    console.log(a(1)); //returns 2





    But when I call the same function with no parameters it returns undefined.






    function a(b) {
    arguments[0] = 2;
    return b;
    }
    console.log(a()); //returns undefined





    But even if I pass undefined, the value will update as well.






    function a(b) {
    arguments[0] = 2;
    return b;
    }
    console.log(a(undefined)); //returns 2





    I thought that if you do not pass a parameter to a JavaScript function, it automatically creates it and assigns the value to undefined and after updating it should reflect the updated value, right?



    Also a() and a(undefined) are the same thing, right?










    share|improve this question



























      35












      35








      35


      1






      I am learning JavaScript and am pretty confused about the arguments property array.



      I have a function that takes a single argument and returns it. When I pass the parameter and reassign it using arguments[0] = value, it's updating the value.






      function a(b) {
      arguments[0] = 2;
      return b;
      }
      console.log(a(1)); //returns 2





      But when I call the same function with no parameters it returns undefined.






      function a(b) {
      arguments[0] = 2;
      return b;
      }
      console.log(a()); //returns undefined





      But even if I pass undefined, the value will update as well.






      function a(b) {
      arguments[0] = 2;
      return b;
      }
      console.log(a(undefined)); //returns 2





      I thought that if you do not pass a parameter to a JavaScript function, it automatically creates it and assigns the value to undefined and after updating it should reflect the updated value, right?



      Also a() and a(undefined) are the same thing, right?










      share|improve this question
















      I am learning JavaScript and am pretty confused about the arguments property array.



      I have a function that takes a single argument and returns it. When I pass the parameter and reassign it using arguments[0] = value, it's updating the value.






      function a(b) {
      arguments[0] = 2;
      return b;
      }
      console.log(a(1)); //returns 2





      But when I call the same function with no parameters it returns undefined.






      function a(b) {
      arguments[0] = 2;
      return b;
      }
      console.log(a()); //returns undefined





      But even if I pass undefined, the value will update as well.






      function a(b) {
      arguments[0] = 2;
      return b;
      }
      console.log(a(undefined)); //returns 2





      I thought that if you do not pass a parameter to a JavaScript function, it automatically creates it and assigns the value to undefined and after updating it should reflect the updated value, right?



      Also a() and a(undefined) are the same thing, right?






      function a(b) {
      arguments[0] = 2;
      return b;
      }
      console.log(a(1)); //returns 2





      function a(b) {
      arguments[0] = 2;
      return b;
      }
      console.log(a(1)); //returns 2





      function a(b) {
      arguments[0] = 2;
      return b;
      }
      console.log(a()); //returns undefined





      function a(b) {
      arguments[0] = 2;
      return b;
      }
      console.log(a()); //returns undefined





      function a(b) {
      arguments[0] = 2;
      return b;
      }
      console.log(a(undefined)); //returns 2





      function a(b) {
      arguments[0] = 2;
      return b;
      }
      console.log(a(undefined)); //returns 2






      javascript arrays function arguments variable-assignment






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 9 at 14:57









      grooveplex

      1,14721324




      1,14721324










      asked Jan 23 at 8:39









      Amit DasAmit Das

      221213




      221213
























          6 Answers
          6






          active

          oldest

          votes


















          22














          Assigning to arguments indicies will only change the associated argument value (let's call it the n-th argument) if the function was called with at least n arguments. The arguments object's numeric-indexed properties are essentially setters (and getters):



          http://es5.github.io/#x10.6



          Italics in the below are my comments on how the process relates to the question:




          (Let) args (be) the actual arguments passed to the [[Call]] internal method




          1. Let len be the number of elements in args.


          2. Let indx = len - 1.


          3. Repeat while indx >= 0, (so, the below loop will not run when no arguments are passed to the function:)



          (assign to the arguments object being created, here called map:)





            1. Add name as an element of the list mappedNames.



            1. Let g be the result of calling the MakeArgGetter abstract operation with arguments name and env.



            1. Let p be the result of calling the MakeArgSetter abstract operation with arguments name and env.



            1. Call the [[DefineOwnProperty]] internal method of map passing ToString(indx), the Property Descriptor {[[Set]]: p, [[Get]]: g, [[Configurable]]: true}, and false as arguments.





          So, if the function is invoked with no arguments, there will not be a setter on arguments[0], so reassigning it won't change the parameter at index 0.



          The same sort of thing occurs for other indicies as well - if you invoke a function with 1 parameter, but the function accepts two parameters, assigning to arguments[1] will not change the second parameter, because arguments[1] does not have a setter:






          function fn(a, b) {
          arguments[1] = 'bar';
          console.log(b);
          }
          fn('foo');





          So




          a() and a(undefined) are the same thing right?




          is not the case, because the second results in an arguments object with a setter and a getter on index 0, while the first doesn't.






          share|improve this answer


























          • So if I understand it correctly then if I do not pass any parameters then the arguments object will have length 0. So if I try to access that length 0 object with arguments[0] should it not throw an error? Or is it like there IS a index 0 but just no setter and getter?

            – Amit Das
            Jan 23 at 9:30








          • 2





            The arguments object is defined regardless, and nearly any object can have any property assigned to it at will. For example, const foo = {}; foo[0] = 'bar';. That's kind of similar to your arguments[0] = 2 when a is being called with no arguments - there's no setter or getter on index 0, so you can assign to it without problems, it won't throw an error, but it won't affect anything else.

            – CertainPerformance
            Jan 23 at 9:36








          • 1





            I see. Thanks. Wow JavaScript is weird.

            – Amit Das
            Jan 23 at 9:39











          • Nice, haven't seen an answer cite the ES5 spec in ages :-) It's totally appropriate here, though.

            – Bergi
            Jan 23 at 19:06











          • @AmitDas Always "use strict" mode, and JavaScript becomes a lot less weird.

            – Bergi
            Jan 23 at 19:07





















          6














          ECMA 262 9.0 2018 describes this behaviour in 9.4.4 Arguments Exotic Objects with




          NOTE 1:



          The integer-indexed data properties of an arguments exotic object whose numeric name values are less than the number of formal parameters of the corresponding function object initially share their values with the corresponding argument bindings in the function's execution context. This means that changing the property changes the corresponding value of the argument binding and vice-versa. This correspondence is broken if such a property is deleted and then redefined or if the property is changed into an accessor property. If the arguments object is an ordinary object, the values of its properties are simply a copy of the arguments passed to the function and there is no dynamic linkage between the property values and the formal parameter values.




          In short,




          • if in 'sloppy mode', then all arguments are mapped to their named variables, if the length correspond to the given parameter, or


          • if in 'strict mode', then the binding is lost after handing over the arguments.



          This is only readable in an older version of ECMA 262 7.0 2016. It describes this behaviour in 9.4.4 Arguments Exotic Objects with




          Note 1:



          For non-strict functions the integer indexed data properties of an arguments object whose numeric name values are less than the number of formal parameters of the corresponding function object initially share their values with the corresponding argument bindings in the function's execution context. This means that changing the property changes the corresponding value of the argument binding and vice-versa. This correspondence is broken if such a property is deleted and then redefined or if the property is changed into an accessor property. For strict mode functions, the values of the arguments object's properties are simply a copy of the arguments passed to the function and there is no dynamic linkage between the property values and the formal parameter values.







          share|improve this answer

































            1














            This is the undefined value definition from javascript spec :



            primitive value used when a variable has not been assigned a value.



            so if you do not specify the function return type it will return undefined.



            so a() and a(undefined) it is not same thing. returning undefined is based on return type is defined or not.



            for more clarification similar_problem






            share|improve this answer
























            • that is obviously wrong with the arguments object, depending on the mode.

              – Nina Scholz
              Jan 23 at 9:16



















            1














            My understanding is that the arguments object only tracks what is passed into the function. Since you've not initially passed anything, b is not bound and at that point arguments is not 'tracking' b. Next, you assign a value to the initialised but empty Array-like object arguments and finally return b, which is undefined.



            To delve into this further:




            If a non-strict function does not contain rest, default, or destructured parameters, then the values in the arguments object do change in sync with the values of the argument variables. See the code below:




            function func(a) { 
            arguments[0] = 99; // updating arguments[0] also updates a
            console.log(a);
            }
            func(10); // 99


            and



            function func(a) { 
            a = 99; // updating a also updates arguments[0]
            console.log(arguments[0]);
            }
            func(10); // 99



            When a non-strict function does contain rest, default, or destructured parameters, then the values in the arguments object do not track the values of the arguments. Instead, they reflect the arguments provided when the function was called:




            function func(a = 55) { 
            arguments[0] = 99; // updating arguments[0] does not also update a
            console.log(a);
            }
            func(10); // 10


            and



            function func(a = 55) { 
            a = 99; // updating a does not also update arguments[0]
            console.log(arguments[0]);
            }
            func(10); // 10


            and



            // An untracked default parameter
            function func(a = 55) {
            console.log(arguments[0]);
            }
            func(); // undefined


            Source: MDN Web docs






            share|improve this answer


























            • "at that point arguments is undefined" - that's not true. arguments is not undefined - it has only length equal to 0

              – puffy.bun
              Jan 23 at 8:56











            • @puffy.bun you're absolutely right, I've updated my answer

              – Barzev
              Jan 23 at 9:01



















            0














            it's because arguments it's not like a Array, it's a object with integer indexed data keys, and property length, And if length equal zero it's mean you don't have a arguments






            function a(b) {
            arguments[0] = 2;
            console.log(arguments.length)
            return b;
            }
            a(1); // length 1 returns 2
            console.log(a()); // length 0 returns undefined








            share|improve this answer































              0














              When you are not providing any parameter then arguments array has length equal to 0. Then you are trying to set the non existing element of array to 2 which causes returning undefined



              You can simply test this with this snippet:



              function a(b){ 
              alert(arguments.length) // It will prompt 0 when calling a() and 1 when calling a(undefined)
              arguments[0] = 2;
              return b;
              }





              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54323086%2fchanging-javascript-functions-parameter-value-using-arguments-array-not-working%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                6 Answers
                6






                active

                oldest

                votes








                6 Answers
                6






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                22














                Assigning to arguments indicies will only change the associated argument value (let's call it the n-th argument) if the function was called with at least n arguments. The arguments object's numeric-indexed properties are essentially setters (and getters):



                http://es5.github.io/#x10.6



                Italics in the below are my comments on how the process relates to the question:




                (Let) args (be) the actual arguments passed to the [[Call]] internal method




                1. Let len be the number of elements in args.


                2. Let indx = len - 1.


                3. Repeat while indx >= 0, (so, the below loop will not run when no arguments are passed to the function:)



                (assign to the arguments object being created, here called map:)





                  1. Add name as an element of the list mappedNames.



                  1. Let g be the result of calling the MakeArgGetter abstract operation with arguments name and env.



                  1. Let p be the result of calling the MakeArgSetter abstract operation with arguments name and env.



                  1. Call the [[DefineOwnProperty]] internal method of map passing ToString(indx), the Property Descriptor {[[Set]]: p, [[Get]]: g, [[Configurable]]: true}, and false as arguments.





                So, if the function is invoked with no arguments, there will not be a setter on arguments[0], so reassigning it won't change the parameter at index 0.



                The same sort of thing occurs for other indicies as well - if you invoke a function with 1 parameter, but the function accepts two parameters, assigning to arguments[1] will not change the second parameter, because arguments[1] does not have a setter:






                function fn(a, b) {
                arguments[1] = 'bar';
                console.log(b);
                }
                fn('foo');





                So




                a() and a(undefined) are the same thing right?




                is not the case, because the second results in an arguments object with a setter and a getter on index 0, while the first doesn't.






                share|improve this answer


























                • So if I understand it correctly then if I do not pass any parameters then the arguments object will have length 0. So if I try to access that length 0 object with arguments[0] should it not throw an error? Or is it like there IS a index 0 but just no setter and getter?

                  – Amit Das
                  Jan 23 at 9:30








                • 2





                  The arguments object is defined regardless, and nearly any object can have any property assigned to it at will. For example, const foo = {}; foo[0] = 'bar';. That's kind of similar to your arguments[0] = 2 when a is being called with no arguments - there's no setter or getter on index 0, so you can assign to it without problems, it won't throw an error, but it won't affect anything else.

                  – CertainPerformance
                  Jan 23 at 9:36








                • 1





                  I see. Thanks. Wow JavaScript is weird.

                  – Amit Das
                  Jan 23 at 9:39











                • Nice, haven't seen an answer cite the ES5 spec in ages :-) It's totally appropriate here, though.

                  – Bergi
                  Jan 23 at 19:06











                • @AmitDas Always "use strict" mode, and JavaScript becomes a lot less weird.

                  – Bergi
                  Jan 23 at 19:07


















                22














                Assigning to arguments indicies will only change the associated argument value (let's call it the n-th argument) if the function was called with at least n arguments. The arguments object's numeric-indexed properties are essentially setters (and getters):



                http://es5.github.io/#x10.6



                Italics in the below are my comments on how the process relates to the question:




                (Let) args (be) the actual arguments passed to the [[Call]] internal method




                1. Let len be the number of elements in args.


                2. Let indx = len - 1.


                3. Repeat while indx >= 0, (so, the below loop will not run when no arguments are passed to the function:)



                (assign to the arguments object being created, here called map:)





                  1. Add name as an element of the list mappedNames.



                  1. Let g be the result of calling the MakeArgGetter abstract operation with arguments name and env.



                  1. Let p be the result of calling the MakeArgSetter abstract operation with arguments name and env.



                  1. Call the [[DefineOwnProperty]] internal method of map passing ToString(indx), the Property Descriptor {[[Set]]: p, [[Get]]: g, [[Configurable]]: true}, and false as arguments.





                So, if the function is invoked with no arguments, there will not be a setter on arguments[0], so reassigning it won't change the parameter at index 0.



                The same sort of thing occurs for other indicies as well - if you invoke a function with 1 parameter, but the function accepts two parameters, assigning to arguments[1] will not change the second parameter, because arguments[1] does not have a setter:






                function fn(a, b) {
                arguments[1] = 'bar';
                console.log(b);
                }
                fn('foo');





                So




                a() and a(undefined) are the same thing right?




                is not the case, because the second results in an arguments object with a setter and a getter on index 0, while the first doesn't.






                share|improve this answer


























                • So if I understand it correctly then if I do not pass any parameters then the arguments object will have length 0. So if I try to access that length 0 object with arguments[0] should it not throw an error? Or is it like there IS a index 0 but just no setter and getter?

                  – Amit Das
                  Jan 23 at 9:30








                • 2





                  The arguments object is defined regardless, and nearly any object can have any property assigned to it at will. For example, const foo = {}; foo[0] = 'bar';. That's kind of similar to your arguments[0] = 2 when a is being called with no arguments - there's no setter or getter on index 0, so you can assign to it without problems, it won't throw an error, but it won't affect anything else.

                  – CertainPerformance
                  Jan 23 at 9:36








                • 1





                  I see. Thanks. Wow JavaScript is weird.

                  – Amit Das
                  Jan 23 at 9:39











                • Nice, haven't seen an answer cite the ES5 spec in ages :-) It's totally appropriate here, though.

                  – Bergi
                  Jan 23 at 19:06











                • @AmitDas Always "use strict" mode, and JavaScript becomes a lot less weird.

                  – Bergi
                  Jan 23 at 19:07
















                22












                22








                22







                Assigning to arguments indicies will only change the associated argument value (let's call it the n-th argument) if the function was called with at least n arguments. The arguments object's numeric-indexed properties are essentially setters (and getters):



                http://es5.github.io/#x10.6



                Italics in the below are my comments on how the process relates to the question:




                (Let) args (be) the actual arguments passed to the [[Call]] internal method




                1. Let len be the number of elements in args.


                2. Let indx = len - 1.


                3. Repeat while indx >= 0, (so, the below loop will not run when no arguments are passed to the function:)



                (assign to the arguments object being created, here called map:)





                  1. Add name as an element of the list mappedNames.



                  1. Let g be the result of calling the MakeArgGetter abstract operation with arguments name and env.



                  1. Let p be the result of calling the MakeArgSetter abstract operation with arguments name and env.



                  1. Call the [[DefineOwnProperty]] internal method of map passing ToString(indx), the Property Descriptor {[[Set]]: p, [[Get]]: g, [[Configurable]]: true}, and false as arguments.





                So, if the function is invoked with no arguments, there will not be a setter on arguments[0], so reassigning it won't change the parameter at index 0.



                The same sort of thing occurs for other indicies as well - if you invoke a function with 1 parameter, but the function accepts two parameters, assigning to arguments[1] will not change the second parameter, because arguments[1] does not have a setter:






                function fn(a, b) {
                arguments[1] = 'bar';
                console.log(b);
                }
                fn('foo');





                So




                a() and a(undefined) are the same thing right?




                is not the case, because the second results in an arguments object with a setter and a getter on index 0, while the first doesn't.






                share|improve this answer















                Assigning to arguments indicies will only change the associated argument value (let's call it the n-th argument) if the function was called with at least n arguments. The arguments object's numeric-indexed properties are essentially setters (and getters):



                http://es5.github.io/#x10.6



                Italics in the below are my comments on how the process relates to the question:




                (Let) args (be) the actual arguments passed to the [[Call]] internal method




                1. Let len be the number of elements in args.


                2. Let indx = len - 1.


                3. Repeat while indx >= 0, (so, the below loop will not run when no arguments are passed to the function:)



                (assign to the arguments object being created, here called map:)





                  1. Add name as an element of the list mappedNames.



                  1. Let g be the result of calling the MakeArgGetter abstract operation with arguments name and env.



                  1. Let p be the result of calling the MakeArgSetter abstract operation with arguments name and env.



                  1. Call the [[DefineOwnProperty]] internal method of map passing ToString(indx), the Property Descriptor {[[Set]]: p, [[Get]]: g, [[Configurable]]: true}, and false as arguments.





                So, if the function is invoked with no arguments, there will not be a setter on arguments[0], so reassigning it won't change the parameter at index 0.



                The same sort of thing occurs for other indicies as well - if you invoke a function with 1 parameter, but the function accepts two parameters, assigning to arguments[1] will not change the second parameter, because arguments[1] does not have a setter:






                function fn(a, b) {
                arguments[1] = 'bar';
                console.log(b);
                }
                fn('foo');





                So




                a() and a(undefined) are the same thing right?




                is not the case, because the second results in an arguments object with a setter and a getter on index 0, while the first doesn't.






                function fn(a, b) {
                arguments[1] = 'bar';
                console.log(b);
                }
                fn('foo');





                function fn(a, b) {
                arguments[1] = 'bar';
                console.log(b);
                }
                fn('foo');






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 23 at 9:05

























                answered Jan 23 at 8:59









                CertainPerformanceCertainPerformance

                92.7k165384




                92.7k165384













                • So if I understand it correctly then if I do not pass any parameters then the arguments object will have length 0. So if I try to access that length 0 object with arguments[0] should it not throw an error? Or is it like there IS a index 0 but just no setter and getter?

                  – Amit Das
                  Jan 23 at 9:30








                • 2





                  The arguments object is defined regardless, and nearly any object can have any property assigned to it at will. For example, const foo = {}; foo[0] = 'bar';. That's kind of similar to your arguments[0] = 2 when a is being called with no arguments - there's no setter or getter on index 0, so you can assign to it without problems, it won't throw an error, but it won't affect anything else.

                  – CertainPerformance
                  Jan 23 at 9:36








                • 1





                  I see. Thanks. Wow JavaScript is weird.

                  – Amit Das
                  Jan 23 at 9:39











                • Nice, haven't seen an answer cite the ES5 spec in ages :-) It's totally appropriate here, though.

                  – Bergi
                  Jan 23 at 19:06











                • @AmitDas Always "use strict" mode, and JavaScript becomes a lot less weird.

                  – Bergi
                  Jan 23 at 19:07





















                • So if I understand it correctly then if I do not pass any parameters then the arguments object will have length 0. So if I try to access that length 0 object with arguments[0] should it not throw an error? Or is it like there IS a index 0 but just no setter and getter?

                  – Amit Das
                  Jan 23 at 9:30








                • 2





                  The arguments object is defined regardless, and nearly any object can have any property assigned to it at will. For example, const foo = {}; foo[0] = 'bar';. That's kind of similar to your arguments[0] = 2 when a is being called with no arguments - there's no setter or getter on index 0, so you can assign to it without problems, it won't throw an error, but it won't affect anything else.

                  – CertainPerformance
                  Jan 23 at 9:36








                • 1





                  I see. Thanks. Wow JavaScript is weird.

                  – Amit Das
                  Jan 23 at 9:39











                • Nice, haven't seen an answer cite the ES5 spec in ages :-) It's totally appropriate here, though.

                  – Bergi
                  Jan 23 at 19:06











                • @AmitDas Always "use strict" mode, and JavaScript becomes a lot less weird.

                  – Bergi
                  Jan 23 at 19:07



















                So if I understand it correctly then if I do not pass any parameters then the arguments object will have length 0. So if I try to access that length 0 object with arguments[0] should it not throw an error? Or is it like there IS a index 0 but just no setter and getter?

                – Amit Das
                Jan 23 at 9:30







                So if I understand it correctly then if I do not pass any parameters then the arguments object will have length 0. So if I try to access that length 0 object with arguments[0] should it not throw an error? Or is it like there IS a index 0 but just no setter and getter?

                – Amit Das
                Jan 23 at 9:30






                2




                2





                The arguments object is defined regardless, and nearly any object can have any property assigned to it at will. For example, const foo = {}; foo[0] = 'bar';. That's kind of similar to your arguments[0] = 2 when a is being called with no arguments - there's no setter or getter on index 0, so you can assign to it without problems, it won't throw an error, but it won't affect anything else.

                – CertainPerformance
                Jan 23 at 9:36







                The arguments object is defined regardless, and nearly any object can have any property assigned to it at will. For example, const foo = {}; foo[0] = 'bar';. That's kind of similar to your arguments[0] = 2 when a is being called with no arguments - there's no setter or getter on index 0, so you can assign to it without problems, it won't throw an error, but it won't affect anything else.

                – CertainPerformance
                Jan 23 at 9:36






                1




                1





                I see. Thanks. Wow JavaScript is weird.

                – Amit Das
                Jan 23 at 9:39





                I see. Thanks. Wow JavaScript is weird.

                – Amit Das
                Jan 23 at 9:39













                Nice, haven't seen an answer cite the ES5 spec in ages :-) It's totally appropriate here, though.

                – Bergi
                Jan 23 at 19:06





                Nice, haven't seen an answer cite the ES5 spec in ages :-) It's totally appropriate here, though.

                – Bergi
                Jan 23 at 19:06













                @AmitDas Always "use strict" mode, and JavaScript becomes a lot less weird.

                – Bergi
                Jan 23 at 19:07







                @AmitDas Always "use strict" mode, and JavaScript becomes a lot less weird.

                – Bergi
                Jan 23 at 19:07















                6














                ECMA 262 9.0 2018 describes this behaviour in 9.4.4 Arguments Exotic Objects with




                NOTE 1:



                The integer-indexed data properties of an arguments exotic object whose numeric name values are less than the number of formal parameters of the corresponding function object initially share their values with the corresponding argument bindings in the function's execution context. This means that changing the property changes the corresponding value of the argument binding and vice-versa. This correspondence is broken if such a property is deleted and then redefined or if the property is changed into an accessor property. If the arguments object is an ordinary object, the values of its properties are simply a copy of the arguments passed to the function and there is no dynamic linkage between the property values and the formal parameter values.




                In short,




                • if in 'sloppy mode', then all arguments are mapped to their named variables, if the length correspond to the given parameter, or


                • if in 'strict mode', then the binding is lost after handing over the arguments.



                This is only readable in an older version of ECMA 262 7.0 2016. It describes this behaviour in 9.4.4 Arguments Exotic Objects with




                Note 1:



                For non-strict functions the integer indexed data properties of an arguments object whose numeric name values are less than the number of formal parameters of the corresponding function object initially share their values with the corresponding argument bindings in the function's execution context. This means that changing the property changes the corresponding value of the argument binding and vice-versa. This correspondence is broken if such a property is deleted and then redefined or if the property is changed into an accessor property. For strict mode functions, the values of the arguments object's properties are simply a copy of the arguments passed to the function and there is no dynamic linkage between the property values and the formal parameter values.







                share|improve this answer






























                  6














                  ECMA 262 9.0 2018 describes this behaviour in 9.4.4 Arguments Exotic Objects with




                  NOTE 1:



                  The integer-indexed data properties of an arguments exotic object whose numeric name values are less than the number of formal parameters of the corresponding function object initially share their values with the corresponding argument bindings in the function's execution context. This means that changing the property changes the corresponding value of the argument binding and vice-versa. This correspondence is broken if such a property is deleted and then redefined or if the property is changed into an accessor property. If the arguments object is an ordinary object, the values of its properties are simply a copy of the arguments passed to the function and there is no dynamic linkage between the property values and the formal parameter values.




                  In short,




                  • if in 'sloppy mode', then all arguments are mapped to their named variables, if the length correspond to the given parameter, or


                  • if in 'strict mode', then the binding is lost after handing over the arguments.



                  This is only readable in an older version of ECMA 262 7.0 2016. It describes this behaviour in 9.4.4 Arguments Exotic Objects with




                  Note 1:



                  For non-strict functions the integer indexed data properties of an arguments object whose numeric name values are less than the number of formal parameters of the corresponding function object initially share their values with the corresponding argument bindings in the function's execution context. This means that changing the property changes the corresponding value of the argument binding and vice-versa. This correspondence is broken if such a property is deleted and then redefined or if the property is changed into an accessor property. For strict mode functions, the values of the arguments object's properties are simply a copy of the arguments passed to the function and there is no dynamic linkage between the property values and the formal parameter values.







                  share|improve this answer




























                    6












                    6








                    6







                    ECMA 262 9.0 2018 describes this behaviour in 9.4.4 Arguments Exotic Objects with




                    NOTE 1:



                    The integer-indexed data properties of an arguments exotic object whose numeric name values are less than the number of formal parameters of the corresponding function object initially share their values with the corresponding argument bindings in the function's execution context. This means that changing the property changes the corresponding value of the argument binding and vice-versa. This correspondence is broken if such a property is deleted and then redefined or if the property is changed into an accessor property. If the arguments object is an ordinary object, the values of its properties are simply a copy of the arguments passed to the function and there is no dynamic linkage between the property values and the formal parameter values.




                    In short,




                    • if in 'sloppy mode', then all arguments are mapped to their named variables, if the length correspond to the given parameter, or


                    • if in 'strict mode', then the binding is lost after handing over the arguments.



                    This is only readable in an older version of ECMA 262 7.0 2016. It describes this behaviour in 9.4.4 Arguments Exotic Objects with




                    Note 1:



                    For non-strict functions the integer indexed data properties of an arguments object whose numeric name values are less than the number of formal parameters of the corresponding function object initially share their values with the corresponding argument bindings in the function's execution context. This means that changing the property changes the corresponding value of the argument binding and vice-versa. This correspondence is broken if such a property is deleted and then redefined or if the property is changed into an accessor property. For strict mode functions, the values of the arguments object's properties are simply a copy of the arguments passed to the function and there is no dynamic linkage between the property values and the formal parameter values.







                    share|improve this answer















                    ECMA 262 9.0 2018 describes this behaviour in 9.4.4 Arguments Exotic Objects with




                    NOTE 1:



                    The integer-indexed data properties of an arguments exotic object whose numeric name values are less than the number of formal parameters of the corresponding function object initially share their values with the corresponding argument bindings in the function's execution context. This means that changing the property changes the corresponding value of the argument binding and vice-versa. This correspondence is broken if such a property is deleted and then redefined or if the property is changed into an accessor property. If the arguments object is an ordinary object, the values of its properties are simply a copy of the arguments passed to the function and there is no dynamic linkage between the property values and the formal parameter values.




                    In short,




                    • if in 'sloppy mode', then all arguments are mapped to their named variables, if the length correspond to the given parameter, or


                    • if in 'strict mode', then the binding is lost after handing over the arguments.



                    This is only readable in an older version of ECMA 262 7.0 2016. It describes this behaviour in 9.4.4 Arguments Exotic Objects with




                    Note 1:



                    For non-strict functions the integer indexed data properties of an arguments object whose numeric name values are less than the number of formal parameters of the corresponding function object initially share their values with the corresponding argument bindings in the function's execution context. This means that changing the property changes the corresponding value of the argument binding and vice-versa. This correspondence is broken if such a property is deleted and then redefined or if the property is changed into an accessor property. For strict mode functions, the values of the arguments object's properties are simply a copy of the arguments passed to the function and there is no dynamic linkage between the property values and the formal parameter values.








                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 29 at 20:32

























                    answered Jan 23 at 8:45









                    Nina ScholzNina Scholz

                    191k15100174




                    191k15100174























                        1














                        This is the undefined value definition from javascript spec :



                        primitive value used when a variable has not been assigned a value.



                        so if you do not specify the function return type it will return undefined.



                        so a() and a(undefined) it is not same thing. returning undefined is based on return type is defined or not.



                        for more clarification similar_problem






                        share|improve this answer
























                        • that is obviously wrong with the arguments object, depending on the mode.

                          – Nina Scholz
                          Jan 23 at 9:16
















                        1














                        This is the undefined value definition from javascript spec :



                        primitive value used when a variable has not been assigned a value.



                        so if you do not specify the function return type it will return undefined.



                        so a() and a(undefined) it is not same thing. returning undefined is based on return type is defined or not.



                        for more clarification similar_problem






                        share|improve this answer
























                        • that is obviously wrong with the arguments object, depending on the mode.

                          – Nina Scholz
                          Jan 23 at 9:16














                        1












                        1








                        1







                        This is the undefined value definition from javascript spec :



                        primitive value used when a variable has not been assigned a value.



                        so if you do not specify the function return type it will return undefined.



                        so a() and a(undefined) it is not same thing. returning undefined is based on return type is defined or not.



                        for more clarification similar_problem






                        share|improve this answer













                        This is the undefined value definition from javascript spec :



                        primitive value used when a variable has not been assigned a value.



                        so if you do not specify the function return type it will return undefined.



                        so a() and a(undefined) it is not same thing. returning undefined is based on return type is defined or not.



                        for more clarification similar_problem







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jan 23 at 8:59









                        sathish kumarsathish kumar

                        36529




                        36529













                        • that is obviously wrong with the arguments object, depending on the mode.

                          – Nina Scholz
                          Jan 23 at 9:16



















                        • that is obviously wrong with the arguments object, depending on the mode.

                          – Nina Scholz
                          Jan 23 at 9:16

















                        that is obviously wrong with the arguments object, depending on the mode.

                        – Nina Scholz
                        Jan 23 at 9:16





                        that is obviously wrong with the arguments object, depending on the mode.

                        – Nina Scholz
                        Jan 23 at 9:16











                        1














                        My understanding is that the arguments object only tracks what is passed into the function. Since you've not initially passed anything, b is not bound and at that point arguments is not 'tracking' b. Next, you assign a value to the initialised but empty Array-like object arguments and finally return b, which is undefined.



                        To delve into this further:




                        If a non-strict function does not contain rest, default, or destructured parameters, then the values in the arguments object do change in sync with the values of the argument variables. See the code below:




                        function func(a) { 
                        arguments[0] = 99; // updating arguments[0] also updates a
                        console.log(a);
                        }
                        func(10); // 99


                        and



                        function func(a) { 
                        a = 99; // updating a also updates arguments[0]
                        console.log(arguments[0]);
                        }
                        func(10); // 99



                        When a non-strict function does contain rest, default, or destructured parameters, then the values in the arguments object do not track the values of the arguments. Instead, they reflect the arguments provided when the function was called:




                        function func(a = 55) { 
                        arguments[0] = 99; // updating arguments[0] does not also update a
                        console.log(a);
                        }
                        func(10); // 10


                        and



                        function func(a = 55) { 
                        a = 99; // updating a does not also update arguments[0]
                        console.log(arguments[0]);
                        }
                        func(10); // 10


                        and



                        // An untracked default parameter
                        function func(a = 55) {
                        console.log(arguments[0]);
                        }
                        func(); // undefined


                        Source: MDN Web docs






                        share|improve this answer


























                        • "at that point arguments is undefined" - that's not true. arguments is not undefined - it has only length equal to 0

                          – puffy.bun
                          Jan 23 at 8:56











                        • @puffy.bun you're absolutely right, I've updated my answer

                          – Barzev
                          Jan 23 at 9:01
















                        1














                        My understanding is that the arguments object only tracks what is passed into the function. Since you've not initially passed anything, b is not bound and at that point arguments is not 'tracking' b. Next, you assign a value to the initialised but empty Array-like object arguments and finally return b, which is undefined.



                        To delve into this further:




                        If a non-strict function does not contain rest, default, or destructured parameters, then the values in the arguments object do change in sync with the values of the argument variables. See the code below:




                        function func(a) { 
                        arguments[0] = 99; // updating arguments[0] also updates a
                        console.log(a);
                        }
                        func(10); // 99


                        and



                        function func(a) { 
                        a = 99; // updating a also updates arguments[0]
                        console.log(arguments[0]);
                        }
                        func(10); // 99



                        When a non-strict function does contain rest, default, or destructured parameters, then the values in the arguments object do not track the values of the arguments. Instead, they reflect the arguments provided when the function was called:




                        function func(a = 55) { 
                        arguments[0] = 99; // updating arguments[0] does not also update a
                        console.log(a);
                        }
                        func(10); // 10


                        and



                        function func(a = 55) { 
                        a = 99; // updating a does not also update arguments[0]
                        console.log(arguments[0]);
                        }
                        func(10); // 10


                        and



                        // An untracked default parameter
                        function func(a = 55) {
                        console.log(arguments[0]);
                        }
                        func(); // undefined


                        Source: MDN Web docs






                        share|improve this answer


























                        • "at that point arguments is undefined" - that's not true. arguments is not undefined - it has only length equal to 0

                          – puffy.bun
                          Jan 23 at 8:56











                        • @puffy.bun you're absolutely right, I've updated my answer

                          – Barzev
                          Jan 23 at 9:01














                        1












                        1








                        1







                        My understanding is that the arguments object only tracks what is passed into the function. Since you've not initially passed anything, b is not bound and at that point arguments is not 'tracking' b. Next, you assign a value to the initialised but empty Array-like object arguments and finally return b, which is undefined.



                        To delve into this further:




                        If a non-strict function does not contain rest, default, or destructured parameters, then the values in the arguments object do change in sync with the values of the argument variables. See the code below:




                        function func(a) { 
                        arguments[0] = 99; // updating arguments[0] also updates a
                        console.log(a);
                        }
                        func(10); // 99


                        and



                        function func(a) { 
                        a = 99; // updating a also updates arguments[0]
                        console.log(arguments[0]);
                        }
                        func(10); // 99



                        When a non-strict function does contain rest, default, or destructured parameters, then the values in the arguments object do not track the values of the arguments. Instead, they reflect the arguments provided when the function was called:




                        function func(a = 55) { 
                        arguments[0] = 99; // updating arguments[0] does not also update a
                        console.log(a);
                        }
                        func(10); // 10


                        and



                        function func(a = 55) { 
                        a = 99; // updating a does not also update arguments[0]
                        console.log(arguments[0]);
                        }
                        func(10); // 10


                        and



                        // An untracked default parameter
                        function func(a = 55) {
                        console.log(arguments[0]);
                        }
                        func(); // undefined


                        Source: MDN Web docs






                        share|improve this answer















                        My understanding is that the arguments object only tracks what is passed into the function. Since you've not initially passed anything, b is not bound and at that point arguments is not 'tracking' b. Next, you assign a value to the initialised but empty Array-like object arguments and finally return b, which is undefined.



                        To delve into this further:




                        If a non-strict function does not contain rest, default, or destructured parameters, then the values in the arguments object do change in sync with the values of the argument variables. See the code below:




                        function func(a) { 
                        arguments[0] = 99; // updating arguments[0] also updates a
                        console.log(a);
                        }
                        func(10); // 99


                        and



                        function func(a) { 
                        a = 99; // updating a also updates arguments[0]
                        console.log(arguments[0]);
                        }
                        func(10); // 99



                        When a non-strict function does contain rest, default, or destructured parameters, then the values in the arguments object do not track the values of the arguments. Instead, they reflect the arguments provided when the function was called:




                        function func(a = 55) { 
                        arguments[0] = 99; // updating arguments[0] does not also update a
                        console.log(a);
                        }
                        func(10); // 10


                        and



                        function func(a = 55) { 
                        a = 99; // updating a does not also update arguments[0]
                        console.log(arguments[0]);
                        }
                        func(10); // 10


                        and



                        // An untracked default parameter
                        function func(a = 55) {
                        console.log(arguments[0]);
                        }
                        func(); // undefined


                        Source: MDN Web docs







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jan 23 at 9:03

























                        answered Jan 23 at 8:55









                        BarzevBarzev

                        8810




                        8810













                        • "at that point arguments is undefined" - that's not true. arguments is not undefined - it has only length equal to 0

                          – puffy.bun
                          Jan 23 at 8:56











                        • @puffy.bun you're absolutely right, I've updated my answer

                          – Barzev
                          Jan 23 at 9:01



















                        • "at that point arguments is undefined" - that's not true. arguments is not undefined - it has only length equal to 0

                          – puffy.bun
                          Jan 23 at 8:56











                        • @puffy.bun you're absolutely right, I've updated my answer

                          – Barzev
                          Jan 23 at 9:01

















                        "at that point arguments is undefined" - that's not true. arguments is not undefined - it has only length equal to 0

                        – puffy.bun
                        Jan 23 at 8:56





                        "at that point arguments is undefined" - that's not true. arguments is not undefined - it has only length equal to 0

                        – puffy.bun
                        Jan 23 at 8:56













                        @puffy.bun you're absolutely right, I've updated my answer

                        – Barzev
                        Jan 23 at 9:01





                        @puffy.bun you're absolutely right, I've updated my answer

                        – Barzev
                        Jan 23 at 9:01











                        0














                        it's because arguments it's not like a Array, it's a object with integer indexed data keys, and property length, And if length equal zero it's mean you don't have a arguments






                        function a(b) {
                        arguments[0] = 2;
                        console.log(arguments.length)
                        return b;
                        }
                        a(1); // length 1 returns 2
                        console.log(a()); // length 0 returns undefined








                        share|improve this answer




























                          0














                          it's because arguments it's not like a Array, it's a object with integer indexed data keys, and property length, And if length equal zero it's mean you don't have a arguments






                          function a(b) {
                          arguments[0] = 2;
                          console.log(arguments.length)
                          return b;
                          }
                          a(1); // length 1 returns 2
                          console.log(a()); // length 0 returns undefined








                          share|improve this answer


























                            0












                            0








                            0







                            it's because arguments it's not like a Array, it's a object with integer indexed data keys, and property length, And if length equal zero it's mean you don't have a arguments






                            function a(b) {
                            arguments[0] = 2;
                            console.log(arguments.length)
                            return b;
                            }
                            a(1); // length 1 returns 2
                            console.log(a()); // length 0 returns undefined








                            share|improve this answer













                            it's because arguments it's not like a Array, it's a object with integer indexed data keys, and property length, And if length equal zero it's mean you don't have a arguments






                            function a(b) {
                            arguments[0] = 2;
                            console.log(arguments.length)
                            return b;
                            }
                            a(1); // length 1 returns 2
                            console.log(a()); // length 0 returns undefined








                            function a(b) {
                            arguments[0] = 2;
                            console.log(arguments.length)
                            return b;
                            }
                            a(1); // length 1 returns 2
                            console.log(a()); // length 0 returns undefined





                            function a(b) {
                            arguments[0] = 2;
                            console.log(arguments.length)
                            return b;
                            }
                            a(1); // length 1 returns 2
                            console.log(a()); // length 0 returns undefined






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 23 at 8:50









                            Vadim HulevichVadim Hulevich

                            775112




                            775112























                                0














                                When you are not providing any parameter then arguments array has length equal to 0. Then you are trying to set the non existing element of array to 2 which causes returning undefined



                                You can simply test this with this snippet:



                                function a(b){ 
                                alert(arguments.length) // It will prompt 0 when calling a() and 1 when calling a(undefined)
                                arguments[0] = 2;
                                return b;
                                }





                                share|improve this answer




























                                  0














                                  When you are not providing any parameter then arguments array has length equal to 0. Then you are trying to set the non existing element of array to 2 which causes returning undefined



                                  You can simply test this with this snippet:



                                  function a(b){ 
                                  alert(arguments.length) // It will prompt 0 when calling a() and 1 when calling a(undefined)
                                  arguments[0] = 2;
                                  return b;
                                  }





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    When you are not providing any parameter then arguments array has length equal to 0. Then you are trying to set the non existing element of array to 2 which causes returning undefined



                                    You can simply test this with this snippet:



                                    function a(b){ 
                                    alert(arguments.length) // It will prompt 0 when calling a() and 1 when calling a(undefined)
                                    arguments[0] = 2;
                                    return b;
                                    }





                                    share|improve this answer













                                    When you are not providing any parameter then arguments array has length equal to 0. Then you are trying to set the non existing element of array to 2 which causes returning undefined



                                    You can simply test this with this snippet:



                                    function a(b){ 
                                    alert(arguments.length) // It will prompt 0 when calling a() and 1 when calling a(undefined)
                                    arguments[0] = 2;
                                    return b;
                                    }






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jan 23 at 8:55









                                    puffy.bunpuffy.bun

                                    1286




                                    1286






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54323086%2fchanging-javascript-functions-parameter-value-using-arguments-array-not-working%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        MongoDB - Not Authorized To Execute Command

                                        in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                                        Npm cannot find a required file even through it is in the searched directory