Variable names must be unique and cannot contain spaces. They don’t begin with a number but can contain numbers. They generally begin with a lowercase letter and if the variable name contains multiple words, then they can be indicated by using camelcase (starting each new word with an uppercase letter) eg. myVariable.
Variables can store data of different types. The main types are:
- string – text including letters, numbers and other special characters eg. “Hello world”
- integer – whole number values eg. 5
- float – numbers with a decimal point eg. 5.3
- boolean – a true or false value
Watch the video below or click here to view it on YouTube.
This source code shows how create and use variables of different data types. Strings are text (letters, numbers and characters), integers are regular whole numbers (treated as numbers), and floats are real numbers (with decimal places eg. 4.5). Boolean is another data type which is used for true/false values. You’ll also see how to use the HTML br tag to add a new line between text that is displayed in the browser.
Hint: Have a look at the toFixed() method if you wish to display floats with a specified number of decimal places.
Next tutorial: Operators