2

Spread Operator throws error for float and boolean variable. Is there any specific reason for getting error on float and boolean variable.

// Works, Array Variable
'use strict';

let aVal = [1, 2, 3];
console.log(...aVal);

// Works, String Variable
'use strict';

let sVal = 'String';
console.log(...sVal);

// throws error, Integer Variable
'use strict';

let iVal = 1234567890;
console.log(...iVal);

// throws error, Float Variable
'use strict';

let fVal = 99.45;
console.log(...fVal);

// throws error, Boolean Variable
'use strict';

let bVal = true;
console.log(...bVal);
Venkat.R
  • 7,420
  • 5
  • 42
  • 63
  • 4
    Yes, because you can't iterate over them. What result would you expect from spreading a float for example? – Thomas Aug 09 '16 at 11:45

1 Answers1

8

Is there any specific reason for getting error on float and boolean variable.

Yes: Spread syntax (it isn't an operator) only works with iterable objects (objects that implement iteration). Numbers and booleans are not iterable. Things like arrays and maps and sets are iterable.

console.log(...aVal); asks the JavaScript engine to iterate through aVal and then call console.log with each iterated value as a discrete argument. That is, it asks javaScript to "spread out" that iterable.

Here's an example of spread with an iterable (in this case, an array):

function foo(a, b, c, d) {
  console.log(a);
  console.log(b);
  console.log(c);
  console.log(d);
}
let a = ["the", "answer", "is", 42];
foo(...a);

Note how the entries in a are "spread out" into discrete (separate) arguments for foo.


The examples below were from earlier when your question was asking about "the rest operator" by mistake. Just for completeness:

Here's an example of rest syntax (also not an operator) in a function signature:

function foo(...args) {
  console.log(`I got ${args.length} args`);
}
foo('a', 'b', 'c');

...and rest syntax in a destructuring assignment:

let a = ['a', 'b', 'c', 'd', 'e'];
let [ x, y, ...z ] = a;
console.log(x);
console.log(y);
console.log(z);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    @FelixKling: Yeah, it's syntax, not an operator. – T.J. Crowder Aug 09 '16 at 15:53
  • @T.J.Crowder, what is difference by calling as spread syntax when compare to spread operator !! – Venkat.R Aug 09 '16 at 16:15
  • You meant to say. below pages need to be corrected ? http://es6-features.org/#SpreadOperator https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator – Venkat.R Aug 09 '16 at 16:22
  • 1
    @Venkatraman: Yup. There is no "spread operator." In fact, there can't be: No operator could do what spread syntax does. People use the term casually, but [the spec](http://www.ecma-international.org/ecma-262/7.0/index.html) doesn't, for good reason. Operators take operands and produce a resulting value. There's no value `...` could produce in `foo(...array)` that would then allow `foo` to be called with discrete arguments. (The spec isn't shy about using the word "operator" for things that actually are operators.) – T.J. Crowder Aug 09 '16 at 16:24
  • @Venkatraman: It might have been possible to define a meta-value for specification purposes and then call it an operator, but they didn't go that way, and [it leads to real-world differences](http://stackoverflow.com/questions/35019557/using-spread-operator-multiple-times-in-javascript). – T.J. Crowder Aug 09 '16 at 16:27
  • You mean to say, we have to call it as Spread syntax. can we change it in MDN ? – Venkat.R Aug 09 '16 at 16:29
  • @Venkatraman: Of course we can, MDN is community-edited. I don't have time to right now, but you're right we should. – T.J. Crowder Aug 09 '16 at 16:30
  • Nice. Will do. I accept your answer. hope you like my observation on this topic as a question. – Venkat.R Aug 09 '16 at 16:31
  • @Venkatraman: I usually use terms depending on the context: spread parameter `foo(...bar)`, spread element `[...foo]`, rest argument `function(...bar) {}`, rest element (destructuring) `[...bar] = foo;`. – Felix Kling Aug 09 '16 at 21:02
  • 1
    @FelixKling: Yeah. "Syntax" for the general case, but then more specific for the specific case. – T.J. Crowder Aug 10 '16 at 05:55