Why can't I use every method on arguments array?

Second call to every method works fine.

function addTogether() {
  let nums = [2, 3]; 
  console.log(arguments[1])
  console.log(typeof arguments)
  console.log(arguments.every(item => typeof item === 'number'));
  console.log(nums.every(item => typeof item === 'number'))
  return false;
}

addTogether(2,3);

It’s not an array, it’s an object, and every is a method for arrays, so you can’t use it. Options:

  1. Preferably use the rest operator instead, any arguments are then accessible as an array straightaway:
    function addTogether(...nums) {
    
  2. Convert to an array if you have to use arguments and you need it to be an array:
    function addTogether() {
      let nums = Array.from(arguments);
    

Ok so then why does this print out false? Second item is NaN.

function addTogether(...nums) {
  console.log(nums.every(item => typeof item !== 'number'))
  return false;
}

addTogether(2, '3');

There you’re checking if every item is not a number. If you negate that, it’s the opposite of having at least one number.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.