Changing a Variable's Value

Why do we care about variables?

Variables are useful in two ways:

  1. They allow us to use the same value over and over, without having to write a string or other data type over and over.
  2. More importantly, we can assign variables different values that can be read and changed by the program without altering our code.

For example, a weather app can show you a different high temperature every day. Instead of writing a new website everyday, they store the information in a variable and just change the value of that variable.

We can change a variable's value if we want, like this:

var weatherCondition = 'Monday: Raining cats and dogs';
weatherCondition = 'Tuesday: Sunny';

// Output: 'Tuesday: Sunny'
  1. We created a variable by using the keyword var and the name weatherCondition.
  2. Then, we took the existing weatherCondition variable, and set its value equal to 'Tuesday: Sunny'.

True to their name, variables are indeed variable.

Community Forums
Get help and ask questions in the Codecademy Forums
Report a Bug
If you see a bug or any other issue with this page, please report it here.