KindOfGuy KindOfGuy - 3 months ago 17
Javascript Question

function rest parameters throw error

running:

$> node restparamstest.js


where:

restparamstest.js


var addTestNotification = function(x, ...theArgs) {
theArgs.forEach(function (post) {
console.log(post);
});
};
addTestNotification(1, 2, 4);


throws:

(function (exports, require, module, __filename, __dirname) { var addTestNotification = function(x, ...theArgs) {
^^^

SyntaxError: Unexpected token ...
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3


on node version:

console.log(process.versions);
{ http_parser: '2.5.2',
node: '4.4.7',
v8: '4.5.103.36',


any ideas? Thanks!

Answer

Rest parameters are fully supported only since node 6.31. If you want to use them with earlier versions of node, you should use the --harmony flag.

You can see ES2015 support by node version here.