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 and then scroll down to view the sample code.
Here is the HTML code:
<html> <head> <title>Variables</title> <script src="script.js"></script> </head> <body> </body> </html>
Hint: Have a look at the toFixed() method if you wish to display floats with a specified number of decimal places.
Next tutorial: Operators