String (that is text) values are placed in quotes or double quotes.
Numbers, including both integers and decimals are not placed in quotes.
If two number variables are created then mathematical operators can be used on them.
var age = 21; var increaseAge = 4; console.info(age + increaseAge);
You should see the value '25' output in the console window. Notice we could have produced the same output with this:
var age = 21; var increaseAge = 4; var newAge = age + increaseAge; console.info(newAge);
Spot the difference? There is a third variable this time called newAge that is assigned the sum of the other two variables.
Try changing the code to:
var age = "21"; var increaseAge = 4; var newAge = age + increaseAge; console.info(newAge);
Warning: Be careful with the quotes. If you want something treated as a number then don't use quotes. If it is text then use quotes.
There are times when of course you would like to concatenate string variables.
var firstName = "Martin"; var surname = "Cooper"; var fullname = firstName + " " + surname; console.info(fullname);
Notice that in the above example we are actually concatenating three strings. They are "Martin", then a whitespace and then "Cooper". If we didn't concatenate in a whitespace then the console would read MartinCooper with no space.
Booleans are simple true / false flags. To create a boolean variable we would use:
var staff = true;
Note that with a boolean only the values
false can be used and that these are not placed in quotes.
null vs undefined
There is a subtle difference between
undefined data types. A
null data type is assigned to a variable that has been defined but not assigned a value ie:
var myWeight; console.info(myWeight); // output undefined
The null data type is used when you deliberately want to set a value to
null as opposed to leaving it
var myIQ = null; console.info(myIQ); // output null
Lots of Variables at Once
We can also declare lots of variables in one go ie:
// declare var firstName, surname, age;
and then initialize them.
// declare var firstName, surname, age; // initialize firstName = "Martin"; surname = "Cooper"; age = 21;
Its Got to be a loose fit
var age; age = 21; // and then later change to a string age = "Young and Dumb";