Spread Operator

Trying to figure out why the spread operator is needed in the code below in order for it to work. Also, why you can’t use spread in situations like “let string = ...string” . Any insight would be greatly appreciated.

function binaryAgent(str) {

  return String.fromCharCode(
    ...str.split(" ").map(function(char) {
      return parseInt(char, 2);
    })
  );
}

You can’t do that

the spread has the lowest priority, it works at the end
It’s equivalent of this:

function binaryAgent(str) {
  const arrayOfNumbers = str.split(" ").map(function(char) {
    return parseInt(char, 2);
  })
  return String.fromCharCode(...arrayOfNumbers);
}
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.