• Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
test
Code4Developers

Code4Developers

Code4Developers
  • Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
  • JavaScript

arguments object in JavaScript function

  • Yatendrasinh Joddha
  • March 19, 2018
  • 2 minute read
javascript
Total
0
Shares
0
0
0

The arguments object is an Array-like object matching to the arguments passed to a function. The arguments object is a local variable available within all (non-arrow) functions. You can refer to a function’s arguments within the function by using the arguments object.Let’s consider below code:

function addition(num1, num2) { 
  return num1 + num2; 
} 
addition(10,20);

In above code, we are calling function “addition()” passing 10 and 20 for “num1” and “num2”. So, the function will return an answer 30. Now what if we pass one more arguments while calling function “addition()”? Let’s do it:

function addition(num1, num2) { 
  return num1 + num2; 
} 
addition(10,20,25);

Will this function “addition()” throw an error? Answer is no. It will return an answer 30. So, here comes one more question. What happen to the value 25? Answer is, it will be there in arguments object.

Let’s consider below code to access all the passed arguments using arguments object

function addition(num1, num2) { 
  return arguments[0] + arguments[1] + arguments[2]; 
} 
addition(10,20,25);

In above code, we are able to access all the passed arguments and the function “addition()” will return an answer 55 here. In JavaScript we can access all the passed arguments using arguments objects and this object contains an entry for each argument passed to the function, the first entry’s index starting at 0.

arguments object is an array-like object

In above example we can see the code where we are accessing arguments object same as we access an array. But we can’t say arguments object a pure array, because it does not provide all the properties which array provides. It provides only length property which make it an array-like object. If you will try anything else than length in arguments object it will throw an exception calling “…is not a function”.

Modify an arguments object

We can modify or set an value to the arguments object. Refer below code:

function addition(num1, num2) { 
  arguments[0] = 5;
  return arguments[0] + arguments[1] + arguments[2]; 
} 
addition(10,20,25);

We have used same code as our previous example, here we are setting first argument to the number 5 and it will get updated. So, this function “addition()” will return an answer 50.

Convert arguments object to a pure array

As we discussed, arguments object is not an array, but it is array like object. So, how to convert it to the pure array. Let’s consider below code

var args = Array.prototype.slice.call(arguments);

OR

var args = [].slice.call(arguments);

In ECMAScript 6 we can achieve same as below

var args = Array.from(arguments);

We can also do it using spread operator

var args = [...arguments];

Interested in JavaScript? Click here to read more JavaScript articles in Code4Developers

Yatendrasinh Joddha
Yatendrasinh Joddha

I have healthy knowledge and experience in Azure, O365 and ASP.Net with c#, MVC, WebApi along with client-side frameworks and libraries like JavaScript, JQuery, KnockoutJs, AngularJs, Angular, ReactJs, NodeJs

Views: 8,786

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related Posts

Total
0
Shares
Share 0
Tweet 0
Pin it 0

Leave a ReplyCancel reply

Subscribe to Website via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts
  • React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    1 year ago
  • Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    2 years ago
  • Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    2 years ago
  • Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    2 years ago
  • Angular Custom Elements: Creating Reusable Components with Angular

    Angular Custom Elements: Creating Reusable Components with Angular

    2 years ago
  • Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    5 years ago
  • How to Build an Inclusive Esports Community

    How to Build an Inclusive Esports Community

    5 years ago
  • Best Digital Icebreakers

    Best Digital Icebreakers

    5 years ago
  • Email alerts when a docker container stopped in AWS ECS CLUSTER

    Email alerts when a docker container stopped in AWS ECS CLUSTER

    5 years ago
  • New Learning Models for Fall 2020

    New Learning Models for Fall 2020

    5 years ago
Subscribe to Website via Email

Enter your email address to subscribe to this website and receive notifications of new posts by email.

Featured Posts
  • javascript 1
    Spread syntax (three dots) in JavaScript
    • March 21, 2018
  • Angular 2
    Angular 6 CRUD – Part 1: Project Setup, Routing, Service
    • May 9, 2018
  • javascript 3
    Local Storage and Session Storage
    • May 22, 2017
  • Angular 4
    Angular 4 Project Structure
    • June 18, 2017
  • AWS 5
    Email alerts when a docker container stopped in AWS ECS CLUSTER
    • July 24, 2020
Code4Developers
Learning is never ending process

Input your search keywords and press Enter.

%d