How are strings saved in JavaScript ?

On this article, we are going to attempt to perceive how the Strings are saved in JavaScript. Strings are a primitive knowledge sort in JavaScript and are allotted a particular place within the reminiscence to retailer and manipulate. The particular place allotted within the reminiscence is called String Fixed Pool. The string fixed pool is a small cache that resides throughout the heap. JavaScript shops all of the values contained in the string fixed pool on direct allocation.
In different phrases, the string fixed pool exists primarily to cut back reminiscence utilization and enhance the reuse of present cases in reminiscence. There are some circumstances the place we would like a definite String object to be created regardless that it has the identical worth. On this case, we use the new key phrase.
Allow us to now perceive with a fundamental instance, how strings are saved in reminiscence.
Instance 1: On this instance, we are going to create Strings and perceive how they’re saved in reminiscence.
Javascript
|
Output:
true false
Beneath is the diagrammatic illustration of how the strings within the above instance shall be saved in reminiscence.

How are strings saved in javascript?
We will see that str1 and str2 level on the similar location within the reminiscence whereas a brand new area is created for str3 because it has a special worth. On this approach string fixed pool saves reminiscence by making the identical worth string level to the identical location within the reminiscence.
Instance 2: On this instance, we are going to make the strings with the identical worth discuss with completely different areas in reminiscence
Javascript
|
Output:
false false

How are strings saved in javascript?
We will see within the picture that regardless that str1 and str2 are having the identical worth however due to the brand new key phrase they’re referring to completely different areas within the reminiscence. Therefore, they return false compared.
Conclusion:
When creating the strings utilizing quotations(” “) they’re instantly saved within the String Fixed Pool the place equal values discuss with the identical location within the reminiscence. Whereas, when strings are created utilizing the brand new key phrase, a brand new occasion is at all times created within the heap reminiscence then the worth is saved within the String Fixed Pool due to this even when the info saved is similar nonetheless the strings won’t be equal.