π» Overview
Programming is the process of giving instructions to a computer to perform specific tasks. These instructions are written in programming languages that the computer can understand. This guide covers the fundamental building blocks that are common across most programming languages.
What You'll Learn
- Variables: How to store and manage data
- Data Types: Different kinds of data (numbers, text, etc.)
- Operators: How to perform calculations and comparisons
- Conditionals: Making decisions in your code
- Loops: Repeating actions efficiently
- Functions: Organizing code into reusable blocks
π¦ Variables
A variable is like a labeled box that stores data. You give it a name, and you can put a value inside it. Later, you can retrieve or change that value.
// Creating variables
let playerName = "Alex"; // Stores text
let score = 100; // Stores a number
let isGameOver = false; // Stores true/false
// Changing a variable's value
score = 150; // Now score is 150
Variable Naming Rules
- Must start with a letter, underscore (_), or dollar sign ($)
- Can contain letters, numbers, underscores, dollar signs
- Cannot use reserved keywords (like
if,for,while) - Are case-sensitive (
scoreβScore)
Use playerScore instead of ps or x. Clear names make your code easier to understand.
π Data Types
Data types tell the computer what kind of data a variable holds. Different types behave differently.
| Type | Description | Example |
|---|---|---|
| String | Text (letters, words, sentences) | "Hello, World!" |
| Number/Integer | Whole numbers | 42, -7, 0 |
| Float/Double | Decimal numbers | 3.14, -0.5 |
| Boolean | True or false values | true, false |
| Array/List | Collection of values | [1, 2, 3, 4] |
// Different data types
let name = "Sarah"; // String
let age = 25; // Number (Integer)
let height = 5.7; // Float
let isStudent = true; // Boolean
let colors = ["red", "blue", "green"]; // Array
π’ Operators
Arithmetic Operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 6 * 7 | 42 |
/ | Division | 20 / 4 | 5 |
% | Modulus (remainder) | 17 % 5 | 2 |
Comparison Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | true |
!= | Not equal to | 5 != 3 | true |
> | Greater than | 10 > 5 | true |
< | Less than | 3 < 8 | true |
>= | Greater than or equal | 5 >= 5 | true |
<= | Less than or equal | 4 <= 7 | true |
Logical Operators
| Operator | Name | Description |
|---|---|---|
&& | AND | Both conditions must be true |
|| | OR | At least one condition must be true |
! | NOT | Reverses the boolean value |
π Conditionals (If/Else)
Conditionals let your program make decisions based on whether something is true or false.
let age = 18;
if (age >= 18) {
console.log("You can vote!");
} else {
console.log("You're too young to vote.");
}
let score = 85;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B");
} else if (score >= 70) {
console.log("Grade: C");
} else {
console.log("Grade: F");
}
// Output: Grade: B
π Loops
Loops let you repeat code multiple times without writing it over and over.
For Loop
Use when you know how many times to repeat.
// Print numbers 1 to 5
for (let i = 1; i <= 5; i++) {
console.log(i);
}
// Output: 1, 2, 3, 4, 5
For Loop Breakdown
let i = 1β Start at 1 (initialization)i <= 5β Keep going while i is β€ 5 (condition)i++β Add 1 to i each time (increment)
While Loop
Use when you don't know how many times to repeat.
let count = 0;
while (count < 3) {
console.log("Count is: " + count);
count++;
}
// Output: Count is: 0, Count is: 1, Count is: 2
Always make sure your loop has a way to stop! If the condition never becomes false, your program will freeze.
βοΈ Functions
Functions are reusable blocks of code that perform a specific task. Think of them as mini-programs within your program.
// Defining a function
function greet(name) {
return "Hello, " + name + "!";
}
// Calling the function
let message = greet("Alex");
console.log(message); // Output: Hello, Alex!
Function Parts
- Name: What you call the function (
greet) - Parameters: Inputs the function accepts (
name) - Body: The code inside the curly braces
- Return: The value the function gives back
function calculateArea(width, height) {
return width * height;
}
let area = calculateArea(5, 10);
console.log(area); // Output: 50
- Reusability: Write once, use many times
- Organization: Break complex problems into smaller parts
- Readability: Give meaningful names to blocks of code
- Debugging: Easier to find and fix errors
βοΈ Practice Problems
let x = 10; x = x + 5; console.log(x);
Answer: 15
Explanation: x starts at 10. Then we add 5 to it (10 + 5 = 15) and store it back in x.
function checkEvenOdd(num) {
if (num % 2 === 0) {
return "even";
} else {
return "odd";
}
}
console.log(checkEvenOdd(4)); // "even"
console.log(checkEvenOdd(7)); // "odd"
The modulus operator (%) gives the remainder. If a number divided by 2 has remainder 0, it's even.
for (let i = 10; i >= 1; i--) {
console.log(i);
}
// Alternative with while loop:
let count = 10;
while (count >= 1) {
console.log(count);
count--;
}
Note: i-- decreases i by 1 each iteration (opposite of i++).
function factorial(n) {
let result = 1;
for (let i = n; i >= 1; i--) {
result = result * i;
}
return result;
}
console.log(factorial(5)); // 120
console.log(factorial(3)); // 6
We start with result = 1, then multiply by each number from n down to 1.
β οΈ Common Mistakes
if (x = 5) assigns 5 to x. Use if (x == 5) to compare.
Most languages need semicolons at the end of statements. Missing them can cause errors.
Starting at 0 vs 1, using < vs <= can cause loops to run one time too many or too few.
myVariable and myvariable are different variables. Be consistent!
Track Your Coding Journey
Use Centauri to organize your programming projects and learning goals.
Get Early Access