⬅️ Assignment

5th Grade 💻 Coding

Assignment is the action that puts a value into a variable. In Python and JavaScript you do it with a single equals sign: `x = 5`.

Free · US Letter · no sign-up
Google Classroom Save to Pinterest Email me

In an assignment, the computer first works out whatever is to the right of the = sign, and only then puts the result into the variable on the left. That’s why x = x + 1 makes perfect sense: the computer reads the current value of x, adds 1 and stores the result back in x. In math that would be an equation with no solution — in programming it’s an action.

An assignment wipes out the old value: after x = 5 and then x = 8, x holds only 8. That’s the difference from the comparison sign ==, which asks “Are these equal?” and doesn’t change anything. In JavaScript, you also write let the first time you assign a variable.

For example

  • name = "Ava" puts the text “Ava” into the variable name.
  • After a = 3 and a = a * 2, the value of a is 6.

📌 Remember: Assignment: work out the right side first, then store it on the left.

Quick quiz

  1. What does an assignment do?

  2. If x = 4 and then x = x + 1, what is x?

  3. What happens to the old value when you assign a new one?

🔎 Ask Thinkapedia another question