Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between identifier and variable (in JavaScript)

I had been looking through this query for the answer.

With the below JavaScript syntax:

var var1 = 1;
var var2 = function(a, b){
         return a + b;
      };
var var3 = var2(3, 5);

I would like to know, whether var1/var2/var3 are variables or identifiers.
A bit confused with var keyword in JavaScript.

like image 453
overexchange Avatar asked Jan 28 '15 06:01

overexchange


2 Answers

The difference between identifiers and variables is the same as that between names and people.

Names identify people. They can also identify dogs, for example. Names are not people, nor are people names. But you can say that I am Amadan (since saying that I am identified by the name Amadan sounds clunky).

In the same way:

Identifiers identify variables. They can also identify labels, for example. Identifiers are not variables, nor are variables identifiers. But you can say that that variable is var2 (since saying that that is the variable identified by the identifier var2 sounds clunky).

I would like to know, whether var1/var2/var3 are variables or identifiers.

Is Amadan a person or a name? Both, I suppose, depending on how strictly you view it. And for variables and identifiers, the answer is again completely parallel.

EDIT:

Can I say, Name and Value, in general?

To be precise, "value" is a third concept, and "contents" of a variable a related fourth one.

Maybe a better analogy for a variable would be locker boxes: they have identifiers (the number written on the box) and contents (whatever you put inside). A variable is not necessarily the memory location of a value, because a variable can contain a reference, and not an object itself (kind of like putting an address of a piece of real-estate into a locker, as opposed to trying to stuff a whole house into the box). So, in this stretched example, the house is the value; the locker is the variable; the #284 written on the locker is the identifier; and the piece of paper with "102 Nowhere Lane, Nowhereville" is a reference to the value, and also the contents of the variable. If the value is small and simple enough (in programming terms, a "primitive"), you can stuff the value itself into the variable, instead of the reference.

For instance:

var a = 1;         // assign a value
var b = [2, 3, 4]; // assign a reference
var aa = a;        // copy the contents
var bb = b;        // copy the contents

declares four variables (a, b, aa, bb), and four identifiers to name them (also a, b, aa and bb); it also mentions many values (1, 2, 3, 4, the array [2, 3, 4]). a and aa each contain a different copy of the primitive value 1. b contains the reference to the value [2, 3, 4] (not the value [2, 3, 4] itself!), which, in turn, contains the values 2, 3 and 4. bb contains another copy of... the reference! So if you change the value that is contained in b, the value in bb automagically changes too:

b.push(5);
console.log(b);
// => [2, 3, 4, 5]
console.log(bb);
// => [2, 3, 4, 5]

Functions are also values.

function hello(name) {
  console.log("Hello, " + name);
}

is (almost but not 100%) identical to

var hello = function(name) {
  console.log("Hello, " + name);
}

which defines a variable whose identifier is hello, and whose contents is a reference to a function. The function itself is a value.

like image 56
Amadan Avatar answered Oct 07 '22 02:10

Amadan


The following is based on, and inspired by, a 'stackoverflow' post by Amadan on 28 Jan 2015; I've re-worked it for my personal benefit and clarification then thought to share it in case it may be of some help to others when trying to get their mind around this topic.

The distinction between identifiers and variables has an equivalence as that between names and people.

Names identify people, and can also identify dogs, horses, pets, etc. Names are not people, nor are people names. Generally, it is easier to say "I am Amadan" than say "I am associated with the name Amadan", the latter is less efficient in conversation though more concise in meaning.

Hence, is Amadan a person or a name? Well, it depends on the context in which you view the question. And so it is also for variables and identifiers.

In the same way:- Identifiers can be associated with variables; they can also be associated with labels, functions, procedures, programs, apps, classes, methods, etc. Identifiers are not variables, variables are not identifiers. So, it may be said that some arbitary variable is 'var2', which avoids the slightly 'clunky' way of saying "I'll use a variable and associate it with the identifier 'var2'".

So now, the question may occur "Can I instead generally use the terms Name and Value?". For me, No!, generally not because, to be precise, "value" introduces a third concept, and incidentally, the "contents" of a variable introduces a related fourth concept.

Maybe a good analogy for a variable would be to consider a locker box: they both have identifiers (some number/name/colour/icon) and contents (whatever is held there).

A variable is not necessarily a memory location for a value, it can be a memory location for a reference to an object, and not contain the object itself (it's kind of like putting the address of a piece of real-estate into a locker, as opposed to trying to put that piece of real estate into the locker).

So, to summarize this analogy, the locker box is the variable; the identifier is the number/name/colour/icon for the locker box; the contents is a piece of paper with an address on it; the reference is the address for the piece of real estate; the value is the piece of real estate.

Hence, if a value is small and simple enough (in programming terms, a "primitive") the value itself can be directly assigned to the variable, rather than assign the reference to that value.

For instance:-

var a = 1; // assigns a value
var b = [2, 3, 4]; // assigns a reference
var aa = a; // copy the contents of 'a' to 'aa'
var bb = b; // copy the contents of 'b' to 'bb'

the above declares four variables and 'allocates' four identifiers (a, b, aa, bb);
it also specifies numerous values (1, 2, 3, 4, the array []);
a and aa each contain a different copy of the primitive value 1;
b contains references [2], [3], [4] to some values which, in turn, for this example happen to contain the values 2, 3, 4;
bb contains a copy of the references held by aa.

So if a value that is contained in b changes, the value in bb automagically changes also:-

b.push(5);
console.log(b);  
// console shows [2, 3, 4, 5]  
console.log(bb);  
// console shows [2, 3, 4, 5] 

Functions are also values:-

function greeting(name) {  
  console.log("Hello, " + name);  
}

the above function uses the identifier 'greeting',
it is (almost but not 100%) identical to:-

var greeting = function(name) {  
  console.log("Hello, " + name);  
}

the above variable, associated with the identifier 'greeting'
is 'assigned' with a function;
the contents of both code blocks refer to a function - console.log();
each function itself is a value.

like image 28
yoyojoe Avatar answered Oct 07 '22 03:10

yoyojoe