Part 2.2: Variables
declaring & reassigning variables
You can also create variables in javascript with their own names. These are used everywhere in programming to store values. Some variables can be changed later on as the context of your code changes. Refer to the slides to see the main reasons they're used!
// There are two main ways to declare a variable in javascript
// Constant variables
const unchanging = 1;
unchanging = 5; // will throw an error
// Mutable (changeable) variables
// The name of the variable here is planet, and the value is "earth".
let planet = "earth";
planet = "jupiter";
printing variables
You can print your variables with console.log()
. You can combine this with reassigning variables
to print different messages.
let message = "Hello World!";
console.log(message);
message = "I love CompClub!";
console.log(message);
question
There are some words you cannot use as variable names. Do you remember what these are? You can refer to the slides if you don't remember!
tasks
- Create two different types of variables with your own names and values
- Print your variables to the console
- Change the value of your variables and print them before and after the change
- Run your file!
- Make sure you save your code with
ctrl + s
!!