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.– user3015682Commented Jan 23, 2015 at 21:09
-
1I guess it is infinite... Haha this is a good question :P– NieckCommented Jun 4, 2016 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.
-
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. Commented Nov 27, 2009 at 17:18
-
1what about json field name length ? can that be as long as variable names in javascript? for example 100 characters long Commented Oct 7, 2014 at 10:00
-
1@Saifee LMGTFY - I reckon this post answers your question well: stackoverflow.com/questions/13367391/…– maurisCommented Oct 7, 2014 at 13:00
-
2@mauris I know this is old but JS is NOT a scripting language, it is compiled– jk121960Commented May 31, 2021 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