In theory, there is a small amount of stored memory without making a variable declaration, since variable declaration requires the JavaScript engine to create a variable binding object property for the execution context in which you declare it. (See Specification Section 10.5 and related sections.)
In reality, you will never notice the difference, even on a relatively slow engine such as IE. Feel free to use variables wherever they make the code clearer. Creating a property in a variable binding object (for example, declaring a variable) is a very, very, very fast operation. (Avoid it globally, of course, but not because of memory usage.)
Caveat . If a variable refers to a large memory structure that you hold only temporarily, and you create closures in this function context, a large memory structure may (in some engines) be stored in memory longer than necessary. Example:
function foo(element) { var a = ; element.addEventListener("event", function() {
In an inexperienced engine, since the event handler is a closure in the context of the call to foo , theoretically a is available to the handler, and therefore its links remain "link" and are not accessible to the collection garbage until this event handler function ceases to be bound. In any decent engine (like the V8 engine in Chrome), since you are not referencing a and not using eval in the close (event handler), this is great. If you want to protect yourself from mediocre engines, set a to undefined until foo returns, so the engine (no matter how mediocre) knows that the event handler will not reference this structure, even if it were a .
source share