0
let str = "harsh"

let strArray = [...str]

//strArray is now [ 'h', 'a', 'r', 's', 'h' ]

let addEmptyArr = strArray + []

// addEmptyArr is now 'h,a,r,s,h'

let addSameArr = strArray + strArray

// addSameArr is now 'h,a,r,s,hh,a,r,s,h'

why array and string merging show this type of behaviour

Harsh Mangalam
  • 570
  • 4
  • 14
  • 2
    You're adding two arrays, so `.toString()` will be called on both of them and concatenation will occur – Nick Parsons Mar 13 '21 at 13:00
  • 2
    The `+` operator does not do what you think it does. The array is converted to a string when you "add" it to `[]`, so after that it's just a string, not an array. – Pointy Mar 13 '21 at 13:00

0 Answers0