JS Coding Questions Logo
JS Coding Questions
#129💼 Interview💻 Code

What is an arguments object

Advertisement

728x90

The arguments object is an Array-like object accessible inside functions that contains the values of the arguments passed to that function. For example, let's see how to use arguments object inside sum function,

javascript
1function sum() {
2  var total = 0;
3  for (var i = 0, len = arguments.length; i < len; ++i) {
4  total += arguments[i];
5  }
6  return total;
7  }
8
9sum(1, 2, 3); // returns 6

Note: You can't apply array methods on arguments object. But you can convert into a regular array as below.

javascript
1var argsArray = Array.prototype.slice.call(arguments);

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 45

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
129of476