Saturday, October 14, 2023
HomeSoftware DevelopmentHow are characters saved in JavaScript ?

How are characters saved in JavaScript ?


Enhance Article

Save Article

Like Article

Enhance Article

Save Article

Like Article

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 scale 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’ll retailer characters in variables.

Javascript

var char1 = 'a';

var char2 = 'a';

var char3 = 'b';

  

console.log(typeof(char1));

console.log(char1 == char2);

console.log(char1 == char3);

Output: Right here, we are able to 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 scale back reminiscence utilization.

string
true
false

Instance 2: On this instance, we’ll retailer characters with the identical worth however at completely different reminiscence places.

Javascript

var char1 = new String('a');

var char2 = new String('a');

var char3 = 'a';

  

console.log(typeof(char1));

console.log(char1 == char2);

console.log(char1 == char3);

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 regardless that it accommodates 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

Like Article

Save Article



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments