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?

  • 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

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

  • 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 – indianwebdevil Dec 13 '11 at 5:28

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.

  • Upvoted, but I think the price/commodity is not a very good example. The price could be whatever we want :-), and the same is true for the variable. The commodity, on the other side, cannot be easily changed (it exists "as is"), and the same is true for the value. – john c. j. Jul 13 at 20:52

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.


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.


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.


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.


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.

  • 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.


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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