-1

How can I insert both a string and an array to a return function and the array doesn't change??

function name(){
   let array = [1, 2, 3]
   return "Hello" + array  // I want Hello [ 1, 2, 3]
}

return array is equal to [ 1, 2, 3 ].

But return "Hello" + array is equal to Hello1,2,3.

What I want is Hello [ 1, 2, 3 ]

2 Answers2

0

Like that?

function name(){
   let array = [1, 2, 3]
   return "Hello [ " + array.join(', ') + " ]";  // I want Hello [ 1, 2, 3]
}
Yuri Molodyko
  • 590
  • 6
  • 20
0

As said in the comments thanks to Jared Farrish, you can simply use

'Hello' + JSON.stringify([1,2,3,])

For example

let array = [1,2,3];
console.log(JSON.stringify([ 1, 2, 3])) // [ 1, 2, 3]

You can check more use of the function by clicking JSON.stringify

Staxlinou
  • 1,351
  • 1
  • 5
  • 20