How are characters saved in JavaScript ?

In lots of languages like C, C++, Java, and so forth. char is a primitive knowledge sort however in JavaScript, there is no such thing as a particular knowledge sort to retailer characters. Internally JavaScript shops characters as String solely. In JavaScript, a String of size solely is taken into account a personality. Since a personality can be a string so character additionally will get saved in a string fixed pool.
The string fixed pool is a small cache that resides inside the heap. JavaScript shops all of the values contained in the string fixed pool on direct allocation. The string fixed pool exists primarily to cut back reminiscence utilization and enhance the reuse of present situations in reminiscence.
Allow us to now perceive with a fundamental instance, how characters are saved in reminiscence.
Instance 1: On this instance, we are going to retailer characters in variables.
Javascript
|
Output: Right here, we will see that internally all of the characters are being saved as String and strings with the identical worth seek advice from the identical location within the reminiscence to cut back reminiscence utilization.
string true false
Instance 2: On this instance, we are going to retailer characters with the identical worth however at completely different reminiscence places.
Javascript
|
Output: We are able to observe, that now the character is being saved as an object and every new occasion of an object is allotted a brand new location within the reminiscence although it incorporates the identical worth.
object false true
Conclusion:
Once we are storing characters utilizing simply citation(”) marks then variables having the identical worth are saved at similar location within the String Fixed Pool but when we use new key phrase then distinct reminiscence location is assigned to every worth even when they’ve the identical worth