CODY ← Back to Courses
JS

JavaScript — The Language of the Web

Make web pages interactive and dynamic. From DOM manipulation to APIs, JavaScript powers everything modern on the web.

Interactive Pages Most Used Language Frontend & Backend

What is JavaScript?

JavaScript is the programming language of the web. It runs in every browser and makes HTML pages interactive — from form validation to complex apps.

🖱 Interactivity

Respond to user clicks, keyboard input, form submissions and mouse events.

🌐 DOM Control

Read and modify HTML and CSS on the fly — add elements, change styles, show/hide content.

🔗 APIs

Fetch data from servers, work with JSON, connect to third-party services.

⚙️ Server-side

With Node.js, JavaScript runs on servers too — full-stack in one language.

how to add JS to a page
// Method 1: inline <script> tag
<script>
  console.log("Hello, world!");
</script>

// Method 2: external file (preferred)
<script src="script.js"></script>

Variables

Variables store data. JavaScript has three ways to declare them — understanding when to use each is essential.

var / let / const
// let — can be reassigned
let score = 0;
score = 10; // ✅ OK

// const — cannot be reassigned
const MAX_SCORE = 100;
// MAX_SCORE = 200; ❌ Error!

// var — old way, avoid it
var name = "Alice"; // has scoping issues
💡 Rule of thumb: Use const by default. Switch to let only when you need to reassign. Never use var in modern code.

Data Types

JavaScript has several built-in types. Understanding them prevents common bugs.

TypeExampleNote
String"Hello" / 'World'Text. Use template literals for interpolation.
Number42 / 3.14All numbers (int and float) are the same type.
Booleantrue / falseYes/No, on/off values.
Array[1, 2, 3]Ordered list of values.
Object{ name: "Bob" }Key-value pairs.
nullnullIntentionally empty value.
undefinedundefinedVariable declared but not assigned.
template literals
const name = "Alice";
const age = 25;

// Old way:
console.log("My name is " + name + " and I am " + age);

// Modern: template literal with backticks
console.log(`My name is ${name} and I am ${age}`);

Functions

Functions are reusable blocks of code. JavaScript has several ways to write them — arrow functions are the modern standard.

function types
// Function declaration
function greet(name) {
  return `Hello, ${name}!`;
}

// Arrow function (modern)
const greet = (name) => `Hello, ${name}!`;

// With default parameter
const greet = (name = "World") => `Hello, ${name}!`;

// Call the function
console.log(greet("Alice")); // → "Hello, Alice!"
console.log(greet());        // → "Hello, World!"
▶ INTERACTIVE — click Run to execute
const add = (a, b) => a + b; console.log(add(5, 3)); // 8 console.log(add(10, 25)); // 35

Conditionals

Conditionals let your code make decisions based on values. The if/else statement is the foundation of program logic.

if / else / ternary
const score = 72;

// if / else if / else
if (score >= 90) {
  console.log("A — Excellent!");
} else if (score >= 70) {
  console.log("B — Good");
} else {
  console.log("C — Keep practicing");
}

// Ternary — compact version
const result = score >= 70 ? "Pass" : "Fail";
▶ INTERACTIVE — check login
const user = "admin"; const pass = "1234"; const ok = user === "admin" && pass === "1234"; console.log(ok ? "✅ Logged in!" : "❌ Wrong credentials");

Loops

Loops repeat code. The for loop and forEach method are the ones you'll use most.

loops
// Classic for loop
for (let i = 0; i < 5; i++) {
  console.log(`Step ${i + 1}`);
}

// while loop
let count = 3;
while (count > 0) {
  console.log(`Countdown: ${count}`);
  count--;
}

// forEach on array (most common)
["HTML", "CSS", "JS"].forEach(lang => {
  console.log(`Learning: ${lang}`);
});

Arrays & Objects

Arrays store ordered lists; objects store named properties. Together they model almost any real-world data.

arrays
const skills = ["HTML", "CSS", "JavaScript"];

skills.push("React");     // add to end
skills.pop();             // remove from end
skills.length;           // → 3
skills[0];               // → "HTML"

// Powerful array methods
const upper = skills.map(s => s.toUpperCase());
const web   = skills.filter(s => s !== "HTML");
objects
const user = {
  name: "Alice",
  age: 25,
  skills: ["HTML", "CSS"],
  greet() { return `Hi, I'm ${this.name}`; }
};

console.log(user.name);      // → "Alice"
console.log(user["age"]);   // → 25
console.log(user.greet()); // → "Hi, I'm Alice"

DOM & Events

The DOM (Document Object Model) is JavaScript's view of the HTML page. You can read and change it — and react to user events.

select & modify elements
// Select elements
const btn   = document.getElementById("myBtn");
const title = document.querySelector("h1");
const cards = document.querySelectorAll(".card");

// Modify content and style
title.textContent = "New Title";
title.style.color  = "crimson";
btn.classList.add("active");
btn.classList.remove("hidden");
btn.classList.toggle("open");
event listeners
const btn = document.getElementById("myBtn");

// Listen for a click event
btn.addEventListener("click", () => {
  alert("Button clicked!");
});

// Input event — fires every keypress
const input = document.getElementById("search");
input.addEventListener("input", (e) => {
  console.log(e.target.value);
});

🛠 Project: Interactive Counter

Build a click counter that combines HTML, CSS and JavaScript — your first fully interactive web component!

counter.html
<!-- HTML -->
<div id="app">
  <h2 id="count">0</h2>
  <button onclick="change(-1)">−</button>
  <button onclick="change(1)">+</button>
  <button onclick="reset()">Reset</button>
</div>

<!-- JavaScript -->
<script>
let n = 0;

function change(delta) {
  n += delta;
  document.getElementById("count").textContent = n;
}

function reset() {
  n = 0;
  document.getElementById("count").textContent = n;
}
</script>
🎯 Challenge: Make the counter color change: red when negative, green when positive, white at zero. Use JavaScript to update style.color inside the change() function.