Take the 2-minute tour ×
English Language & Usage Stack Exchange is a question and answer site for linguists, etymologists, and serious English language enthusiasts. It's 100% free, no registration required.

I was wondering which of these phrases is/are correct:

  • assign a variable to a value
  • assign a value to a variable

I'd say the second is correct, but I'm not a native speaker. A quick Google search reveals thousands of results such as "assign a variable to the file content", but another search returns a lot of results for the other word order ("assign a value to HTML input field").

Which of these two phrases is correct? Or are both correct?

share|improve this question
6  
There's a clue in the name "variable": this is something that can vary or change. –  Hugo Dec 11 '11 at 22:43
    
I believe "assign a variable to the file content" is just a lazy/informal way of saying "declare a variable for the file content". –  vocaro Dec 12 '11 at 2:52
2  
@vocaro Without more context, I'd call it gibberish. –  Izkata Dec 12 '11 at 3:44
    
I understand that the variable is the receiver, but I was not sure whether or not that implies only option #2 is correct. –  pimvdb Dec 12 '11 at 15:34

9 Answers 9

up vote 31 down vote accepted

Assign a value to a variable is correct. Alternatively, you could say assign a variable a value.

Compare assign and give:

Give a value to a variable vs. give a variable a value

share|improve this answer
3  
Thanks to all. I'm only able to mark one answer as the accepted answer, but this comparison I found the most enlightening. –  pimvdb Dec 11 '11 at 21:19
    
beautiful comparison +1 –  nepsdotin Dec 13 '11 at 5:28
    
@nepsdotin Thanks. –  Terry Li Dec 13 '11 at 5:38

You want "assign a value to a variable"; it is the variable that is receiving the assignment of something. Think of the same sentence with "price" for "value" and "commodity" for "variable"; you assign a price to a commodity but not the other way around.

share|improve this answer

A variable is a quantity that may represent any one of a set of values. So you assign a value to a variable. The second one is correct.

share|improve this answer

Assign a value to a variable. The confusion may arise from the fact that you can correctly say assign a variable a value, which has the same meaning.

share|improve this answer

Being a math teacher, I immediately began running my classroom dialogue through my head looking for common usage (which of course isn't always correct usage).

So, from the trenches, we say "Assign a value to a variable."

When we take a value, assign it a variable name so we can look at behavior for all possible values, we call that a parameter, rather than a variable.

However, we don't typically assign values to variables. Rather we define variables to represent quantities, and then solve for values of interest under the given conditions.

NOTE: I recognize that the StackExchange is for providing answers, rather than getting into discussions. I offered the above in case @pimvdb was writing in/about an unfamiliar field and could benefit from some "insider" knowledge. If the moderators need to delete this, that won't bother me.

share|improve this answer

I agree with other answers that "assign a value to a variable" is the most common and generally intended answer. However, there is one occasion where "assign a variable to a value" would make sense: When you are trying to generalize, say, a mathematical example or solution, you can take a value and assign it a variable to generate the solution for all possible values of that variable.

share|improve this answer

They mean two different things.

To "assign a value to a variable" is something you tell the computer to do. Once the value has been assigned to the variable, the variable holds that value.

To "assign a variable to a value" is something you do. It means to designate a specific variable to hold a certain value. After you have done this, you intend that the variable will hold that value, but it doesn't actually hold it yet.

So, to "assign a variable to hold the contents of the file" means to create or select a variable that will at some point hold the contents of the file. To "assign the contents of the file to the variable" means to perform the steps so that the variable actually does hold the contents of the file.

share|improve this answer
    
When a variable isn't assigned a value, it's actually said to be just declared. A variable declaration and assignment can be (and often is) done in a single statement: var color = "green"; However, the declaration can be done in one statement: var color; and the assignment in another: color = "green"; (Of course, the assignment must follow the declaration and not precede it.) My point is that variable declarations and variable assignments are not the same thing. –  user39420 Mar 13 '13 at 8:12

Well, according to me, assign a value to variable is correct because technically we always assign some value to a variable that holds that assigned value and assign a variable to value is wrong technically. We cannot assign a variable to a value. Please let me know if there is any confusion.

share|improve this answer

Both were right in their context and the exactness depends on how the user interprets and requires to be noted.

share|improve this answer

Your Answer

 
discard

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.