5 Replies - 684 Views - Last Post: 07 May 2020 - 12:45 PM

#1 johnywhy   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 152
  • Joined: 07-April 20

How to Assign ...array Concatenation to Variable?

Posted 07 May 2020 - 11:29 AM

Given:

ary = ['a', 'b', 'c']


i discovered these two statements produce identical results (in chrome):

console.log(ary.join())
console.log(...ary)


I hadn't seen the spread operator used this way, but it appears to concat the elements same as `join`.
(i understand that, normally, you'd use `[...ary]` or just `ary`, depending on your purpose).

How to apply `...ary` to a string variable?

These raise an error `Unexpected token '...'`

let str = ...ary
// or
let str = (...ary)


update: error in my code fixed. I had used parens where i meant square brackets.

This post has been edited by johnywhy: 07 May 2020 - 01:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to Assign ...array Concatenation to Variable?

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15686
  • View blog
  • Posts: 62,833
  • Joined: 12-June 08

Re: How to Assign ...array Concatenation to Variable?

Posted 07 May 2020 - 11:43 AM

They are not the same thing. Spread breaks apart an array into separate elements, and join returns a string concatenated with the elements.
Was This Post Helpful? 1
  • +
  • -

#3 DarenR   User is offline

  • D.I.C Lover

Reputation: 686
  • View blog
  • Posts: 4,436
  • Joined: 12-January 10

Re: How to Assign ...array Concatenation to Variable?

Posted 07 May 2020 - 11:51 AM

to do what you want dont use spread
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • a lovely bit of linq
  • member icon

Reputation: 6891
  • View blog
  • Posts: 28,511
  • Joined: 12-December 12

Re: How to Assign ...array Concatenation to Variable?

Posted 07 May 2020 - 12:22 PM

('a','b','c') is not an array, that would be with square brackets.

Note that you cannot effectively use console.log to make such comparisons, the console produces its own display of the data.
Was This Post Helpful? 2
  • +
  • -

#5 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2679
  • View blog
  • Posts: 7,898
  • Joined: 15-January 14

Re: How to Assign ...array Concatenation to Variable?

Posted 07 May 2020 - 12:38 PM

For comparison:

var ar = [1, 2, 3]; console.log(ar.join()); console.log(...ar); console.log(1, 2, 3);

Was This Post Helpful? 0
  • +
  • -

#6 ge∅   User is offline

  • D.I.C Lover

Reputation: 318
  • View blog
  • Posts: 1,334
  • Joined: 21-November 13

Re: How to Assign ...array Concatenation to Variable?

Posted 07 May 2020 - 12:45 PM

when you write
console.log(...["a", "b", "c"]);
what you do is
console.log("a", "b", "c")
and it prints
"a" "b" "c"

You won't see the quote signs but if you want to be convinced, replace ["a", "b", "c"] by ["a", 2, "c"]

When you write
console.log(["a", "b", "c"].join(" "));
what you do is
console.log("a b c");
and it prints
"a b c"


-
edited for clarity

This post has been edited by ge∅: 07 May 2020 - 01:51 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1