arguments object (js)

The Arguments Object

In the Create Phase of the Execution context, we know that variables environment, this, and reference to the outer scope is set up. Additionally, it also sets up the arguments object.

Contains the list of all values that we pass into the function we’re calling.

Arguments just another name for parameters we pass into our functions. Parameters and arguments are interchangeable.

In JS, we are given keyword arguments.
During the creation phase, memory space is set for the function definition. Then it has memory space set for the parameters, and assigned them undefined, just like any other variable names.

JavaScript functions have a built-in object called the arguments object.

The argument object contains an array of the arguments used when the function was called (invoked).

This way you can simply use a function to find (for instance) the highest value in a list of numbers:

..or create a sum function

Arguments are Passed by Value

The parameters, in a function call, are the function’s arguments.

JavaScript arguments are passed by value: The function only gets to know the values, not the argument’s locations.

If a function changes an argument’s value, it does not change the parameter’s original value.

For primitive values changes to arguments are not visible (reflected) outside the function. We copy them in and manipulate them inside the function. However, the outside values stay the same.

output:

a: 10, b: 66

However, for objects, it’s different. Objects are being pointed to by references. And when you pass in an object, the parameter name will reference that object. Hence you’ll have the outside var reference obj1 reference your literal object, as well as the parameter ‘objRef’ reference that object.

In // 1, we take the reference that’s pointing to the literal object and access its property ‘name’ and then change it. This will work because we’re directly manipulating a reference that’s pointing to the object.

However, in // 2, you see that we take this parameter reference and point it to ANOTHER literal object with the property address.

When we log the results, you’ll see that obj1 did not change. This is because we didn’t do anything to the original object that obj1 is pointing to. Rather, we just took the parameter reference ‘objRef’ and pointed it somewhere else.