51
votes

What is the maximum length of a variable name in JavaScript?

2
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
I guess it is infinite... Haha this is a good question :P - Nieck

2 Answers

35
votes

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.

26
votes

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