Skip to content

Latest commit

 

History

History

03_program_structure

Program Structure 🛠️

In JavaScript, understanding program structure is essential for writing clean, organized, and readable code. This guide explores expressions, statements, bindings, functions, control flow, and best practices that help structure your code effectively. Let’s dive in! 🌐

📖 Table of Contents

  1. Expressions and Statements
  2. Bindings
  3. Functions
  4. Control Flow
  5. Capitalization
  6. Comments
  7. Summary
  8. Exercises

1. Expressions and Statements 📜

In JavaScript, expressions and statements are the building blocks of code:

  • Expressions: Pieces of code that evaluate to a value.
    let result = 5 * 10; // 5 * 10 is an expression
  • Statements: Instructions that perform actions, like declaring a variable or calling a function.

Differentiating between expressions and statements is crucial for understanding how code executes.

2. Bindings 🔗

Bindings are like labeled containers that store data values:

let age = 25;

The age variable is a binding that holds the value 25.

Binding Names ✍️

Naming variables follows certain rules:

  • Names must start with a letter, underscore, or $.
  • Avoid reserved words (like let or function).
  • Use descriptive names to enhance readability.

The Environment 🌍

The environment refers to the collection of bindings available at any point in a program. Understanding how variables relate to the environment is key for managing data.

3. Functions 🛠️

Functions are reusable blocks of code that perform specific tasks:

function greet() {
  console.log("Hello, world!");
}

Using functions makes your code modular and more manageable.

The console.log Function 🖥️

console.log outputs data to the console, which is invaluable for testing and debugging.

console.log("This will display in the console.");

Return Values 🔄

Functions can return values to provide output for further use:

function add(a, b) {
  return a + b;
}

4. Control Flow 🔄

Control flow determines the sequence in which code executes.

Conditional Execution 🔍

Use if-else statements to make decisions:

if (age > 18) {
  console.log("Adult");
} else {
  console.log("Minor");
}

while and do Loops 🔁

Loops repeat code based on conditions:

  • while loop: Runs as long as a condition is true.
  • do-while loop: Runs at least once before checking the condition.
let count = 0;
while (count < 5) {
  console.log(count);
  count++;
}

Indenting Code 🖊️

Indentation enhances readability by visually structuring code, especially within loops, conditions, and functions.

for Loops 🔄

for loops provide a concise way to repeat actions a specific number of times:

for (let i = 0; i < 5; i++) {
  console.log(i);
}

Breaking Out of a Loop 🚪

Use break to exit a loop prematurely:

for (let i = 0; i < 10; i++) {
  if (i === 5) break;
  console.log(i);
}

Updating Bindings Succinctly ✏️

Update variable values more efficiently using shorthand operators:

let count = 1;
count += 1; // same as count = count + 1

Dispatching on a Value with switch 🔄

switch statements offer an alternative to if-else chains for multi-way branching:

switch (day) {
  case "Monday":
    console.log("Start of the week");
    break;
  case "Friday":
    console.log("Weekend is near!");
    break;
  default:
    console.log("Midweek");
}

5. Capitalization 🖊️

Follow capitalization conventions:

  • Variables and functions: camelCase (myVariable)
  • Constants: ALL_CAPS (PI)
  • Classes: PascalCase (MyClass)

6. Comments 📝

Comments are essential for explaining code. Use // for single-line and /* ... */ for multi-line comments.

// This is a single-line comment
/*
  This is a multi-line comment
  explaining the code below.
*/

🧠 Tip: Comments should clarify code, not restate it. Focus on explaining the "why" behind complex logic.

7. Summary 🧾

  • Expressions and statements form the basic structure of code.
  • Bindings store values, with specific rules for naming.
  • Functions encapsulate logic, allowing for reusable code.
  • Control flow directs the sequence of code execution.
  • Capitalization and comments contribute to code readability.