Skip to main content

Arguments in JavaScript – What Exactly Is an Argument?

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 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 arguments Mean in Technical Terms?

Technically, arguments is an array-like object built-in to every non-arrow 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 include as part of the function's built-in arguments' values.

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

If you like this article, please Tweet it.

Thanks for reading!