最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Difference between identifier and variable (in JavaScript) - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Jul 7, 2021 at 18:11 Heretic Monkey 12.1k7 gold badges61 silver badges130 bronze badges asked Jan 28, 2015 at 6:17 overexchangeoverexchange 1 4
  • Does this help? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – shree.pat18 Commented Jan 28, 2015 at 6:20
  • @shree.pat18 No, Because, I do not understand the meaning of Variable name. It can be any legal identifier. – overexchange Commented Jan 28, 2015 at 6:31
  • 1 You could think about a variable and an identifier to refer to the same thing but in different contexts. Given var var1;, from a source code or AST perspective, var1 is an identifier. When the code is actually executed, var1 represents a variable. When talking about code it's someimtes easier to not distinguish between those though. The term "identifier" has a very specific meaning in JavaScript btw: es5.github.io/#x7.6. Here is an other example of source vs runtime: {foo: 'bar'}. This is an object literal which, at runtime, evaluates to an object. – Felix Kling Commented Jan 28, 2015 at 6:31
  • What is a variable? Just a location to store a value. And when we want to talk about it, we like to give it a name - from the set of identifier values. So "var1" on its own is an identifier, but the variable var1 is a variable. – Bergi Commented Jan 28, 2015 at 6:40
Add a comment  | 

3 Answers 3

Reset to default 15

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.

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.

var1, var2, var3 are identifiers where as their actual values which are 1, function() and callToFunction are actual variables. The values of the variables may differ but its identifiers is always the same.

发布评论

评论列表(0)

  1. 暂无评论