According to theoretical reasoning, opting out of variable declaration in JavaScript can potentially save a small amount of memory. This is because creating a property on the variable binding object for the execution context is required when declaring a variable. (Refer to Section 10.5 and related sections in the specification for more details).
However, in practical terms, this slight memory-saving technique is unlikely to have any noticeable impact, even on slower engines like IE's. It is perfectly acceptable to use variables wherever they enhance code clarity. The process of creating a property on a variable binding object is very quick, so there is no need to worry about memory consumption unless dealing with global scope.
One caveat to consider: If a variable points to a large memory structure used temporarily, and closures are created within that function context, some engines may retain the large structure in memory longer than necessary. For example:
function foo(element) {
var a = /* ...create REALLY BIG structure here */;
element.addEventListener("event", function() {
// Do something **not** referencing `a` and not using `eval`
}, false);
}
In less advanced engines, the event handler being a closure over the context of the call to `foo` means that `a` remains accessible to the handler until the event handler itself is no longer referenced, preventing garbage collection. However, in modern engines such as V8 in Chrome, as long as `a` is not referenced or `eval` is not used within the closure, it should not cause any issues. To safeguard against mediocre engines, you can set `a` to `undefined` before `foo` returns, ensuring that the event handler does not reference the structure even if it were to refer to `a`.