Javascript Tutorial #2.1 - Variables

Imagine this: You're creating a game, where for every time you pick up some food, your score increases by 1. How would you keep track of the score? This is where variables come in. Variables are objects which have a stored value, which can either be a number, string of characters, a boolean (either true or false), null or undefined. These are incredibly useful, and almost no javascript projects will have no variables.

There are 3 ways to declare a variable, using 3 different keywords, which do 3 different things. These are letvar and const. In this part, we'll cover var.

In order to declare a variable using var, type the following:
var <name> = <value>;
Where <name> is replaced with the name of the variable and <value> is replaced with the value that you want your variable to have. Remember to include the semicolon at the end of the line.

If you want to make the value of your variable a string or a character, remember to put 'quotation marks' around the value.

More examples can be found here.

CHALLENGE: Make a variable using var that stores your favourite colour, and log it to the console.

If you have any questions, let me know in the comments section.
- Noah

Comments