/ / Arguments in JavaScript: What Exactly Is an Argument?

Arguments in JavaScript: What Exactly Is an Argument?

What is the arguments object in JavaScript?

There are two ways by which you could define arguments in JavaScript. The first is the basic definition, while the second is technical.

Let’s discuss both ways, starting with the basic one.

What does JavaScript arguments mean in basic terms?

Arguments are optional values you may pass to a function’s parameter through an invocator.

Here’s an example:

// Declare a function with two parameters:
function myName(firstName, lastName) {
  return firstName + " " + lastName;
}

// Invoke myName() function with two arguments:
myName("Oluwatobi", "Sofela");

// The invocation above will return:
"Oluwatobi Sofela"

Try it on StackBlitz

In the snippet above, while invoking myName(), we passed “Oluwatobi” and “Sofela” to the function’s firstName and lastName parameters.

In other words, the “Oluwatobi” and “Sofela” arguments are the values we’ve assigned to the function’s parameters.

So now that we know the basic definition of an argument, we can talk about the technical meaning.

What does JavaScript arguments mean in technical terms?

Technically, arguments is an array-like object built-in to every nonarrow function.

In other words, an arguments object is one of the default properties JavaScript automatically adds to every non-arrow function you define.

Keep in mind that the purpose of the arguments object is to encase all the values you passed to a function’s parameter through the function’s invocator.

Here’s an example:

// Declare a function with two parameters:
function myName(firstName, lastName) {
  return arguments;
}

// Invoke myName() function with two arguments:
myName("Oluwatobi", "Sofela");

// The invocation above will return:
["Oluwatobi", "Sofela"]

// Note: Some browser’s output may look like so:
{0: "Oluwatobi", 1: "Sofela"}

Try it on StackBlitz

Notice that we programmed myName() to return the content inside the function’s built-in arguments object.

Therefore, when we invoked myName(), the function returned an array-like object containing the values we passed to the function’s parameters.

Just in case you are wondering what an array-like object is, let’s discuss it below.

What is an array-like object?

An array-like object is an object that has only a few — not all — of the features of a regular JavaScript array.

Array vs array-like objects: What’s the difference?

The main similarities between a regular array and an array-like object are:

  • They both have the length property.
  • The two object’s properties start from the index number zero (0).

But the main difference between the two objects is that an array-like object does not have built-in methods like map(), pop(), and forEach(). However, a regular array has those methods.

Note

You can use the spread operator or the Array.from() method to convert an array-like object to a regular array.

In summary

Each value you pass to your function’s parameter is an argument JavaScript will incorporate as one of the items of the function’s built-in arguments array-like object.

And there you have it! An argument is a value of the built-in arguments object.

Thanks for reading!

Get Helpful Resources in Your Inbox

A CodeSweetly Digest

Similar Posts