I'm reading Professional JavaScript for Web Developers 3rd ed. and in the summary of chapter 4 one can read:
Two types of values can be stored in JavaScript variables: primitive values and reference values. Primitive values have one of the five primitive data types: Undefined, Null, Boolean, Number, and String. Primitive and reference values have the following characteristics:
- Primitive values are of a fixed size and so are stored in memory on the stack.
But I can have different strings, say:
var a = "ABC";
// or
var b = "Some very irritatingly long string..."
They clearly differ in size, so how can they be allocated on the stack?
I believe the same question can be asked about numbers...
So I am for sure missing something important here.
Can someone explain why strings/numbers are of fixed size and how they can be stored on stack?
Primitive values are of a fixed size and so are stored in memory on the stack.
sentence is not very accurate. String has fixed char size, but is generally a dynamically sized type. I don't see how would that make it impossible to keep it on the stack. – Samsara+
in sequence for substring accumulation is bad practice (because slow) and you should use array.join() instead. – Twentyfourmo