Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

  1. Create two different types of variables with your own names and values
  2. Print your variables to the console
  3. Change the value of your variables and print them before and after the change
  4. Run your file!
  5. Make sure you save your code with ctrl + s!!