What is the maximum length of a variable name in JavaScript?
-
On a related note: I'd assume that name length has no affect on run-time performance after it is compiled with JavaScript JIT compilers. Perhaps just a minor initial parsing slow-down and a slightly longer js file download time on un-minified code. Another thing: all characters in a name are significant. In contrast with the C64 Basic interpreter where you can make variable names very long, but the interpreter only paid attention to the first 2 characters. – user3015682 Jan 23 '15 at 21:09
-
1I guess it is infinite... Haha this is a good question :P – Nieck Jun 04 '16 at 21:20
2 Answers
Just as I have tested earlier on:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
/* <![CDATA[ */
var aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 'test';
alert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa );
/* ]]> */
</script>
</body>
</html>
This script works as per fine. The variable name is longer than 512 characters.
My guess is that since Javascript is a interpreted language (or aka scripting language), the variable name length does not matter as long as it refers to a defined variable.

- 42,982
- 15
- 99
- 131
-
12This is correct. The ECMAScript spec doesn't mention any limits (at least that I've found) and variables are just (conceptually at least) properties of the function call's activation object (or the global object). Since properties can be arbitrary strings, the limit would be the maximum length of a string. – Matthew Crumley Nov 27 '09 at 17:18
-
what about json field name length ? can that be as long as variable names in javascript? for example 100 characters long – sarfarazsajjad Oct 07 '14 at 10:00
-
1@Saifee LMGTFY - I reckon this post answers your question well: http://stackoverflow.com/questions/13367391/is-there-a-limit-on-length-of-the-key-string-in-js-object – mauris Oct 07 '14 at 13:00
-
1@mauris I know this is old but JS is NOT a scripting language, it is compiled – jk121960 May 31 '21 at 22:58
I've tried the example above, x10 the amount of characters, then again, then again, then again.
When the variable length is of 513k characters, my Notepad++ is slugging along like a snail. Yet when I managed to save the file and run it locally, the alert appears without any delay.
Tested on: Chrome 19.0.1084.52, IE9, FireFox 11.0, Safari 5.1.5, Opera 11.62
Since JSBin only allows a maximum of 65535 characters within the <script></script>
tags, I only managed to create a page with a 32.5k length variable: http://jsbin.com/ukunow/10

- 86,231
- 106
- 366
- 634