Unshift & Join on JavaScript String

var hello = 'hello';

Array.prototype.unshift.call(hello, '11') // gives error

Array.prototype.join.call(hello, ', ') // works, why??

Because strings are immutable, and unshift tries to assign to an index (property) of the string.

join doesn't assign anything and only reads properties, therefore it works with any object that has .length.

A String object is an exotic object that encapsulates a String value and exposes virtual integer indexed data properties corresponding to the individual code unit elements of the String value. Exotic String objects always have a data property named "length" whose value is the number of code unit elements in the encapsulated String value. Both the code unit data properties and the "length" property are non-writable and non-configurable.

http://www.ecma-international.org/ecma-262/6.0/#sec-string-exotic-objects sec-string-exotic-objects

Ref: georg@stackoverflow