0

The MDN page on Array#slice states:

For strings, numbers and booleans (not String, Number and Boolean objects), slice copies the values into the new array. Changes to the string, number or boolean in one array do not affect the other array.

Surely string literals, being reference types (admittedly with a value semantic), are not copied. Instead a reference is copied?

3
  • 2
    It literally says that strings are copied and not their reference and therefore not affected in the second sentence. Commented Jun 5, 2017 at 23:02
  • 1
    This comment is simply a restatement of my question. I am questioning MDN's correctness. If I am wrong, I'll accept a short explanation in an answer... Commented Jun 5, 2017 at 23:23
  • 1
    I think engines would optimize that (by not actually copying values), it seems possible to do so as strings are immutable, it won't violate the semantics of slice function. I'm also surprised that the doc mentions that, as I would have expected strings to be not copied. Also, ran a quick test: build a big array of strings, take memory snapshot, then slice that array to produce another big one, take new memory snapshot, compare statistics, result: memory occupied by strings is almost identical between 2 snapshots. Commented Jun 5, 2017 at 23:23

2 Answers 2

2

String literals produce immutable primitive values. Those are not reference values.

That strings might be implemented with shared references to character arrays in JS engines is just that, an implementation detail. As you say yourself, strings do have value semantics in JS, and that's all what matters.

Sign up to request clarification or add additional context in comments.

17 Comments

What if I told you a secret and reveal that some numbers are implemented with references to boxed double-precision floating point numbers as well? :-O
@HuguesMoreau You could ask that question for sure, but it would be a duplicate of Do common JavaScript implementations use string interning?
@BenAston If you only care about the spec, use the term "primitive". There is no official distinction between "value types" and "reference types" in JS. Both objects and primitives are values.
@HuguesMoreau No, string interning does not apply only to constants (although code literals are an obvious and simple target). It works for any dynamically constructed immutable value (see also hash consing) that is used in multiple locations - when it's deemed worth it by the engine.
OK. I can agree with that. But I think MDN is misleading. If I say 'the value of "foo" is copied into the array' then I immediately think that 3*16bits have been copied... when in reality this is very unlikely to have happened.
|
0

No, string literals are considered primitive types in JavaScript, just like numbers.

See MDN - Strings # Distinction between string primitives and String objects

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.