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

Tell us what’s happening:
I think I understand REST-parameters. But some things are still confusing for me. :cold_sweat:

So I tried 3 things. To test this.

3
Test 1: Above you can see that you cannot put a string and array in 1 sentence. Which makes sense that it doesn’t work

1
Test 2: Above you can see that the REST parameter takes the arguments and puts them into an array.

2
Test3 + QUESTION: Why am I not getting an array here or undefined? :cold_sweat:

BTW: Starting to love freeCodeCamp, you have my donation for sure !! Good excercises + good projects + good support !! :heart: :heart:

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36

Challenge: Use the Rest Parameter with Function Parameters

Link to the challenge:

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

Very good example! oke thank you very much! You explained it very well! :slightly_smiling_face:

And yes will not post Images anymore. :sweat_smile:

Thanks @lasjorg !!

1 Like

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