๐ Program Flow: Code Structure
The Story of the Invisible Notes ๐
Imagine youโre writing a recipe for your favorite cookies. Sometimes you want to write little notes to yourself like โMomโs secret ingredient!โ or โDonโt forget to preheat!โ These notes help YOU remember things, but theyโre not actual cooking steps.
JavaScript has the same idea! You can write notes in your code that the computer completely ignores. These notes are called comments.
๐จ๏ธ Comments: Your Secret Notes
What Are Comments?
Comments are messages you write for humans (yourself or other programmers). The computer skips right over them!
Think of it like this:
- ๐ช Recipe step: โMix flour and sugarโ โ The baker DOES this
- ๐ Your note: โGrandma taught me this trickโ โ The baker IGNORES this
Two Ways to Write Comments
1. Single-Line Comments โ Use //
// This is a comment
let cookies = 12; // I made a dozen!
2. Multi-Line Comments โ Use /* */
/*
This is a longer note.
It can span many lines!
Perfect for big explanations.
*/
let recipe = "chocolate chip";
Why Use Comments?
| Reason | Example |
|---|---|
| Explain tricky code | // Convert Celsius to Fahrenheit |
| Leave reminders | // TODO: Add error handling |
| Temporarily disable code | // console.log(debug); |
โก Statements vs Expressions
Hereโs a fun way to remember this:
Statements are like complete sentences โ They DO something Expressions are like math problems โ They PRODUCE a value
Expressions: They Give You Something Back
An expression is any code that results in a value:
5 + 3 // โ 8
"Hi" + "!" // โ "Hi!"
age > 18 // โ true or false
Think of expressions like questions that always have an answer!
Statements: They Take Action
A statement is a complete instruction that performs an action:
let name = "Alex"; // Creates a box
if (happy) { dance(); } // Makes a decision
for (i=0; i<3; i++) {} // Repeats something
The Key Difference
graph TD A[Your Code] --> B{Does it produce a value?} B -->|YES| C[Expression ๐งฎ] B -->|NO, it does something| D[Statement ๐] C --> E["5 + 3 โ gives 8"] D --> F["let x = 5 โ creates x"]
Quick Test:
7 * 2โ Expression (gives 14)let score = 100;โ Statement (creates score)score > 50โ Expression (gives true/false)
๐ช Automatic Semicolon Insertion (ASI)
The Invisible Helper
In JavaScript, semicolons (;) mark the end of statements, like periods end sentences.
let a = 5;
let b = 10;
But JavaScript has a secret helper called ASI (Automatic Semicolon Insertion). If you forget semicolons, JavaScript tries to add them for you!
let a = 5 // No semicolon? JS adds one!
let b = 10 // Same here!
โ ๏ธ When ASI Goes Wrong
Sometimes the helper makes mistakes. Look at this trap:
function getNumber() {
return
42
}
JavaScript sees this as:
function getNumber() {
return; // ASI added semicolon here!
42 // This never runs!
}
Result: The function returns undefined instead of 42! ๐ฑ
The Safe Way
function getNumber() {
return 42; // Keep return and value together!
}
ASI Survival Tips
| Do This โ | Not This โ |
|---|---|
return 42; |
return (newline) 42 |
Keep { on the same line |
Put { on the next line |
| Add semicolons yourself | Trust ASI completely |
๐ Strict Mode: The Rule Enforcer
What Is Strict Mode?
Remember when you were little and your parents said โNo cookies before dinner!โ? Thatโs a strict rule.
Strict mode is like having a strict teacher for your JavaScript. It catches mistakes that normal JavaScript would ignore!
How to Turn It On
Add this magic line at the very top of your file:
"use strict";
// Now JavaScript is extra careful!
let x = 10; // โ
Works fine
y = 20; // โ ERROR! y wasn't declared
What Strict Mode Catches
graph TD A[Strict Mode Catches] --> B[Undeclared Variables] A --> C[Duplicate Parameters] A --> D[Reserved Words as Names] A --> E[Other Mistakes] B --> B1["x = 5 โ ERROR!"] C --> C1["function f#40;a, a#41; โ ERROR!"] D --> D1["let let = 1 โ ERROR!"]
Examples
Without Strict Mode (Sloppy Mode):
oops = "I forgot let!";
// Works, but creates global variable! ๐
With Strict Mode:
"use strict";
oops = "I forgot let!";
// ReferenceError: oops is not defined โ
Why Use Strict Mode?
| Benefit | What It Means |
|---|---|
| ๐ Catches bugs early | Errors now, not later |
| ๐ Safer code | Prevents accidental globals |
| ๐ Better performance | Some optimizations possible |
| ๐ Future-ready | Prepares for new JS features |
๐ฏ Quick Summary
| Concept | What It Does | Example |
|---|---|---|
| Comments | Notes for humans | // This is a note |
| Expressions | Produce values | 5 + 3 โ 8 |
| Statements | Perform actions | let x = 5; |
| ASI | Auto-adds semicolons | Be careful with return! |
| Strict Mode | Catches mistakes | "use strict"; |
๐ You Did It!
Now you understand the building blocks of JavaScript code structure:
- โ Write comments to help yourself and others
- โ Know that expressions give values, statements take action
- โ Donโt fully trust ASI โ add your own semicolons!
- โ Use strict mode to catch bugs early
Youโre ready to write cleaner, safer JavaScript! ๐