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
    Commented Dec 11, 2011 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
    Commented Dec 12, 2011 at 2:52
  • 2
    @vocaro Without more context, I'd call it gibberish.
    – Izkata
    Commented Dec 12, 2011 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
    Commented Dec 12, 2011 at 15:34

9 Answers 9


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

  • 4
    Thanks to all. I'm only able to mark one answer as the accepted answer, but this comparison I found the most enlightening.
    – pimvdb
    Commented Dec 11, 2011 at 21:19
  • beautiful comparison +1 Commented Dec 13, 2011 at 5:28
  • Note that you can "bind a variable to a value" which evokes the image of the universe of eternal values floating around out there and you tape a variable name to one of them. Commented Mar 18 at 23:22
  • Usage example from my old edition of "Programming in Modula-2" (1st edition, 1982) by Niklaus Wirth: We now proceed towards the goal of producing a complete program from the above fragment. We realize that we need to specify an action that assigns initial values to the variables x and y, as well as an action that makes the result visible. Commented Mar 18 at 23:30

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.

  • 1
    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.
    Commented Jul 13, 2019 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.


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.


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.

  • 2
    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
    Commented Mar 13, 2013 at 8:12
  • -1 You are confusing declaration and assignment as @user39420 said.
    – Géry Ogam
    Commented Sep 25, 2021 at 20:20
  • @Maggyero Can you be precise about where you think I'm doing that? I've read my answer over a few times and can't see where you think the confusion is. Commented Sep 27, 2021 at 1:39
  • ‘After you have done this, you intend that the variable will hold that value, but it doesn't actually hold it yet.’ You are describing storage allocation here, not value assignment.
    – Géry Ogam
    Commented Sep 27, 2021 at 7:38
  • @Maggyero I'm not describing storage allocation. I'm describing value assignment. As in, "I will assign the variable sum to hold the sum." It's something a human does, as I clearly state, not something a computer does. Did you miss the part about it being "something you do"? Computers don't assign variables to values, humans do. Computers assign values to variables. Commented Sep 27, 2021 at 16:37

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 and acknowledge you have read our privacy policy.

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