By the end of this lesson, you will be able to:
:information_source: What is a Variable? A variable is a memory location to store information. It's like a labeled box where you can keep and retrieve data whenever you need it in your program.
We can imagine a variable is a box that can hold something inside.
How to declare a variable with a name called myVariable:
var myVariable;
As you can see, you write the var
and then a name with a spacing in between.
We can give any name to our variable, but there are rules and common practices!
:white_check_mark: Allowed Characters | :x: Not Allowed | Why Not? |
---|---|---|
Letters: a-z, A-Z | Spaces | Variables can't have gaps |
Numbers: 0-9 | Special symbols: !, @, #, % | Reserved for operations |
Underscore: _ | Punctuation: . , ; : | Used for other purposes |
Dollar sign: $ | Math symbols: +, -, *, / | Used for calculations |
:white_check_mark: Correct | :x: Incorrect |
---|---|
var hello123; |
var 123hello; |
var price1; |
var 1price; |
var carName2; |
var 2CarName; |
:white_check_mark: Correct | :x: Incorrect |
---|---|
var myVariable; |
var my Variable; |
var totalNumber; |
var total Number; |
Names are case-sensitive! This means there is a difference between capital and lower-case.
Just like passwords, a capital letter is not the same as a lower-case letter:
myVariable
is not the same as myvariable
fullName
is not the same as fullname
Age
is not the same as age
myFavColour
is not the same as myfavcolour
It is a common practice to write variable names in lower camel cases (camelCase).
Begin with the first letter in lowercase, then the first capital letter for the next word:
:white_check_mark: Good Practice | :x: Hard to Read |
---|---|
var helloWorld; |
var helloworld; |
var lovePeople; |
var lovepeople; |
var pizzaParty; |
var pizzaparty; |
var pieceOfCake; |
var pieceofcake; |
var favouriteRose; |
var favouriterose; |
As you can see, it is very hard to read some names without camel casing.
Camel casing will help separate words without using spacings or symbols.
Certain words cannot be used as variable names because they have other meanings within JavaScript. These are called JavaScript reserved words.
Examples of reserved words you cannot use:
A full list of JavaScript reserved words can be referred here: https://www.w3schools.com/js/js_reserved.asp
JavaScript variables can hold many types of data, but in most situations, we'll only use these 3:
Data Type | Example |
---|---|
String | var myString = "Hello World! 123"; |
Number | var myNumber = 7; |
Boolean | var isFinished = true; var isOngoing = false; |
" "
or single quotes ' '
:var exampleString = "This is a string."; // double quotes
var exampleString2 = 'This is also a string!'; //single quotes
" "
.var myString = "It's the best day ever!";
var myString2 = 'Remember to say "please" and "thank you."';
var myString = "Hello World!";
:white_check_mark: Correct | :x: Incorrect |
---|---|
var myNumber = 42; // This is a number data |
var myNumber = "42"; // This is a string data, not number data |
" "
for your number, the data type will become string.true
or false
.var isFinished = true;
var isOngoing = false;
var removed = false;
var gameOver = false;
var activated = true;
var lightsOn = true;
You can get a value by using its variable name.
Below is an example when we use console.log() to say our variable value:
Code | Browser Console (F12) |
---|---|
var myNumber = 42; console.log(myNumber); |
42 |
You can also do that with alert() to alert our variable value:
Code | Alert Box |
---|---|
var myNumber = 42; alert(myNumber); |
42 |
var myName = "Tommy"; alert(myName); |
Tommy |
var finished = false; alert(finished); |
false |
If you already declared a variable, you can re-assign its value without writing var.
You can just assign the value using the =
assignment operator:
:white_check_mark: Correct | :x: Incorrect |
---|---|
var favouriteColour = "Blue"; favouriteColour = "Yellow"; |
var favouriteColour = "Blue"; var favouriteColour = "Yellow"; |
var myPocketMoney; myPocketMoney = 20; myPocketMoney = 30; |
var myPocketMoney; var myPocketMoney = 20; var myPocketMoney = 30; |
You can actually set the value of a variable using the prompt() method.
Here is an example of using the prompt() method with "What is your name?" as the message.
Then you can console.log() the variable to test the result:
var yourName = prompt("What is your name?");
console.log(yourName);
This way, it will ask you to key in the value in the pop-up box for the variable:
If you key in John as the value and press OK.
It will look like this in the browser console (press F12 to open console):