Skip to main content

Argument vs arguments Object – Learn the Difference

An argument is an optional value we pass to a function's parameter through an invocator.

Example of an Argument

// 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 Editing It

In the snippet above, "Oluwatobi" and "Sofela" are the arguments (values) we passed to the function's firstName and lastName parameters.

So, now that we know what an argument is, we can discuss the arguments object.

What Is a JavaScript arguments Object?

JavaScript arguments is an array-like object built-in to every non-arrow function. It is one of the default properties JavaScript automatically adds to every non-arrow function you define.

The arguments object encases all the values you pass to a function's parameter through the function's invocator.

Example of an arguments Object

// 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 Editing It

The snippet above 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.

Overview

An argument is the value of the built-in arguments object.

In other words, each value you pass to your function's parameter is an argument JavaScript will put into the function's built-in arguments object.

Your support matters: Buy me a coffee to support CodeSweetly's mission of simplifying coding concepts.

Tweet this article