Variables & Constants in JavaScript (OCR A-Level Computer Science): Revision Notes
📚 Revision Notes
Variables & Constants in JavaScript
Overview
Variables in JavaScript store data values that can be used and modified, while constants are fixed values that cannot be changed once set. Variables and constants are fundamental to storing and processing data in JavaScript.
Key Concepts
- Declaring Variables: Use let or var to declare variables, and const to declare constants.
- Data Types: JavaScript supports types like numbers, strings, and booleans.
lightbulbExample
Example:
let age = 25;
const pi = 3.14159;
infoNote
Common Mistakes
- Reassigning a constant: Constants (const) cannot be reassigned after their initial assignment.
- Using var instead of let or const: var has function scope, which can lead to unexpected results.
infoNote
Key Takeaways
- Use let for variables that change and const for constants.
- Understand the basic data types and when to use each one.