Accessing Arguments Using the Arguments Object

/ Published in: JavaScript
Save to your folder(s)

Every function has an object called the arguments object. This is an object that contains all the arguments that were passed to the function. The example below shows how to access these. Very rarely used but useful to know. Note: it may look like an array, but it's better to think of it as an object with numbered properties.

Copy this code and paste it in your HTML
  1. function containerScope(){
  2. var a = [];
  3. var b = {};
  4. var c = true;
  5. var d = 22;
  6. function argumentsObject(a, b, c, d){
  7. console.log(arguments.length);
  8. console.log(arguments[0]);//Array
  9. console.log(arguments[1]);//Object
  10. console.log(arguments[2]);//true
  11. console.log(arguments[3]);//22
  12. arguments[3] = 55;//I have now changed d as well!
  13. console.log(arguments[3]);
  14. console.log(d);
  15. }
  16. argumentsObject(a, b, c, d);
  17. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.