doorfly doorfly - 7 months ago 25
Node.js Question

Object has no method 'reduce' error when using arguments in node.js?

Why do I get an error when using

like this?

function sum(){
return arguments.reduce(function(a,b){
return a+b;



return arguments.reduce(function(a,b){
TypeError: Object #<Object> has no method 'reduce'
at sum (/Users/bob/Documents/Code/Node/hello.js:2:19)
at Object.<anonymous> (/Users/bob/Documents/Code/Node/hello.js:8:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:903:3

This is from Mr. Crockford's JS lectures.


arguments is not a real array, it's an "array-like" object and reduce is not a method of array-like objects. You can use reduce by passing arguments as context, like this:

[], function(a, b) {


Edit: more info on array-like objects here at the MDN.