Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In JavaScript, there is no such thing as a "private" variable. In order to achieve encapsulation and information hiding in JavaScript, I can wrap a variable inside a function closure, like so:

var counter = (function() {    
    var i = 0;
    var fn = {};
    fn.increment = function() { i++; };
    fn.get = function() { return i; };
    return fn;
alert(counter.get()); // alerts '2'

Since I don't call i a private variable in JavaScript, what do I call it?

share|improve this question
s/fn/counter/ outside of the outer anonymous function. – delnan Apr 4 '12 at 19:42
up vote 7 down vote accepted

According to wikipedia they're called "upvalues".

A closure allows a function to access variables outside its immediate lexical scope. An upvalue is a free variable that has been bound (closed over) with a closure. The closure is said to "close over" its upvalues.

Although it's probably better to wrongly call them private variables so people will understand what you mean.

share|improve this answer
You could also just call it a free variable – Raynos Apr 4 '12 at 19:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.