Why does the array become a string? (rest parameter)

Please post code and not images.

Use a comma , to separate the log values instead. I assume toString is called on the array when doing string concatenating.

const sum = (...args) => {
  console.log(Array.isArray(args))
  console.log(args)
  console.log('testing ' + args + ' testing')
  console.log('testing ', args, ' testing')
  console.log('testing ', args.toString(), ' testing')
}

console.log(sum(1,2,3))

// true
// (3) [1, 2, 3]
// testing 1,2,3 testing
// testing  (3) [1, 2, 3]  testing
// testing  1,2,3  testing

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like