# TL&DRs: What I know about JavaScript String.

Hey! What do you know about `string` type in JavaScript? I believe you saw gazillion times about `1+'1'` or so. In this article, I won't talk about it, but I'd suggest we cover these questions:

1. How strings are stored?
    
2. Is there a limit for the string length?
    
3. Is it possible to overflow the call stack?
    

## How are strings stored?

📝 Strings are stored in Heap. There is a special place for all the strings you're using: string pool:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691227412368/f829ff47-3683-46a5-a6b1-1b5d2456f292.png align="center")

Use DevTools console + memory tab to check how the string pool allocates memory:

1. Open `about://blank` page
    
2. Paste code like this (but do not execute it):
    
3. ```javascript
    const a = 'hello';
    const b = 'world';
    const c = a + b;
    const d = 'hello'
    ```
    
4. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691227999455/9168b3b6-8757-43a1-8e31-3fcce97db25d.png align="center")
    
    Go to the memory tab and take the first heap snapshot. You will get something like this:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691229251138/e2fa2aea-4dab-48f9-8b29-4b2f91006b9f.png align="center")
    
5. Execute the pasted code and take the second snapshot. You can compare snapshots using the select menu on top:
    
6. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691229307895/e0a9d5e7-e65d-4c0f-9816-69407f6b440a.png align="center")
    
    You can see that `hello`, `world` and their concatenation were used only once.
    

### Is there a limit for the string length?

📝 Yes, in V8 (node.js, chrome, etc.) there is a **limit of 512Mb**:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691231503905/36bca473-e603-4110-a690-cc3b39d8f97d.png align="center")

It was done by using:

```javascript
const a = Array.from({length: MB64}).join('a')
b = a + a + a + a + a + a + a + a
```

Further increase in length would lead to the `Invalid string length`exception:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691231866687/eece4107-46b3-45d2-acc2-ac9ed87d31c9.png align="center")

📝 Be careful while processing big JSON entities or huge files in Node.JS or Chrome environment. You might need to use streams / binary blobs while reading data, instead of strings.

📝 Firefox at the same time doesn't have such a limit. You can see on a screenshot a string with about 1GB length:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691232317605/5221fb48-af2d-4176-9f03-4ee7fb45a946.png align="center")

### Is it possible to overflow the stack using strings?

As strings are stored in a heap, the answer is no.

The only thing how you can overflow the stack is by creating too many references, like that:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691233512604/586e50c0-4494-4250-b17d-5f3c32bc5fc9.png align="center")

More context about this trick: [https://blog.xnim.me/js-call-stack-size-exceeded](https://blog.xnim.me/js-call-stack-size-exceeded)
