JavaScript · JS Tutorial · Lesson 7
JS Variables
var, let and const containers for data.
Variables are containers for storing data. JavaScript variables can be declared in 4 ways: automatically (not recommended), with var, with let, and with const.
Example
<p id="demo"></p>
<script>
let x = 5;
let y = 6;
let z = x + y;
const carName = "Volvo";
document.getElementById("demo").innerHTML = "z = " + z + ", car = " + carName;
</script>
When to use what
- Use
constif the value should not be changed - Use
letif the value may change - Only use
varfor old browser support
Keep reading “JS Variables”
Sign up to read the full lesson (1 more parts), try every example in the live editor, and take the 5-question quiz.