JavaScript variables can store many different data types. A "data type" means the type that the data is. For example a person's name is a string data type (text) and their age is a number data type.
let firstName = "Marcus" // string
let lastName = "Jones" // string
let age = 17 // number
Strings are written inside double or single quotes. Numbers are written without quotes. If you put a number in quotes, it will be treated as a text string.
let age = "Fifteen" // string
let age = "15" // string
let age = 15 // number
A string (or a text string) is a series of characters like "Samuel O'Leary". Strings are written with quotes. You can use single or double quotes.
let country1 = "USA" // double quotes
let country2 = 'Japan' // single quotes
You can use quotes inside a string, as long as they don't match the quotes surrounding the string.
let sentence1 = "Dave O'Leary"; // Single quote inside double quotes
let sentence2 = "He is called 'Dave'"; // Single quotes inside double quotes
let sentence3 = 'He is called "Dave"'; // Double quotes inside single quotes
In JavaScript numbers can be written with or without decimal points.
let price = 4.99 // a number variable with a decimal point
let age = 23 // a number variable with no decimal point
A boolean is a value that is either true or false. JavaScript booleans can only be one of two values, true or false.
let isWearingHat = true;
let isRaining = false;
Booleans are often used to test conditions in conditional statements such as if then
conditional statements.
if (isRaining){
isWearingHat = true;
}else{
isWearingHat = false;
}
JavaScript arrays are used to store multiple values in a single variable.
JavaScript arrays are written with square brackets []
and the array items are separated by commas ,
The following code creates an array called 'team', containing three players.
let team = ["John", "Sarah", "Kyrie"]