JavaScript Variables
Variables are containers for storing data values. In this example, x, y, and z, are variables, declared with the var keyword:
javascript
var x = 5;
var y = 6;
var z = x + y;When to Use var, let, or const?
- Always declare variables
- Use
constif the value should not be changed - Use
constif the type should not be changed (Arrays and Objects) - Only use
letif you can't useconst - Only use
varif you MUST support old browsers.
Test Yourself with an Exercise
Which keyword is used for a variable whose value cannot be changed?