Bring your web pages to life — learn variables, logic, loops, functions, and data structures.
Variables
Data Types
Operators
Conditionals
Loops
Functions
Arrays
Objects
Quiz
Variables
Variables are like labeled jars in a kitchen. The label is the variable name, and the contents are the value. You can change what's inside the jar anytime, and you use the label to find what you need.
JavaScript has three ways to declare variables:
let — can be reassigned (use this most of the time)
const — cannot be reassigned (use for values that won't change)
var — the old way (avoid in modern JS)
JavaScript — Variables & console.log
Always use const by default. Only switch to let when you know the value will change. Never use var in modern JavaScript — it has weird scoping rules that cause bugs.
Data Types
JavaScript has several built-in data types. Every value you work with is one of these types:
JavaScript — Data Types
Type
Example
Description
String
"hello"
Text data
Number
42, 3.14
Integers and decimals
Boolean
true, false
Yes/no, on/off
undefined
undefined
No value assigned yet
null
null
Intentionally empty
Array
[1, 2, 3]
Ordered list of values
Object
{name: "Alex"}
Key-value pairs
Operators
JavaScript — Operators
Always use === (strict equality), never ==. The loose equality == does automatic type conversion that leads to weird bugs: "0" == false is true!
Conditionals (if/else)
Conditionals are like a traffic light. If the light is green, you go. If it's yellow, you slow down. If it's red, you stop. Your code makes decisions the same way — checking conditions and running different code for each case.
JavaScript — if/else Conditionals
Loops
A loop is like a washing machine cycle. It repeats the same action (wash, rinse, spin) until a condition is met (the timer runs out). In code, loops repeat a block of code until a condition becomes false.
JavaScript — Loops
In modern JavaScript, prefer for...of for looping over arrays and for...in for looping over object keys. The classic for (let i = 0; ...) loop is still useful when you need the index number.
Functions
A function is like a recipe. You write it once, then use it whenever you want. You can pass in different ingredients (arguments) and get a different dish (return value) each time.
JavaScript — Functions
Arrays
An array is like a numbered shopping list. Each item has a position (index) starting from 0. You can add items, remove items, search through the list, and transform every item at once.
JavaScript — Arrays & Array Methods
🎯 Challenge!
Create an array of 5 names
Use .map() to make them all uppercase (hint: name.toUpperCase())
Use .filter() to keep only names longer than 4 characters
Objects
An object is like a contact card. Instead of numbered positions (like arrays), each piece of data has a name (key). A person's contact card might have keys like "name", "phone", "email" — each with its own value.
JavaScript — Objects
Objects are everywhere in JavaScript! DOM elements, API responses, configuration, even arrays — they're all objects under the hood. Mastering objects is the key to mastering JavaScript.
📝 Chapter 4 Quiz
1. Which keyword declares a variable that CANNOT be reassigned?