What are variables?
Variables store data that you’ll use throughout your program - user input, API responses, calculation results, and more. JavaScript has two ways to declare variables:let for values that change, and const for values that don’t.
Declaring variables
const prevents reassignment - once you set a value, you can’t change it. let allows the value to be updated later.
Updating variables
let, you can reassign the variable as many times as needed. Use +=, -=, *=, /= for arithmetic shortcuts.
Naming conventions
JavaScript variable names can contain letters, numbers, underscores, and dollar signs - but they can’t start with a number.
Variable scope
const and let are “block-scoped” - they only exist within the nearest set of curly braces {}.
Comparing to Python
- JavaScript
- Python
const helps you catch bugs by preventing accidental reassignments.
Common mistakes
Trying to reassign a const
Trying to reassign a const
Thinking const makes objects immutable
Thinking const makes objects immutable
Using var instead of let/const
Using var instead of let/const
What’s next?
Now that you can store data in variables, let’s learn about the different types of data JavaScript can handle.Data types
Numbers, strings, booleans, and more