What are variables and constants?
- Variables and constants are used to store a single item of data in a program
- This can be accessed through the identifier
- Variables can be changed during program execution while constants remain the same
Declaring variables & constants
- Variables are declared using a data type, a name and a value (optional)
- Constants are declared using the ‘const’ keyword, a name and a value
- In all programming languages, variable names should follow certain rules, such as:
- Starting with a letter
- Not containing spaces
- Can contain letters, numbers, _ or $
- Not using reserved words (like if, while, for etc.)
- Examples of data types include integer, float, boolean, and string
Examples in pseudocode:
Declare a variable called ‘score’ with a value of 10
score = 10Declare a constant called ‘PI’ with a value of 3.14
const PI = 3.14Examples in JavaScript:
let score = 10;
const PI = 3.14;Examiner Tips and Tricks
- You might see code that declares variables like this
var variableName = value;
- This is bad practice as it utilises global variables but was used until 2015 when let was added to JavaScript