0

I am newbie to ES6. Just wanted to know the typeof for the spread parameter. http://es6-features.org/#RestParameter I am not sure why i am not able to print the typeof for spread parameter could someone explain the reason.

Please find the snippet below:

function restify(...x){ 
    console.log(typeof (...x));
}
restify([1,2,3,5,]);
Vemonus
  • 868
  • 1
  • 16
  • 28
Rakesh
  • 1
  • 2

2 Answers2

0

You should use the spread operator only in the parameters definition

const restify = (...x) => {
  console.log(typeof x);
}

restify([1, 2, 3, 4, 5]);

Which will print Object. I used const and arrow functions which are also ES6 features, check out http://es6-features.org/

pridegsu
  • 1,108
  • 1
  • 12
  • 27
  • 1
    _"You should use the spread operator only in the parameters definition"_ Not sure what you mean by "only in the parameters definition"? Note `...` is not an operator http://stackoverflow.com/questions/37151966/what-is-spreadelement-in-ecmascript-documentation-is-it-the-same-as-spread-oper – guest271314 Dec 06 '16 at 15:40
  • `...` is an operator, check out: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator – pridegsu Dec 06 '16 at 15:41
  • 1) The article you are referring to says spread **syntax**, not operator. 2) it has nothing to do with the question. The OP uses rest parameters – a better oliver Dec 06 '16 at 15:46
  • @zeroflagL OP does use spread element within body of function. – guest271314 Dec 06 '16 at 15:48
  • @zeroflagL did you see the link? "Operators/Spread_operator". Another reference (from microsoft): https://msdn.microsoft.com/en-us/library/dn919259(v=vs.94).aspx – pridegsu Dec 06 '16 at 15:48
0

Use typeof x.

function restify(...x){ 
    console.log(typeof x);
}
restify([1,2,3,5,]);
guest271314
  • 1
  • 15
  • 104
  • 177
  • Thanks for the reply. I got the answer with the explanation – Rakesh Dec 07 '16 at 02:08
  • @Rakesh No worries. _"I got the answer with the explanation"_ Not sure what you mean? What was expected result of calling `typeof (...x)`? – guest271314 Dec 07 '16 at 02:10
  • 1
    Apparently i was using in the wrong way. If i need to print the typeof spread variable there is no reason to add the spread operator i was using it in the wrong way that was the explanation i understood from the above answers. – Rakesh Dec 07 '16 at 07:53
  • @Rakesh Was not sure if you were trying to call `typeof` on elements of the array – guest271314 Dec 07 '16 at 07:54