Selection in JavaScript (OCR A-Level Computer Science): Revision Notes
📚 Revision Notes
Selection in JavaScript
Overview
Selection structures (conditionals) in JavaScript allow the code to make decisions based on conditions, using statements like if, else, and switch.
Key Concepts
- if...else: Executes code based on a true or false condition.
- switch statement: Useful for multiple conditions.
lightbulbExample
Example:
let age = 18;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
Common Mistakes
- Incorrect syntax in conditions: Make sure conditions are properly formatted and closed.
infoNote
Key Takeaways
- Selection structures control programme flow based on conditions.
- if...else and switch are the main selection statements.