Variables and Data
Declaring a Variable
Before we can use a Variable, we must declare the variable. We do this by typing the keyword var followed by the variable name.
Initializing a Variable
Once we have declared our variable we can then initialize it with a value by typing the variable name followed by a = symbol (assignment operator — more on this will be covered in future posts) and then the value you wish to store in that variable:
var myVariable1 = 5;
Let’s break that down:
var (declaring the variable) myVariable1 (variable name) = (assignment operator) 5 (value);
break case catch class const continue debugger default delete do else export extends finally for function if import in instanceof new return super switch this throw try typeof var voide while with yield
We can have multiple variables like so:
var myVariable1 = 5;
var myVariable2 = 3;
var myVariable3 = 2;
Numbers, Strings and Booleans
A variable value can be a number, (like the examples above), a text value which is called a string, or a Boolean which can have a value of true or false.
When using a string, the contents, whether that be numbers or letters, must be contained within single or double quotations. Containing numbers inside quotations will turn it into a string.
var myVariable4 = "Hello World";
var numberVariable1 = "56";
var myVariable5 = 'Hello World';
var numberVariable2 = '10.5';
All of the above are strings, however a time may crop up when you have a string that contains quoted text, this causes some hiccups. The string will stop running at the second quotation mark:
var myVariable6 = "The farmer said "My Apples are 70p each" to the greengrocer";
var myVariable7 = 'The boy had been to see 'Toy Story' at the cinema';
To avoid this, you can use something called an escape character (\), which is one single backslash entered before your nested quotation mark:
var myVariable6 = "The farmer said \"My Apples are 70p each\" to the greengrocer";
var myVariable7 = 'The boy had been to see \'Toy Story\' at the cinema';
This will allow the string to be run to the end quotation like it is intended to do so.
There are various special characters that can be used in similar ways. We don’t go into those just yet, but feel free to have a nosey on Google if you would like to know more.
To keep a number from turning into a string, the number mustn’t be contained in quotations. We can have any number value and even decimals:
var myVariable8 = 2;
var myVariable9 = 159;
var myVariable10 = 0.5;
A Boolean is a data type which represents one of two values: true or false. Just like numbers, Booleans do not need to be contained in quotation marks as this would turn it into a string.
var myVariable11 = true;
var myVariable12 = false;
Updating a Variable
Once we have a declared variable with a unique name and it has been initialised with a value, we can then look at changing that variable should we ever need to.
We can do this with a simple line of code that can change the value of our variable like so:
var myName = "Leanne";
var myAge = 32;
myName = "Lea"; // This will change the value of myName from "Leanne" to "Lea".
The variable myName now has the value of “Lea”.