Valid javascript?

I found this somewhere online discussing new features.

function sum(a){
  let sum =0;
  for(const x of a){
    sum += x}

Is this valid? if so where/how can I play with it?

In VS code I get TypeError: a is not iterable, and
the same thing in codepen as well.

You are trying to loop over a number, which isn’t possible.

for (const x of a) is saying go through the ordered collection of values a, and for every one do something with the current value (x).

A number isn’t a collection of values, it’s just a number — it doesn’t make any logical sense to say for (const x of 4), which is what you’re doing when you try to run sum(4)

The error is saying you’re trying to iterate over something that isn’t iterable (iterable things are ordered collections of values: an array, a string, a map or a set)

Well it did not make sense to me either.

But now as to your explanation I see if you pass in an array it does make sense, and works.

Ok, now I will check it…