3

I've defined the function prod which makes use of ES6's default arguments and destructuring:

function prod([a, b, c] = [1, 2, 3]) {
  console.log(a * b * c);
}

When called without arguments, it logs 6 to the console as expected.

prod() // 6

When called with an array of arguments, it logs the correct product:

prod([2, 3, 4]) // 24

When called with a number of arguments, it throws an error:

prod(2, 3, 4) // Uncaught TypeError: undefined is not a function(…)

Why does it throw an undefined is not a function error?

Edit

I understand why it throws an error. What I don't understand is why it throws that particular error.

2
  • Tried prod([2,3,4]) ? Commented Jan 30, 2016 at 20:40
  • I did and it worked as expected. I want to know why the error is undefined is not a function. I'll update the question. Commented Jan 30, 2016 at 20:46

1 Answer 1

2

The issue is that it wants an array for arguments, not 3 separate arguments. So doing this: prod([1, 2, 3]) will give you back your expected answer.

When I run prod(1, 2, 3) in Firefox I get this error: TypeError: [Symbol.iterator] is not a function.

According to the mdn docs (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator#Non-well-formed_iterables)

The function is expecting an interable object to be returned with the method @@iterator. Because that method doesn't exist (and therefore isn't a function) you're getting the is not a function error

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I understand why it throws the error. I want to know why it throws that particular error.
Thank you so much for this. I'll do to test code in Firefox's Dev tools as well. The error in Chrome's dev tools provided much less information. The stack trace wasn't helpful either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.