DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • SRE vs. DevOps
  • How To Integrate Microsoft Team With Cypress Cloud
  • Why You Should Consider Using React Router V6: An Overview of Changes
  • Top 10 Engineering KPIs Technical Leaders Should Know

Trending

  • SRE vs. DevOps
  • How To Integrate Microsoft Team With Cypress Cloud
  • Why You Should Consider Using React Router V6: An Overview of Changes
  • Top 10 Engineering KPIs Technical Leaders Should Know
  1. DZone
  2. Coding
  3. Languages
  4. Easy JavaScript Part 4: What Is the Arguments Object in a Function?

Easy JavaScript Part 4: What Is the Arguments Object in a Function?

Welcome back! In Part 4 of this series, we explain how to use this object, and the several different ways it can be employed in our JS code.

Dhananjay Kumar user avatar by
Dhananjay Kumar
·
Oct. 31, 17 · Tutorial
Like (3)
Save
Tweet
Share
4.94K Views

Join the DZone community and get the full member experience.

Join For Free

A JavaScript function has array-like objects called arguments which correspond to the arguments passed to the function. All arguments passed to a JavaScript function can be referred using the arguments object.

Now as we get started, let's consider the code listed here:

function add(num1, num2) {
    var res = num1 + num2;
    return res;
}

var r = add(7, 8);
console.log(r);

In the above function, num1 and num2 are two arguments. You can refer to these arguments using the arguments named num1 and num2. Besides the arguments name, you can also refer to them using a JavaScript array like the object arguments. So, the above function can be rewritten as shown in the listing below:

function add(num1, num2) {
    var res = arguments[0] + arguments[1];
    return res;
}

var r = add(7, 8);
console.log(r);

In JavaScript functions, the arguments object is used to access or refer to all arguments passed to the function. The arguments object is a local variable available to the function. The length of the arguments object is equal to the number of arguments passed to the function. Let us consider the code below, where you'll get 2 as an output because two arguments have been passed to the function:

function add(num1, num2) {
    var res = arguments.length;
    return res;
}

var r = add(7, 8);
console.log(r);

An Arguments Object Is Not a Pure Array

The JavaScript arguments object is not a pure JavaScript array. You cannot perform operations such as push, pop, slice, etc. with the arguments object. As you'll see in the code listed below, doing so will throw an exception because arguments.push is not a function.

function add(num1, num2) {
    arguments.push(78);
    var res = num1 + num2;
    return res;
}

An Arguments Object Can Be Set

You can set a particular item in an arguments object array. To begin, you can set the first item of the array using the index 0 as shown in the listing below:

function add(num1, num2) {
    arguments[0] = 15;
    var res = num1 + num2;
    return res;
}

var r = add(7, 8);
console.log(r);

In the add function, num1 and arguments[0] refer to same value. So, when you update the arguments[0], the value of num1 also get updated. For the above code, the output would be 23.

Convert Arguments Object to an Array

As we've covered already in this post, a JavaScript function arguments object is not a pure array. Other than the length property, it does not have any other property. However, you can convert an arguments object to an array using Array.prototype.slice.call as shown in the listing below:

function add(num1, num2) {
    var arg = Array.prototype.slice.call(arguments);
    console.log(arg.pop());
}

In ECMAScript 6, you can convert the arguments object to an array as shown in the listing below:

function add(num1, num2) {
    var arg = Array.from(arguments);
    console.log(arg.pop());
}

Conclusion

To recap, here are a few important things to remember about the arguments object:

  • The length of the arguments object is equal to the number of arguments (parameters) passed to the function.
  • An arguments object is an array-like object but not a JavaScript array.
  • You cannot use other JavaScript array methods such as push, pop, slice, etc. with the arguments object.
  • The JavaScript arguments object index starts with zero. So the first parameter will be referred by arguments[0], the second parameter will be referred by arguments[1], and so on.

Simply put, a JavaScript arguments object is an array-like object which refers parameters passed to the function. In ECMAScript 6, rest parameters were introduced and are now widely used instead of the arguments object in functions for variable numbers or arguments.

In the next post of this "Easy JavaScript" series, you will learn about the class in JavaScript functions.

Object (computer science) JavaScript

Published at DZone with permission of Dhananjay Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • SRE vs. DevOps
  • How To Integrate Microsoft Team With Cypress Cloud
  • Why You Should Consider Using React Router V6: An Overview of Changes
  • Top 10 Engineering KPIs Technical Leaders Should Know

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: