Can JavaScript string store 100K characters? I've written a script where a string from PHP is passed to a variable in JavaScript. It works fine when it is cut short to almost ten thousand characters but breaks the script when attempting to pass the entire string whose length is a bit greater than 100K. No errors could be found though. Is there any solution for this as to any way of increasing character limit of JavaScript variable? I'm just a beginner. Would appreciate is some one could find a solution for this.
-
What do you mean "breaks the script"? – Xymostech Dec 04 '12 at 06:51
-
2See http://stackoverflow.com/questions/5926263/javascript-object-max-size-limit for some relevant info. My guess is the limit is in PHP, not in JS. – jfriend00 Dec 04 '12 at 06:52
-
5Open the JS console and try this for yourself. `str = ""; for(var i = 0; i < 100000; i++) { str = str + "x"; }` then type `str` and hit enter. See what happens. – Richard Rout Dec 04 '12 at 06:57
-
2my browser crashes.. – Mister Verleg Jan 19 '16 at 15:21
3 Answers
The ECMAScript Standard ECMA-262 (6th Edition, June 2015) says
6.1.4 The String Type
The String type is the set of all ordered sequences of zero or more 16-bit unsigned integer values ("elements") up to a maximum length of 253-1 elements.
So don't plan on using more than 9,007,199,254,740,991 or about 9 quadrillion characters. Of course, you should be prepared for systems which cannot allocate 18 PB chunks of memory, as this is not required for conforming ECMAScript implementations.

- 11,269
- 13
- 67
- 105
-
I like this answer a lot -- I wrote it, after all -- but please remember to vote for Jack's answer if you vote for this. – Charles Apr 24 '23 at 14:27
I think the question is asking about the practical limit, not the spec limit. And, no, it is not always the amount of RAM you have. I have x86_64 24GB PC running Linux Mint with x86_64 Firefox and x86_64 Chrome, and the limits I ran into were:
- 1,073,741,822 limit in Firefox 84
- 536,870,888 limit in Chrome 87
Any higher and Firefox throws a Uncaught RangeError: repeat count must be less than infinity and not overflow maximum string size
, whereas Chrome throws Uncaught RangeError: Invalid string length
. Use the following snippet to run a binary search for the max string length in your browser:
for (var startPow2 = 1; startPow2 < 9007199254740992; startPow2 *= 2)
try {" ".repeat(startPow2);} catch(e) {
break;
}
var floor = Math.floor, mask = floor(startPow2 / 2);
while (startPow2 = floor(startPow2 / 2))
try {
" ".repeat(mask + startPow2);
mask += startPow2; // the previous statement succeeded
} catch(e) {}
console.log("The max string length for this browser is " + mask);

- 4,553
- 2
- 41
- 50
-
2I think this is a fantastic answer and I think it’s a crime that my answer (solid as it may be) has 25x as many votes. Vote for this, people! – Charles Jul 12 '21 at 20:05
There is no theorical limit to JS or PHP on the size of their strings.
I think there are a few possible situations.
Firstly, check that you are not sending your string via HTTP GET. There is a maximum size for GET and i think its dependent on your web server.
Secondly, if you do use POST, check in php.ini for post_max_size and see if it is smaller than the string size you are sending to it as well as your .htacccess file to see if php_value post_max_size is not too small.
Thirdly, check that in php.ini that your memory_limit does not restrict the size of memory that your script can use.
hope this helps.

- 362
- 1
- 5
- 11
-
Thanks for your reply. I've written the javascript in the same php page and I'm assigning the php variable to a javascript variable on the same page. If I echo the variable I can see the output but once assigned to js nothing gets executed below that. – Rohith Dec 04 '12 at 23:12
-
just a shot, but did you regenerate any single quotes or double quotes that can cause your js to break. did you escape them before passing to js? Is there any error in the javascript console? – Aaron Lee Dec 05 '12 at 08:03
-
There is no error in javascript console. No single and double quotes are regenerated as well. – Rohith Dec 06 '12 at 00:35
-
I've even tried assigning the value from php directly to js as in I echoed the output from php and copied it to the script instead of passing it dynamically. Still it did not work. It is strange that it doesn't show any errors. – Rohith Dec 06 '12 at 01:57
-
Sorry, I'm not supposed to as per my company norms. Anyways I can give you a better idea. I'm fetching data from mysql database and storing it as string. It concatenates and becomes very huge depending on the amount of data fetched. This string is passed to javascript variable to generate a graph using jqplot. Everything works well unless the string is around 100 thousand characters long. String of length of almost 50,000 works fine. I thought the jqplot function would be breaking but nothing under the line where the variable is passed gets executed. – Rohith Dec 06 '12 at 03:34
-
I'm not sure if it has something to do with wordpress as I'm using wordpress. I've even tried a different approach. In php instead of creating a string I stored the data as array and passed it as JSON variable but it doesn't work. – Rohith Dec 06 '12 at 03:38
-
It is a wordpress issue. When I wrote the whole script in a new page by calling wordpress header into it instead of writing it directly in a wordpress page, everything worked well. I still don't have any clue as to why it is happening so with wordpress page. – Rohith Dec 07 '12 at 05:26