JavaScript Basics: Understanding Program Structure


Programming Structure

Now that we know about values and types, let’s talk about how to actually put them together into a running program. This is where we cover expressions, statements, variables, and the rules that make up JavaScript’s basic structure.


Statements and Expressions

In JavaScript, a program is made up of statements. A statement might do something (like change a variable or call a function) or produce a value. Programs are just a sequence of these statements.

1;
!false;

These are statements that produce values but don’t store or use them. Usually, you’ll combine statements with variables, functions, and control flow.


Function Calls

One of the simplest actions is to call a function:

alert("Hello world");

Here, alert is called with a string argument. Arguments are the values between the parentheses.


Multiple Statements

We usually separate statements with semicolons. While sometimes optional, including them is a safe habit:

alert("Hello");
alert("Goodbye");

Variables

We use variables to store values for reuse. Create them with let or const:

let message = "Hello";
alert(message);

We can change variables declared with let:

let counter = 1;
counter = counter + 1;

const means the binding can’t be changed to another value:

const pi = 3.14;

Even with const, if the value is an object, the object’s contents can still change — but the name cannot point to a new object.


Operators

Variables can be updated with:

  • = assignment
  • +=, -=, *=, /= for updating in place
  • ++ and -- for incrementing and decrementing by one
let number = 5;
number += 2;   // 7
number++;

Conditional Execution – if / else

We can make code run only in certain cases using if:

let num = 10;
if (num > 5) {
  console.log("Big number!");
}

We add else for the “otherwise” branch:

if (num > 20) {
  console.log("Huge number!");
} else {
  console.log("Not that huge");
}

Or chain with else if:

if (num > 20) {
  console.log("Huge");
} else if (num > 10) {
  console.log("Large");
} else {
  console.log("Small");
}

Switch Statements

When you need to compare a value against several possible matches, switch can be cleaner than multiple else if blocks.

let fruit = "apple";
switch(fruit) {
  case "apple":
    console.log("Apples are red or green!");
    break;
  case "banana":
    console.log("Bananas are yellow!");
    break;
  case "orange":
    console.log("Oranges are... orange!");
    break;
  default:
    console.log("I don't know that fruit.");
}

Loops

while runs as long as its condition remains true:

let count = 0;
while (count < 3) {
  console.log(count);
  count++;
}

do...while always runs the body once before checking:

let value = 0;
do {
  console.log(value);
  value++;
} while (value < 3);

for loops are compact for counting through a range:

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

Breaking and Continuing

break exits the loop early; continue skips to the next iteration:

for (let i = 0; i < 5; i++) {
  if (i === 2) continue; // skip 2
  if (i === 4) break;    // stop at 4
  console.log(i);
}

Functions

Functions let us group code to reuse it:

function square(x) {
  return x * x;
}
console.log(square(12)); // 144

A function can have multiple parameters:

function add(a, b) {
  return a + b;
}
console.log(add(2, 3)); // 5

If return is omitted, the function returns undefined.


Comments

Comments are notes for humans reading the code; JavaScript ignores them.

  • Single-line comments start with //
  • Multi-line comments go between /* and */
// This is a single-line comment
let count = 0; // comment after code

/*
This is a
multi-line comment
*/

Use comments to explain why something is done, not just what it does.


Capitalization Conventions

JavaScript is case-sensitive — myVariable and myvariable are two different identifiers.

  • Use camelCase for variables and functions → thisIsCamelCase
  • Use SCREAMING_SNAKE_CASE for constants → MAX_WIDTH
  • Use PascalCase for class names → Car, UserProfile

Choose a style and stick with it for readability.


Summary

In program structure we’ve learned that:

  • Programs are built from statements and expressions.
  • Variables store values; operators change or compare them.
  • Conditionals like if/else and switch decide what code runs.
  • Loops execute code multiple times, with break and continue to adjust the flow.
  • Functions package code to reuse it.
  • Comments help humans understand code without changing behavior.
  • Consistent capitalization keeps code clear and predictable.

With these fundamentals, you can already write small but meaningful programs in JavaScript.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *