Named 'container' for a piece of computer memory that holds a specified value.
- Variable names consist of letters, number, _, $
- Cannot start with a number
Variables are declared by using the keyword var followed by the desired name. e.g.
var name ; //declaration of a variable, undefined
Variables with no value are Undefined.
Values can also be assigned to a variable (aka initialisation) using the = assignment operator:
var age = 42 ; //definition of a variable
Variables can also be emptied by assigning them to a value of null. e.g. myCar = null;
Multiple variables can be separated with commas: var firstName, middleName, lastName ;
Which can also be multiply initiated at definition: var firstName = "Alan", middleName = "Mathison", lastName = "Turing" ;
var myNumber = 200 ; //integer
var myFloat = 3.14 ; //floating point number
Text strings needs to be quoted with single or double quotes but should not be mixed at start/end. However, a single quote could be used within a double quoted string as an apostrophe:
var mySeason = "It's Spring again." ; //double quotes used around a string of text characters. Note use of single quote allowed with double quotes
var myWeather = 'My favourite weather is rainy!' ; //single quotes used around a string of text characters
var myBool = true ; //a boolean could also be assigned false