Sobes.tech
Middle+

What new features from the ES6 standard are used in the code you wrote? What is the arguments object and how does it differ from rest parameters?

sobes.tech AI

Answer from AI

In the ES6 (ECMAScript 2015) standard, several important innovations appeared that are often used in modern JavaScript code:

  • Rest parameters (...args) — allow collecting an arbitrary number of function arguments into an array.
  • Arrow functions — a concise syntax for functions with lexical this.
  • Destructuring — a convenient way to extract values from arrays and objects.
  • Template strings — for easy string formatting.

The arguments object is a special object available inside all regular functions that contains all passed arguments. It is similar to an array but is not a true array (it lacks array methods like map).

Differences between arguments and rest parameters:

  • arguments exists only in regular functions; it is not available in arrow functions.
  • arguments is not an array but array-like; rest parameters are true arrays.
  • Rest parameters explicitly specify which arguments to collect and only work with remaining parameters.
  • Rest parameters are more convenient and modern; it is recommended to use them instead of arguments.

Example with rest parameters:

function sum(...numbers) {
  return numbers.reduce((acc, n) => acc + n, 0);
}

console.log(sum(1, 2, 3)); // 6

Using arguments:

function sum() {
  let total = 0;
  for(let i = 0; i < arguments.length; i++) {
    total += arguments[i];
  }
  return total;
}

console.log(sum(1, 2, 3)); // 6
What new features from the ES6 standard are used in… - sobes.tech