Why might JavaScript not allocate memory?

The example on memory Management of

MDN mentions that JavaScript may decide not to allocate memory and does not know how to understand it:

var s = "azerty";
var s2 = s.substr(0, 3); // s2 
// 
// JavaScript 
//  [0-3] 

var a = ["ouais ouais", "nan nan"];
var a2 = ["generation", "nan nan"];
var a3 = a.concat(a2); 
//  a  a2 
Does

mean not allocating memory to the variable S2? Just how is the storage [0-3] range stored?

The method

substr is defined on top of String.prototype, and s has already executed new String (s) once when substr is executed as a string. Normally, S2 should also be saved in memory, so is it possible to save S2 in the form of an object, that is, to save the range of [0-3]?


looked up some data to get a general idea of how it works.

var s = "azerty";
var s2 = s.substr(0, 3);
In the

example, s and S2 are basic data types and are stored in stack memory, and a feature of stack memory is data sharing.

JavaScript first creates a variable reference as s in the stack, then looks up whether there is a value of azerty in the stack, and if it doesn't find it, stores the azerty, and then points s to azerty.

then create a variable as a S2 reference to find out whether there is a value of aze in the stack, because azerty, already exists in the stack, so the example says, "JavaScript may decide not to allocate memory." S2 only points to the range of [0-3] of azerty, that is, s and S2 share azerty.

based on this, JavaScript creates a variable S2, but instead of allocating another memory to hold its value aze, S2 points to the 0-3 range of the existing azerty in the stack.

memory usage rules in JavaScript-heap and stack
Menu